我正在尝试编写一个脚本,该脚本以源路径作为输入并复制自给定日期以来已更改的所有文件,包括其目录结构:
param([string]$source,[string]$datum)
(Get-ChildItem $source -Recurse | Where-Object { $_.LastWriteTime -ge $datum }) | Copy-Item -Destination C:\tmp -Recurse
有效。问题在于,它会复制文件(包括其目录结构),但还会将所有文件从源目录复制到目标文件夹的基本路径。
错误在哪里?
答案 0 :(得分:0)
这是一个过于复杂的解决方案。 5次重试会产生很多错误处理。您应该能够将其应用于您的需求。
我会看着Line 21
添加您的Where-Object
并进行测试。
示例用法:复制文件-源“ C:\ Path \”-目标“ C:\ Path2 \”
Function Retry-Command {
$Script:Counter = $Script:Counter + 1
Write-Host "Attempt #" $Script:Counter "out of 5"
if ($Script:Counter -ge 5) {
PAUSE
}
else {
Start-Sleep -Seconds 5 -Verbose
}
}
Function Copy-Files ([string]$Source, [string]$Destination ) {
[System.Collections.ArrayList]$Folder_Content = @()
[string]$Folder_Name = Split-Path $Source -Leaf
[string]$Folder_Path = ($Source -replace [regex]::Escape($Source), ($Destination + $Folder_Name + "\"))
[int]$Script:Counter = 0
while (($Folder_Content.Count -eq "0") -and ($Counter -ne 5)) {
try {
Write-Host ""
Write-Host "------------------------------" -ForegroundColor Cyan
Write-Host "Getting Content of" $Folder_Name -ForegroundColor Yellow
[System.Collections.ArrayList]$Folder_Content = @(Get-ChildItem -Path $Source -Recurse -Force -ErrorVariable Child_Error)
if ((-Not $Child_Error) -and ($Folder_Content.Count -ne 0) ) {
Write-Host "Finished Getting Content of" $Folder_Name -ForegroundColor Green
[int]$Script:Counter = 0
}
else {
Write-Host "Failed to get Content of" $Folder_Name -ForegroundColor Red
Retry-Command
}
}
catch {
Write-Host "Unexpected Error getting Content of" $Folder_Name -ForegroundColor Red
Retry-Command
}
}
if (-Not $Folder_Content.Count -eq 0) {
while ((Test-Path -Path $Folder_Path) -eq $false) {
try {
Write-Host ""
Write-Host "Creating" $Folder_Name "Folder" -ForegroundColor Yellow
Copy-Item -Path $Source -Destination $Folder_Path -Force -ErrorVariable Folder_Error
if (-Not $Folder_Error) {
Write-Host "Finished Creating" $Folder_Name "Folder" -ForegroundColor Green
[int]$Script:Counter = 0
}
else {
Write-Host "Failed to create" $Folder_Name "Folder" -ForegroundColor Red
Retry-Command
}
}
catch {
Write-Host "Unexpected Error Creating" $Folder_Name "Folder" -ForegroundColor Red
Retry-Command
}
}
foreach ($Index in $Folder_Content) {
[string]$Software_File_Name = Split-Path $Index.FullName -Leaf
[string]$Software_File_Path = ($Index.FullName -replace [regex]::Escape($Source), ($Destination + (Split-Path $Source -Leaf) + "\"))
while ((Test-Path -Path $Software_File_Path) -eq $false) {
try {
Write-Host ""
Write-Host "Copying" $Index.FullName -ForegroundColor Yellow
Copy-Item -Path $Index.FullName -Destination $Software_File_Path -Force -ErrorVariable File_Error
if (-Not $File_Error) {
Write-Host "Finished Copying" $Index.FullName -ForegroundColor Green
[int]$Script:Counter = 0
}
else {
Write-Host "Failed to Copy" $Index.FullName -ForegroundColor Red
Retry-Command
}
}
catch {
Write-Host "Unexpected Error Copying" $Index.FullName -ForegroundColor Red
Retry-Command
}
}
}
}
}
答案 1 :(得分:0)
首先,您应该使用robocopy
这样的东西。它具有可能处理您查询的开关:
/ e复制子目录。请注意,此选项包括空目录。有关更多信息,请参见“备注”。
/ maxage:指定文件的最长使用期限(排除早于N天或日期的文件)。
/ l指定仅列出文件(而不复制,删除或添加时间戳)。
因此,知道我将开始尝试robocopy $source $destination /e /maxage:30 /l
,它将显示目录树中最近30天内修改的所有文件。 /maxage
可能不是您要寻找的开关,但这是一个合理的猜测。
您当前的逻辑问题很可能Get-ChildItem $source -Recurse
将同时返回文件夹和文件。因此,您可能要将文件夹传递到Copy-Item
,这是所有其他文件的来源。您应该可以使用-File
开关来缓解这种情况。
Get-ChildItem $source -Recurse -File