在shell中,我如何递归地替换目录内容中的文件名和目录名,同时保留现有的大小写?例如,子目录可以命名为"Test"或"Test",我希望能够将其分别替换为"Testa"或"Testa"。
注意事项-小心运行此操作,因为您可能最终会重命名许多文件,然后意识到您在文件中添加了错误的字符。这里没有撤销按钮;)
好吧,如果我没理解错的话,这个就可以了。
包含几个.txt文件的文件夹结构为
C:Folderfirstfolder
C:FolderfourthFolder
C:FolderSecondFolder
C:FolderThirdfolder
C:FolderfirstfolderaFile.txt
C:FolderSecondFolderAnotherfile.txt
C:FolderThirdfolderyetAnotherFile.txt
脚本如下:我不得不把文件夹和文件分开,因为文件有扩展名,你可能不想在扩展名后添加你的重命名字符串。
$source="C:Folder"
$addtoname="A"
Get-ChildItem $source -Recurse | foreach {
$location = Split-Path $_.FullName
if($_.PSIsContainer){
$name=$_.Name
Rename-Item $location""$name $location""$name$addtoname
}
else
{
$name = $_.BaseName
$ext = $_.Extension
Rename-Item $location""$nameext $location""$name$addtoname$ext
}
}
之后的结果是
C:FolderfirstfolderA
C:FolderfourthFolderA
C:FolderSecondFolderA
C:FolderThirdfolderA
C:FolderfirstfolderAaFileA.txt
C:FolderSecondFolderAAnotherfileA.txt
C:FolderThirdfolderAyetAnotherFileA.txt
我希望这是你想要的,我没有把我的时间花在这上面。