(PowerShell) 重命名文件
2024-09-25 12:35:41
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
- 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 *.txt
- foreach($_ 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++}
最新文章
- javaWeb项目部署到阿里云服务器步骤
- CefSharp .net
- PHP创建数据库数据表
- Mysql基础2
- 'dict' object has no attribute 'a'
- AppCan4.0:开发者要做有价值的APP
- 批处理测试局域网网络连通性ping1-255
- jQuery循环给某个ID赋值
- Regularized Linear Regression with scikit-learn
- C++Primer charpter1.
- 主从mysql 同步设置
- API设计中响应数据格式用json的优点
- CSS(Cascading Style Sheet)简述
- redis最大缓存和回收策略
- eclipse项目版本控制忽略上传文件
- 2017/2/24:Maven的pom jar war的区别
- PyQt5--Signal&;Slot
- Hibernate中得fetch
- codesmith连接postgresql修复bug
- ANDROID – TOOLBAR 上的 NAVIGATION DRAWER(转)
热门文章
- 4、TensorFlow基础(二)常用API与变量作用域
- 【算法笔记】B1052 卖个萌
- hiho#1445 重复旋律5 求子串数量 后缀自动机
- 关于MatlabGUI清除WorkSpace的用法
- [Xamarin] 產生專案的AndroidManifest.xml (转帖)
- Javac的命令(注解相关)
- js中请求数据的$post和$ajax区别(同步和异步问题)
- java1.8中ConcurrentHashMap
- vim操作命令备忘
- ASP.NET MVC Core的ViewComponent