我如何将其压缩成一个好的 for 循环?

How do I condense this into a good for loop?

基本上,如何将其变成高效的 for 循环?我需要在文件夹的文件名中插入一个 space ,最好是子文件夹;在任何数字和它后面的任何字母之间,而不是在它之前,将文件名中的其他所有内容单独保留,包括可见的文件扩展名。这个数字并不总是在同一个标​​记上。这将从 A-Z 开始。

:trim-r
@echo off
setlocal enabledelayedexpansion
CD %workdir%
PUSHD "%workdir%"
for /r %workdir% %%j in (*.*) do (
set filename=%%~nj
set filename=!filename:0a=0 a!
set filename=!filename:1a=1 a!
set filename=!filename:2a=2 a!
set filename=!filename:3a=3 a!
set filename=!filename:4a=4 a!
set filename=!filename:5a=5 a!
set filename=!filename:6a=6 a!
set filename=!filename:7a=7 a!
set filename=!filename:8a=8 a!
set filename=!filename:9a=9 a!
set filename=!filename:0b=0 b!
set filename=!filename:1b=1 b!
set filename=!filename:2b=2 b!
set filename=!filename:3b=3 b!
set filename=!filename:4b=4 b!
set filename=!filename:5b=5 b!
set filename=!filename:6b=6 b!
set filename=!filename:7b=7 b!
set filename=!filename:8b=8 b!
set filename=!filename:9b=9 b!
set filename=!filename:0c=0 c!
set filename=!filename:1c=1 c!
set filename=!filename:2c=2 c!
set filename=!filename:3c=3 c!
set filename=!filename:4c=4 c!
set filename=!filename:5c=5 c!
set filename=!filename:6c=6 c!
set filename=!filename:7c=7 c!
set filename=!filename:8c=8 c!
set filename=!filename:9c=9 c!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
)
endlocal
popd
EXIT /B

您的代码与您的描述不符(为什么 "h" 更改为 "a" 并且 "w" 更改为 "c"?)并且您没有发布任何示例.无论如何,下面的代码将任何 <digit><letter> 更改为 <digit><space><letter>.

for /R %workdir% %%j in (*.*) do (
   set "filename=%%~nj"
   for /L %%n in (0,1,9) do (
      for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
         set "filename=!filename:%%n%%a=%%n %%a!"
      )
   )
   if "!filename!" neq "%%~nj" ren "%%j" "!filename!%%~xj"
)

我的 jrepl.bat regular expression renaming utility 使这项任务变得非常简单和高效。它是纯脚本(混合 JScript/batch),可以在任何 Windows XP 以后的机器上本地运行。

假设 JREPL.BAT 在您的当前目录中,或者更好的是,在您的 PATH 中的某个位置:

call jren "([a-zA-Z])([0-9])" " " /s /p "%workdir%"