(PowerShell) 重命名文件

Get-ChildItem -Path C:\temp\test -Filter *.txt | 
 Rename-Item -NewName {$_.Basename.Replace("Old","New") + $_.extension} -WhatIf -Verbose

 

如上  将指定目录下的所有的文件名从 old 改成 new.. 

adding -WhatIf at the end of the command we are saying to PowerShell: 'Just test, don't actually make any permanent changes'.

 -Recurse 查子文件夹。

 

重命名示例

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

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

 

批量改文件擴展名

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

 

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

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

批量爲文件加前綴

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

 

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

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

  1. $dir = dir D:\ForPS *.txt  
  2. foreach($_ in $dir)  
  3. {  
  4.         rename-Item $_.FullName -NewName ('Test_'+$_.Name)  
  5. }  

 

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

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

 

 

 

 

转载于:https://www.cnblogs.com/fdyang/p/9075884.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值