标题: [文件操作] 批处理如何去除/删除文件名中的中文/汉字 [打印本页]
作者: 674316 时间: 2017-5-11 18:02 标题: 批处理如何去除/删除文件名中的中文/汉字
本帖最后由 pcl_test 于 2017-5-29 21:19 编辑
看了这个帖子: http://www.bathome.net/redirect.php?tid=41603&goto=lastpost
是处理文件夹的,请问文件应该如何处理? 需要删除文件中的空格和中文字符。
作者: pcl_test 时间: 2017-5-29 23:16
- @echo off
- powershell ^
- ^<#去掉-r则不处理子文件#^>^
- dir . -exclude '%~nx0' -r^|?{$_ -is [System.IO.FileInfo]}^|%%{^
- $tmpname=($_.BaseName -replace '[\u4e00-\u9fa5]').trim();^
- write-host $_.FullName.replace('%~dp0','.\') -nonewline;^
- if($tmpname -and ($tmpname -ne $_.BaseName)){^
- $newname=$_.DirectoryName+'\'+$tmpname+$_.Extension;^
- $n=0;^
- while(Test-Path -LiteralPath $newname){^
- $n++;^
- $newname=$_.DirectoryName+'\'+$tmpname+'_'+$n+$_.Extension;^
- };^
- mv -LiteralPath $_.FullName $newname;^
- write-host (' --^> '+($newname.split('\')[-1])) -ForegroundColor green;^
- }else{^
- write-host ' --^> 为纯汉字或不含汉字,不处理' -ForegroundColor red;^
- }^
- }
- pause
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |