问题描述
使用Powershell,我想使用文件所在的文件夹的名称来重命名文件夹中的文件.因此,在我的C:\ temp目录中,有3个文件夹,分别为'aaa','bbb'和'ccc'.在这些文件夹中的每个文件夹中,都有3个文件,分别称为doc1.txt,doc2.txt和doc3.txt.我想将所有9个.txt文件重命名为folderName + fileName,以便将它们重命名为以下内容:
Using Powershell, I want to rename files in folders by using the name of the folder that the files are in. So in my C:\temp directory, there are 3 folders called 'aaa', 'bbb' and 'ccc'. In each of these folders, there are 3 files called doc1.txt, doc2.txt and doc3.txt. I would like to rename all 9 .txt files to folderName+fileName, so they would be renamed to the following:
aaadoc1.txt
aaadoc2.txt
aaadoc3.txt
bbbdoc1.txt
bbbdoc2.txt
bbbdoc3.txt
cccdoc1.txt
cccdoc2.txt
cccdoc3.txt
请问有人可以向我指出正确的方法吗?
Please could anyone point me in the right direction about how to approach this?
推荐答案
这将重命名文件,并在文件夹名和文件名之间添加下划线('_'):
This will rename the files and put an underscore ('_') between the folder name and the file name:
Get-ChildItem C:\temp -Filter *.txt -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}
这篇关于使用文件夹名称重命名Powershell中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!