我是 powershell 新手,正在尝试学习从一个目录到另一个目录的基本文件移动。我的目标是将超过 18 个月的文件和文件夹移动到作为计划任务运行的冷存储文件夹。我需要能够轻松修改它的目录以满足我们的需求。它需要保留文件夹结构并仅移动符合上述参数的文件。我还需要它记录它所做的一切,这样如果有什么问题我知道在哪里。
如果我运行它,它只会复制所有内容。如果我注释掉 %{Copy-Item... 那么它仅根据我的参数运行和列出并记录它。我哪里出错了或者我离基地太远了?
是的,使用 robocopy 来做到这一点很容易,但我想使用 powershell 并从中学习。
#Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
#Clear-Host
#Days older than
$Days = "-485"
#Path Variables
$Sourcepath = "C:\Temp1"
$DestinationPath = "C:\Temp2"
#Logging
$Logfile = "c:\temp3\file_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).log"
#transcript logs all outputs to txt file
Start-Transcript -Path $Logfile -Append
Get-ChildItem $Sourcepath -Force -Recurse |
Where-Object {$_.LastwriteTime -le (Get-Date).AddDays($Days)} |
% {Copy-Item -Path $Sourcepath -Destination $DestinationPath -Recurse -Force}
Stop-Transcript
Problem
Copy-Item -Path $Sourcepath -Destination $DestinationPath -Recurse -Force
您始终为源和目标指定相同的路径。带参数-recurse
您将复制整个目录$SourcePath
对于每个匹配的文件。
Solution
您需要将先前管道步骤的输出提供给Copy-Item
通过使用$_
(aka $PSItem
) 变量,基本上使用Copy-Item
在单项模式下。
试试这个(需要 .NET >= 5.0GetRelativePath
方法):
Get-ChildItem $Sourcepath -File -Force -Recurse |
Where-Object {$_.LastwriteTime -le (Get-Date).AddDays($Days)} |
ForEach-Object {
$relativeSourceFilePath = [IO.Path]::GetRelativePath( $sourcePath, $_.Fullname )
$destinationFilePath = Join-Path $destinationPath $relativeSourceFilePath
$destinationSubDirPath = Split-Path $destinationFilePath -Parent
# Need to create sub directory when using Copy-Item in single-item mode
$null = New-Item $destinationSubDirPath -ItemType Directory -Force
# Copy one file
Copy-Item -Path $_ -Destination $destinationFilePath -Force
}
替代实施无需GetRelativePath
(对于 .NET
Push-Location $Sourcepath # Base path to use for Get-ChildItem and Resolve-Path
try {
Get-ChildItem . -File -Force -Recurse |
Where-Object {$_.LastwriteTime -le (Get-Date).AddDays($Days)} |
ForEach-Object {
$relativeSourceFilePath = Resolve-Path $_.Fullname -Relative
$destinationFilePath = Join-Path $destinationPath $relativeSourceFilePath
$destinationSubDirPath = Split-Path $destinationFilePath -Parent
# Need to create sub directory when using Copy-Item in single-item mode
$null = New-Item $destinationSubDirPath -ItemType Directory -Force
# Copy one file
Copy-Item -Path $_ -Destination $destinationFilePath -Force
}
}
finally {
Pop-Location # restore previous location
}
顺便说一句,$Days = "-485"
应替换为$Days = -485
。
您当前创建一个字符串而不是数字,并依靠 Powershell 在“必要”时自动将字符串转换为数字的能力。但这并不总是有效,因此最好首先创建一个具有适当数据类型的变量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)