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++}

最新文章

  1. javaWeb项目部署到阿里云服务器步骤
  2. CefSharp .net
  3. PHP创建数据库数据表
  4. Mysql基础2
  5. 'dict' object has no attribute 'a'
  6. AppCan4.0:开发者要做有价值的APP
  7. 批处理测试局域网网络连通性ping1-255
  8. jQuery循环给某个ID赋值
  9. Regularized Linear Regression with scikit-learn
  10. C++Primer charpter1.
  11. 主从mysql 同步设置
  12. API设计中响应数据格式用json的优点
  13. CSS(Cascading Style Sheet)简述
  14. redis最大缓存和回收策略
  15. eclipse项目版本控制忽略上传文件
  16. 2017/2/24:Maven的pom jar war的区别
  17. PyQt5--Signal&Slot
  18. Hibernate中得fetch
  19. codesmith连接postgresql修复bug
  20. ANDROID – TOOLBAR 上的 NAVIGATION DRAWER(转)

热门文章

  1. 4、TensorFlow基础(二)常用API与变量作用域
  2. 【算法笔记】B1052 卖个萌
  3. hiho#1445 重复旋律5 求子串数量 后缀自动机
  4. 关于MatlabGUI清除WorkSpace的用法
  5. [Xamarin] 產生專案的AndroidManifest.xml (转帖)
  6. Javac的命令(注解相关)
  7. js中请求数据的$post和$ajax区别(同步和异步问题)
  8. java1.8中ConcurrentHashMap
  9. vim操作命令备忘
  10. ASP.NET MVC Core的ViewComponent