Copying files with Robocopy
Published on: 25th Nov 2019
Updated on: 16th Jan 2022
Explanation
Copying files using the Copy-Item
command can be slow. The fastest copying utility on Windows is Robocopy. The following script copies all files in D:\temp3\test
to two destinations listed in dst-dir.csv
file.
Here is the script for copy-file-to-dir.ps1
## sample values
#
#$src = 'D:\temp3\test'
#$dst_csv = "$PSScriptRoot\dst-dir.csv"
#
param (
[string]$src = $(Read-Host("Source directory")),
[string]$dst_csv = $(Read-Host("Destination CSV file name (blank for default)"))
)
if ($src.Length -eq 0) {
Write-Host "==> the source directory has not been specified. Exit."
return
}
if (!(Test-Path $src)) {
Write-Host "==> the source directory does not exist. Exit."
return
}
if ($dst_csv.Length -eq 0) {
#set default dir
$dst_csv = "$PSScriptRoot\dst-dir.csv"
}
if (!(Test-Path $dst_csv)) {
$dst_csv2 = "$PSScriptRoot\$dst_csv"
# test to see if the user passes in the filename without a path
if (!(Test-Path $dst_csv2)) {
Write-Host "==> the CSV file does not exist. Exit."
return
}
else {
# the user pass in the filename without path
$dst_csv = $dst_csv2
}
}
#====================================
$dst = Import-Csv -Path $dst_csv
$item_cnt = $dst.Length
#====================================
Write-Host "=> cloning source = $src"
Write-Host "=> destination csv file $dst_csv (found $item_cnt destinations)"
Write-Host
$ans = Read-Host("Proceed with the copy process [y/n]?")
if ($ans -eq "n") {
Write-Host "==> the process has been cancelled"
return
}
Write-Host
Foreach ($item in $dst) {
$d = $item.dst_dir
Write-Host -NoNewline "==> copying to $d"
$cmd = "robocopy $src $d /S /E /DCOPY:T /R:10 /W:10 /NP /TEE"
Invoke-Expression -Command $cmd | Out-Null
Write-Host "=> OK"
}
The contents of the "dst-dir.csv" file where it has 3 lines. The first line is the header line and the next 2 lines are the directory to be copied.
dst_dir
D:\temp3\apps\app1\js
D:\temp3\apps\app2\js
Notes
Read-Host("Source directory")
- prompts the message on the screen and waits for user input.[string]$src = $(Read-Host("Source directory"))
- the$(..)
means read the result fromRead-Host
and then assign it to the$src
parameter.Test-Path $dst_csv
- returns true if the$dst_csv
file exists.Test-Path
works for file systems and other systems such asIIS:\
drive (imported fromWebAdministration
module). So, it depends on the context that you are running.Import-Csv -Path $dst_csv
- import the file using CSV format.Write-Host -NoNewline ".."
- show the message on the screen without ending with a line feed. As a result, the nextWrite-Host
will continue writing the message from the cursor position.Invoke-Expression -Command $cmd
- executes the DOS command.| Out-Null
- the pipe symbol|
means continue processing the previous result andOut-Null
means hide the result or output.
Use case
-
To speed up the backup of files. Robocopy is fast in synchronizing files between the source and destination directory by sending the delta (i.e., the changes). This works best if you are synchronizing the files between two servers (except for the first time synchronizing).
-
You may use any other external program to synchronize the files. What you need is to replace the
robocopy
command with the external program command.
Jump to #POWERSHELL blog
Author
Lau Hon Wan, software developer.