Board logo

标题: [文本处理] [已解决]批处理怎么让文件名指定符号中间的字母不作改动,其余字母全部大写 [打印本页]

作者: ChenCheChe    时间: 2020-4-3 21:01     标题: [已解决]批处理怎么让文件名指定符号中间的字母不作改动,其余字母全部大写

我想让文件夹,包括子文件夹里面的文件的名称全部改成大写,
但是在这个括号 [abc] 内的字母不作改动。

比如把文件名[abc]efg123改成[abc]EFG123
子文件夹下的文件也是这个格式修改。
这个应该怎么写?
作者: went    时间: 2020-4-6 23:57

  1. @echo off
  2. set "U_L=a:A b:B c:C d:D e:E f:F g:G h:H i:I j:J k:K l:L m:M n:N o:O p:P q:Q r:R s:S t:T u:U v:V w:W x:X y:Y z:Z"
  3. for /f "delims=" %%i in ('dir /s /b') do (
  4. echo %%i
  5. call :toUpper "%%~ni"
  6. call echo RENAME TO: %%newStr%%%%~xi
  7. call rename "%%~i" "%%newStr%%%%~xi"
  8. echo ----------------
  9. )
  10. pause&exit
  11. :toUpper
  12. set "s=%~1"
  13. set "tmpStr=%s%" & set "newStr=" & set "up=1"
  14. for %%i in (%U_L%) do for /f "tokens=1,2 delims=:" %%a in ("%%i") do call set "tmpStr=%%tmpStr:%%a=%%b%%"
  15. :loop
  16. if "%s:~0,1%"=="[" set "up=0"
  17. if "%s:~0,1%"=="]" set "up=1"
  18. if "%up%"=="0" ( set "newStr=%newStr%%s:~0,1%"  ) else ( set "newStr=%newStr%%tmpStr:~0,1%"  )
  19. set "s=%s:~1%" & set "tmpStr=%tmpStr:~1%"
  20. if not "%s%"=="" goto :loop
复制代码

作者: WHY    时间: 2020-4-8 00:01

  1. $str = '[abc]d[ef]g123';
  2. $reg = '(?<=\[[^[\]]*\]|^)[^[\]]+';
  3. [regex]::Replace($str, $reg, {param($m), $m.Groups[0].Value.ToUpper()});
复制代码
结果:[abc]D[ef]G123

如果需要考虑括弧嵌套的问题:
  1. $str = '[abc[w8w]]de[fg]xy123';
  2. $reg = '\[(?:[^[\]]*|\[(?<open>)|\](?<-open>))*(?(open)(?!))\]|[^[\]]+';
  3. [regex]::Replace($str, $reg, {param($m), $s = $m.Groups[0].Value; if($s.IndexOf('[')+1){$s}else{$s.ToUpper()}});
复制代码
结果:[abc[w8w]]DE[fg]XY123
作者: ChenCheChe    时间: 2020-4-18 19:29

回复 2# went


    谢谢
作者: ChenCheChe    时间: 2020-4-18 19:32

回复 3# WHY


    感谢




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2