用Powershell对文件批量重命名

来源:互联网 发布:tcp ip网络编程 代码 编辑:程序博客网 时间:2024/06/05 02:47

重命名示例

需求:將D盤For PS文件夾下的A.txt文件重命名爲aa.txt

rename-Item 'D:\For PS\A.txt' -NewName 'aa.txt'

批量改文件擴展名

需求:將D盤For PS文件夾下的所有的txt文件改爲html文件,即.txt改爲.html

get-childItem 'D:\For PS' *.txt | rename-item -newname { $_.name -replace '\.txt','.html' }

備註:由於replace的模式匹配字符串參數支持正則表達式,'.txt'要轉義成'\.txt'。

批量爲文件加前綴

需求:將D盤For PS文件夾下的所有的txt文件加上一個“Test_”的前綴

cd 'D:\For PS'get-childItem  -r *.txt | rename-Item -newname{'Test_'+$_.name}

如果覺得上面的命令太精簡,看不太懂,可以用如下語句,更好理解些:

$dir = dir D:\ForPS *.txtforeach($_ in $dir){rename-Item $_.FullName -NewName ('Test_'+$_.Name)}

最後 一例

將D盤For PS文件夾下的所有的txt文件重命名為 Note1.txt、Note2.txt這樣的形式

get-childItem  'D:\For PS' -r  *.txt | foreach-Object -Begin {$count = 1}  -Process{ rename-Item $_.fullname -NewName "Note$count.txt";$count++}  


用PowerShell以編程的思想去操作文件,還可以實現更多更複雜的需求。

0 0
原创粉丝点击