相对于Powershell中的当前位置移动项目
我需要遍历一个文件夹(ParentLogFolder),查找日志并将它们从相对文件夹(SUBLOG)移动到子文件夹(CompressedLogFolder) 我不知道如何将父文件夹的文件放入变量中,并将子文件夹添加为相对于Powershell中的当前位置移动项目,powershell,relative-path,Powershell,Relative Path,我需要遍历一个文件夹(ParentLogFolder),查找日志并将它们从相对文件夹(SUBLOG)移动到子文件夹(CompressedLogFolder) 我不知道如何将父文件夹的文件放入变量中,并将子文件夹添加为Move Item部分中的目标 有谁能帮忙回答这个问题 不要介意代码的$Date部分,它是有效的,它就在那里,因为它需要在解决方案中 >代替OnLink,考虑代码清晰性。也就是说,通过分而治之的方法在子步骤中打破该过程。当管道没有被过多使用时,检查变量是否有不正确的值更容易。这样,
Move Item
部分中的目标
有谁能帮忙回答这个问题
不要介意代码的
$Date
部分,它是有效的,它就在那里,因为它需要在解决方案中 >代替OnLink,考虑代码清晰性。也就是说,通过分而治之的方法在子步骤中打破该过程。当管道没有被过多使用时,检查变量是否有不正确的值更容易。这样,
$allFiles = Get-ChildItem -Filter $Pattern -path $path -recurse
$oldFiles = $allFiles | ? { $_.LastWriteTime -lt $ageLimit }
foreach($file in $oldFiles) {
$archiveFolder = join-path $file.DirectoryName 'someArchiveFolder'
$destination = join-path $archiveFolder $file.Name
move-item -whatif $file.FullName $destination
}
-whatif
开关将打印move命令将执行的操作。删除它以实际移动文件。非常感谢您提供的代码和提示。我对“复杂”的powershell还不太熟悉,只是想不出正确的公式。
$Path = "C:\ParentLogFolder"
$Pattern = "*.log"
$date = get-date
$AgeLimit = $date.AddDays(-31)
Get-ChildItem -Filter $Pattern -path $path -recurse | Where-Object
{$_.LastWriteTime -lt $AgeLimit} | Move-Item -Path
$allFiles = Get-ChildItem -Filter $Pattern -path $path -recurse
$oldFiles = $allFiles | ? { $_.LastWriteTime -lt $ageLimit }
foreach($file in $oldFiles) {
$archiveFolder = join-path $file.DirectoryName 'someArchiveFolder'
$destination = join-path $archiveFolder $file.Name
move-item -whatif $file.FullName $destination
}