powershell - Powershell 基于 x 天前移动文件和文件夹
问题描述
我是 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
解决方案
问题
Copy-Item -Path $Sourcepath -Destination $DestinationPath -Recurse -Force
您始终为源和目标指定相同的路径。使用参数-recurse
,您将为每个匹配文件复制整个目录$SourcePath
。
解决方案
您需要Copy-Item
使用$_
(aka $PSItem
) 变量来提供先前管道步骤的输出,基本上Copy-Item
是在单项模式下使用。
试试这个(GetRelativePath
方法需要 .NET >= 5.0):
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 < 5.0):
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 在“必要”时自动将字符串转换为数字的能力。但这并不总是有效,因此最好首先创建一个具有适当数据类型的变量。
推荐阅读
- c# - 如何同时模拟 Directory.getFiles 确保生产代码也运行
- python-3.x - 这行正则表达式在python中是什么意思?: (r'^(?:.{4}\-){3}.{4}$')
- com - 团队客户端的 COM 对象 - Microsoft.Office.Uc
- database - Larravel 项目没有数据库的迁移脚本。`Illuminate\Foundation\Testing\RefreshDatabase` 是否处理该问题以及如何处理?
- android - 在bash中过滤adb logcat时如何在标记名称中应用正则表达式
- google-compute-engine - 无法使用命令 'gcloud logging logs write' 在日志中设置资源类型。在全局资源类型下创建条目
- azure-devops - 为托管代理指定 vmImage
- git - 如何在不影响历史记录的情况下删除旧提交
- javascript - 使用 HTML 和 JS 运行外部应用程序(vlc 播放器、文本文档、.exe)
- reactjs - 如何在我的登录页面上传递我的错误消息