PowerShell: Measure File Copying Time

The scripty:

$sourcesAndDestinations=@(
"C:\Users\kimconnect\Desktop\test\test1 C:\Users\kimconnect\Desktop\test\test2",
"C:\Users\kimconnect\Desktop\test\test2 C:\Users\kimconnect\Desktop\test\test3"
)

$testPath="C:\Users\kimconnect\Desktop\test\test1"
$switchesMirrorDirectories="/MIR /SEC /W:3 /FFT "
$dateStamp = Get-Date -Format "yyyy-MM-dd-hh-mm"
$currentPath=(Get-Item -Path ".\").FullName
$log="/LOG+:$currentPath\robocopy-log-$dateStamp.txt"

function createFiles{
for ($i=0; $i -lt 100; $i++){
fsutil file createnew "$testPath\$i.txt" 2000
}
}

function startRobocopy{
$totalTime=0;
foreach ($item in $sourcesAndDestinations){
$stopWatch= [System.Diagnostics.Stopwatch]::StartNew()
try{
invoke-expression "robocopy $item $switches $log";
}
catch{
continue;
}
$elapsedSeconds=$stopWatch.Elapsed.TotalSeconds;
"$item`: $elapsedSeconds seconds.";
$totalTime+=$elapsedSeconds;
}
"Total Time elapsed: $totalTime";
}

createFiles;
startRobocopy;

Output:

PS H:\> C:\Users\kimconnect\Desktop\unit-test.ps1
Error: The file exists.
Error: The file exists.
Error: The file exists.

Log File : H:\robocopy-log-2019-07-02-02-20.txt
C:\Users\kimconnect\Desktop\test\test1 C:\Users\kimconnect\Desktop\test\test2: 0.1134972 seconds.

Log File : H:\robocopy-log-2019-07-02-02-20.txt
C:\Users\kimconnect\Desktop\test\test2 C:\Users\kimconnect\Desktop\test\test3: 0.1132981 seconds.
Total Time elapsed: 0.2267953

Leave a Reply

Your email address will not be published. Required fields are marked *