Home > OS >  Powershell Recursive copying and pause between each file is copied
Powershell Recursive copying and pause between each file is copied

Time:07-15

I have the following script to recursive copy data and create a log file of the destination, could any assist please, I would like to pause for 10 seconds after each file is copied so each file allocated a different created time stamp.

$Logfile ='File_detaisl.csv'
$SourcePath = Read-Host 'Enter the full path containing the files to copy'
""
""
$TargetPath = Read-Host 'Enter the destination full path to copy the files'
""
#$str1FileName = "File_Details.csv"

Copy-Item -Path $SourcePath -Destination $TargetPath -recurse -Force

Get-ChildItem -Path $TargetPath -Recurse -Force -File  | Select-Object Name,DirectoryName,Length,CreationTime,LastWriteTime,@{N='MD5 Hash';E={(Get-FileHash -Algorithm MD5 $_.FullName).Hash}},@{N='SHA-1 Hash';E={(Get-FileHash -Algorithm SHA1 $_.FullName).Hash}} | Sort-Object -Property Name,DirectoryName | Export-Csv -Path $TargetPath$Logfile

CodePudding user response:

You would lose the integrity of the folder structure, but one way to do this is using Get-ChildItem then piping to Foreach-Object, or using a loop to iterate through the items one at time.

Get-ChildItem -Path $SourcePath -Recurse -Force | 
    ForEach-Object -Process {
        Copy-Item -LiteralPath $_.FullName -Destination $TargetPath -Force
        Start-Sleep -Seconds 10
    }

The purpose is to get the files processed one after another using a loop in order to place our Start-Sleep after the file has been copied.

CodePudding user response:

Copy-Item has a -PassThru parameter that outputs each item that is currently processed. By piping to ForEach-Object you can add a delay after each file.

Copy-Item -Path $SourcePath -Destination $TargetPath -recurse -Force -PassThru | 
    Where-Object PSIsContainer -eq $false |
    ForEach-Object { Start-Sleep 10 }

The Where-Object is there to exclude folders from the ForEach-Object processing. For folder items the PSIsContainer property is $true and for files it is $false.

  • Related