用于将文件名转换为小写的批处理文件

batch-file for converting file names into lowercase

我需要一个批处理文件来将文件夹及其子文件夹中的所有文件转换为小写。例如:

Here Is Whosebug.txt

here is Whosebug.txt

括号里是一段文件名。是否可以忽略它并将其保留在以前的状态?例如

Here Is [A WEBSITE CALLED] Whosebug.txt

here is [A WEBSITE CALLED] Whosebug.txt

使用 JREN.BAT 轻松完成 - 一个混合 JScript/batch 脚本,通过正则表达式替换重命名文件。 JREN.BAT 是纯脚本,可​​以在任何 Windows XP 以后的机器上本地运行。

简单地将所有文件名转换为小写:

jren "^" "" /l /s 

如果您希望方括号之间的所有文本均为大写,而其他所有文本均为小写,那么使用两个命令即可轻松完成

jren "^" "" /l /s
jren "[.+?]" "uc([=11=])" /j /s

如果要保留方括号内所有文本的原始大小写,并将其他所有内容转换为小写,则需要更复杂的正则表达式和替换字符串。

jren "([^[]*)(\[.*?\])*" "lc(?:'')+(?:'')" /j /s

由于 JREN 是一个批处理脚本,如果您想在另一个批处理脚本中使用该命令,则必须使用 CALL JREN

使用 jren /? 获取有关所有可用选项的帮助。

@echo off
setlocal EnableDelayedExpansion

rem Start the recursive process over the tree
call :processThisDir
goto :EOF


:processThisDir

rem Process all filenames in this folder and separate they in three parts
for /F "tokens=1-3 delims=[]" %%a in ('dir /B /A-D') do (
   set "left=%%a" & set "right=%%c"

   rem Convert left and right parts to lower case
   for %%l 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 "left=!left:%%l=%%l!"
      set "right=!right:%%l=%%l!"
   )

   rem Rename this file
   ren "%%a[%%b]%%c" "!left![%%b]!right!"
)

rem Recursively process the folders in this folder
for /D %%a in (*) do (
   cd "%%a"
   call :processThisDir
   cd ..
)

exit /B

只需在答案中添加另一个选项,这会将文件夹及其子文件夹中的所有文件重命名为名称的小写版本。

来自命令行: 注意,确保 CD 到正确的目录,在 运行 之前:

@for /f "delims=" %i in ('dir /b/l/a-d') do ren "%~fi" "%~i"

或批处理文件:

@echo off
for /f "delims=" %%i in ('dir /b/l/a-d') do echo ren "%%~fi" "%%~i"