如何显示将超过1天的文件从一个文件夹复制到另一个文件夹的进度?

时间:2019-07-16 13:20:56

标签: powershell

我们需要在代码中知道复制了哪些文件,有些文件是旧文件而未被复制。

$date = (get-date).AddDays(-1)
get-childitem -File c:\t\*.*,c:\f\*.*,c:\u\*.*,c:\s\*.* | where-object {$_.LastWriteTime -gt $date} | 
 Copy-Item  -Destination c:\t\1 ```

2 个答案:

答案 0 :(得分:0)

如果您使用的是PowerShell 4.0或更高版本,则可以在“拆分”模式下使用.Where({})扩展方法将新文件和旧文件分为两组:

$new,$old = @(Get-ChildItem -File C:\t\*.*).Where({$_.LastWriteTime -gt $date}, 'Split')

# Write file names to log files
$new.Name > newfiles.txt
$old.Name > oldfiles.txt

$new | Copy-Item -Destination C:\t\1\

答案 1 :(得分:0)

如果“显示进度”是指向控制台写入一些信息,那么这可能就是您想要的。

$date = (Get-Date).AddDays(-1)
$dest = 'C:\t\1'

# if the destination folder does not exist, create it first
if (!(Test-Path $dest -PathType Container)) {
    New-Item -Path $dest -ItemType Directory | Out-Null
}

Get-ChildItem -Path 'C:\t','C:\f','C:\u','C:\s' -File | ForEach-Object {
    if ($_.LastWriteTime -gt $date) {
        Write-Host "Copying file '$($_.FullName)'" -ForegroundColor Green
        $_ | Copy-Item -Destination $dest
    }
    else {
        Write-Host "File '$($_.FullName)' is too old.. Skipped" -ForegroundColor Yellow
    }
}