如何将其浓缩为良好的循环?

时间:2015-03-29 19:49:47

标签: batch-file for-loop rename

基本上,你如何将其变成一个有效的for循环?我需要在文件夹中插入一个空格,最好是子文件夹;在它之后的任何数字和任何字母之间,而不是它之前,只留下文件名中的所有其他内容,包括可见的文件扩展名。该数字并不总是在同一个标​​记上。这将来自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

2 个答案:

答案 0 :(得分:2)

您的代码与您的描述不符(为什么“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"
)

答案 1 :(得分:1)

我的jrepl.bat regular expression renaming utility使这项任务非常简单有效。它是纯脚本(混合JScript /批处理),可以在任何Windows机器上从XP开始本地运行。

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

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