本人新手,如有错误请见谅。
从我的脑海中,我想到了这一点:
foreach ($path in $hdirpath) {
$folders = Get-ChildItem $_ -Recurse | Where-Object {$_.PSIsContainer -eq $True} | Select-Object FullName
$folders | if ($_.Name -eq "Folder Name") {Rename-Item "Folder Name" + Get-Date}
}
可能有人会纠正我,但如果他们不试一试,如果您有任何问题,请告诉我。
另外 - 仅供将来参考,请确保在发布问题之前包含您尝试过的代码。
编辑
所以,我现在得到了以下内容:
$hdirpath = Get-Content C:\temp\dir.txt
$fullDate = Get-Date
$date = $($fullDate.ToShortDateString())
$day = $date -replace "/", "."
foreach ($path in $hdirpath) {
Get-ChildItem -Path $_ -Recurse | Where-Object {$_.PSIsContainer -eq $true} |
Rename-Item -NewName {if ($_.Name.StartsWith('target') -and $_.Name.EndsWith('folder')) {$_.Name + " " + $($day)}}
}
这会返回多个错误,表明它是Cannot evaluate parameter 'NewName' because its argument input did not produce any output.,但它可以工作并将文件夹名称附加到当前日期。
第 3-5 行获取日期并将其格式化为“。”而不是 ”\”。您可以对其进行格式化,但显然对您有用。
我的测试结构是“C:\temp\roc6\username1”——有多个子文件夹和多个“usernameX”。我要重命名的测试文件夹称为“targetfolder”。最终结果以“targetfolder 08.03.2017”结尾。
修订
这将是我最后一次运行脚本 - 以我的有限知识,这是我能做的最好的。
$ErrorActionPreference = "SilentlyContinue"
$hdirpath = Get-Content C:\temp\dir.txt
$fullDate = Get-Date
$day = $($fullDate.ToShortDateString()) -replace "/", "."
foreach ($path in $hdirpath) {
Get-ChildItem -Path $_ -Recurse | Where-Object {$_.PSIsContainer -eq $true} |
Rename-Item -NewName {if ($_.Name.StartsWith('target') -and $_.Name.EndsWith('folder')) {$_.Name + " " + $($day)}}
}