我有一个56个文件夹的文本文件列表,其中包括一组文件夹的完整路径。我需要在路径末尾重命名文件夹。 示例:
Original: \this folder\needs to\move
New: \this folder\needs to\move.moved
我对Powershell完全陌生并尝试学习。我认为这可能是一个很好的开始。任何帮助将不胜感激。
答案 0 :(得分:0)
欢迎来到@Bob Moore。希望这对您有帮助,@我在评论中有任何疑问
# Get the content of the list
# in this case, a text file with no heading, and one path per line
$listContent = Get-Content $ENV:USERPROFILE\Desktop\list.txt
# Loop over each child folder and rename it
foreach($line in $listContent)
{
# check if the current path is valid
$pathTest = Test-Path -Path $line
if($pathTest -eq $True)
{
Write-Output "`nOld path: $($line)"
$newName = $line + ".moved"
Write-Output "New name: $newName"
try
{
# on success, write out message
Rename-Item -Path $line -NewName $newName -Force
# split the string from the file and get the data after the last \ for readability
Write-Output "`nSuccessfully changed directory name $($line.split('\')[-1]) to $newName"
}
catch
{
# on error, throw first error in the Error array
throw $Error[0]
}
}
else {
Write-Output "$($line) is not a valid path"
}
}
Write-Output "`nEnd of script!"