【问题标题】:PowerShell Recursive copying and pause between each file is copiedPowerShell递归复制和复制每个文件之间的暂停
【发布时间】:2022-10-17 07:45:37
【问题描述】:

我有以下脚本来递归复制数据并创建目标的日志文件,请提供帮助,我想在复制每个文件后暂停 10 秒,以便每个文件分配一个不同的创建时间戳。

$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

【问题讨论】:

    标签: powershell copy-item


    【解决方案1】:

    Copy-Item 有一个 -PassThru 参数,用于输出当前处理的每个项目。通过管道到ForEach-Object,您可以在每个文件之后添加延迟。

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

    Where-Object 用于从ForEach-Object 处理中排除文件夹。对于文件夹项目,PSIsContainer 属性是 $true,对于文件,它是 $false

    【讨论】:

    • 谢谢,子文件夹中包含的文件似乎具有匹配的创建时间戳,是否所有文件和文件夹都具有不同的时间戳?
    • @PF6004 我无法重现。您之前是否清除了输出文件夹?顺便说一句,如果您也想要文件夹,请删除 Where-Object 行,因此它也会在创建文件夹后等待。
    【解决方案2】:

    您将失去文件夹结构的完整性,但一种方法是使用Get-ChildItem,然后通过管道连接到Foreach-Object,或者使用循环一次遍历一个项目。

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

    目的是使用循环一个接一个地处理文件,以便在文件复制后放置我们的Start-Sleep

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      相关资源
      最近更新 更多