Skip to content

Instantly share code, notes, and snippets.

@myclau
Last active November 20, 2018 11:14
Show Gist options
  • Save myclau/666b8098ce5e61353321de9cd6cf681a to your computer and use it in GitHub Desktop.
Save myclau/666b8098ce5e61353321de9cd6cf681a to your computer and use it in GitHub Desktop.
when copy files using windows ,sometimes will stuck forever, if file is corrupted expecially on sdcard, this script will help for skip those files and save which file is corrupt so if you stopped and run the copy again will not hit to the corrupt file again, but currently only support single folder will files and will skip subfolder
#Setup Source and Destination
$in="C:\test\logs"
$out = "C:\sdcard\test\logs"
$sourcedir = get-childitem -File "$in"
$destinationdir = Get-ChildItem -File "$out"
#setup error db
if ((Test-Path $out) -ne $true ){
mkdir -Force $out
}
$errordbname=$in.Replace(':\','@').Replace(' ','#').Replace('\','_')
if ((Test-Path $out\$errordbname.txt) -ne $true ){
New-Item -Force $out\$errordbname.txt
$errorList=$null
}else{
$errorList=get-content "$out\$errordbname.txt"
}
echo "start Read Diff List"
$resultFiles = New-Object System.Collections.ArrayList
foreach ($source in $sourcedir){
$find = $false
if($destinationdir.Name -notcontains $source.Name){
#if the file name is exist in file list will drop
if($errorList -notcontains $source.name){
$resultFiles.add($source.Name) > $null
}
}
}
echo "Diff list ready"
#copying base on the result list
for ($i = 0 ; $i -lt $resultFiles.Count ; $i++){
$filename=$resultFiles[$i]
$remain=$resultFiles.Count-$i-1
$total=$resultFiles.Count
echo $filename
robocopy "$in" "$out" $filename /R:0 /W:0
# if file cannot copy will store in db
if($? -eq $false){
Add-Content "$out\$errordbname.txt" "$filename`n"
}
echo $(Get-Date) $remain/$total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment