(建议用电脑浏览本文效果更好)

自己编写的一个DOS批处理,用于批量修改文件名或扩展名。

实现逻辑

批处理如何逐行提取文件内容(电脑里批量修改文件名)(1)

操作:批处理运行后会在当前文件夹下生成一个错误信息文件batResult.txt

字符替换:

示例一:将文件名中的“5”替换成“99”

批处理如何逐行提取文件内容(电脑里批量修改文件名)(2)

原文件

运行批处理文件batRename2.bat:

批处理如何逐行提取文件内容(电脑里批量修改文件名)(3)

结果:

批处理如何逐行提取文件内容(电脑里批量修改文件名)(4)

示例二:将“废柴兄弟”改成“上海滩”,并将扩展名改为“bat”

批处理如何逐行提取文件内容(电脑里批量修改文件名)(5)

操作:

批处理如何逐行提取文件内容(电脑里批量修改文件名)(6)

结果:

批处理如何逐行提取文件内容(电脑里批量修改文件名)(7)

改成新文件名:将所有扩展名为bat文件改成新文件名newBAT,后缀改为ttt

批处理如何逐行提取文件内容(电脑里批量修改文件名)(8)

操作:

批处理如何逐行提取文件内容(电脑里批量修改文件名)(9)

结果:

批处理如何逐行提取文件内容(电脑里批量修改文件名)(10)

批处理如何逐行提取文件内容(电脑里批量修改文件名)(11)

程序实现方法一

@echo off rem 切换为utf-8编码 ::Chcp 65001 ::切换中文 Chcp 936 > nul rem 启用延缓环境变量扩展 setlocal EnableDelayedExpansion echo **可修改当前文件夹下所有的文件名或字符及扩展名** echo *****将此批处理文件拷入需要修改的文件夹中****** set option=9 set cnt=0 set skipChar=/\*?:"<>|^! echo ** 请选择操作... ** echo ** 1 替换部分字符或扩展名 ** echo ** 0 换成全新文件名 ** :selectOption set /p option= 》》请选择操作选项: if %option%==1 ( goto endSelectOption ) else if %option%==0 (goto endSelectOption) set /a cnt =1 if %cnt% GTR 2 (goto end) else (goto selectOption) :endSelectOption echo ------要修改的原文件信息,回车结束输入------- set retVal=0 :inputOfi set oldFile= set retVal=0 if %option%==1 ( set /p oldFile="》》请输入要替换的文件名/文字(必输入):" ) else (set /p oldFile="》》请输入要修改的文件名/文字(不输入则包括当前文件夹下所有文件): ") if %option%==1 (if "%oldFile%"=="" echo 被替换字符不能为空!& goto inputOfi ) else (if "%oldFile%"=="*" goto inputOfe if "%oldFile%"=="" ( set oldFile=* goto inputOfe)) @echo !oldFile! | findstr "[!skipChar!]" > nul if "!errorlevel!"=="0" (echo !!输入的文件名/字符"!oldFile!"错误!! rem 如果用%skipChar%则出现“此时不应有 >”的错误提示。 echo !!字符中不能包含"!skipChar!"中的字符 !! goto inputOfi) :inputOfe set oldFExt= set retVal=0 set /p oldFExt= 》》请输入原文件扩展名(不输入则包括所有扩展名): if "%oldFExt%"=="*" goto endInputOfe if "%oldFExt%"=="" (set oldFExt=* ) else (@echo !oldFExt!| findstr "[^0-9A-Za-z]" > nul if "!errorlevel!"=="0" ( echo !!扩展名"%oldFExt%"错误!! echo !!请输入0-9,A-Z,a-z之间的字符!! goto inputOfe)) :endInputOfe echo === 查找范围:"*!oldFile!*.!oldFExt!" === echo -----新的文件信息,回车结束输入------ :inputNfi set newFile= set retVal=0 set /p newFile="》》请输入新的文件名/字符(必输入):" if not "%newFile%"=="" ( @echo !newFile! | findstr "[!skipChar!]" > nul if "!errorlevel!"=="0" ( echo !!输入的文件名/字符"%newFile%"错误!! echo !!字符中不能包含"!skipChar!"中的字符 !! goto inputNfi) ) else (echo 新的文件名/字符不能为空!& goto inputNfi) :inputNfe set newFExt= set retVal=0 if %option%==1 (if "!oldFExt!"=="*" (goto endInputNfe )) set /p newFExt="》》输入新的文件扩展名(不输入则保留原扩展名):") if "!newFExt!" neq "" ( @echo !newFExt!| findstr "[^0-9A-Za-z]" > nul if "!errorlevel!"=="0" ( echo !! 扩展名"%newFExt%"错误 !! echo !!请输入0-9,A-Z,a-z之间的字符!! goto inputNfe) ) :endInputNfe echo === 查找范围: "*!oldFile!*.!oldFExt!" === if %option%==1 (if "%newFExt%" neq "" (echo === 替换为: "*!newFile!*.!newFExt!" === ) else (echo === 替换为: "*!newFile!*.原扩展名" === ) ) else (if "%newFExt%"=="" (echo === 新的内容为:"!newFile!000N.原扩展名" === ) else (echo === 新的内容为:"!newFile!000N.!newFExt!" ===)) rem --------------------处--理------------------------- set ttl=0 set a=1 set aStr=0001 set /p confirm=请确认是否修改?(输入:Y-是,N-否) if "%confirm%" neq "Y" (if "%confirm%" neq "y" goto end) rem set oft="%oft%.%oldFExt%" for /f "delims=" %%i in ( 'dir /b /a-d "*%oldFile%*.%oldFExt%" ' ) do ( if "%oldFile%"=="*" (@echo %%~ni | findstr /i ".%oldFile%" > nul ) else (@echo %%~ni | findstr /iL /c:"%oldFile%" > nul) if "!errorlevel!"=="0" ( rem 不需修改当前批处理文件名(本身) if not "%%~ni"=="%~n0" ( set /a ttl =1 ::只有文件名,如test.txt时,只有test set file=%%~ni ::只有扩展名,如test.txt时,只有.txt set ext=%%~xi if !option!==1 (set file=!file:%oldFile%=%newFile%! if "%newFExt%"=="" ( ren "%%i" "!file!!ext!" 1>> batResult.txt 2<&1 if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt ) else (set /a a =1) ) else (ren "%%i" "!file!.%newFExt%" 2>> batResult.txt if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!.%newFExt%"-- >>batResult.txt ) else (set /a a =1)) ) else (set file=%newFile%!aStr! if "%newFExt%"=="" ( ren "%%i" "!file!!ext!" 1>> batResult.txt 2<&1 if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt ) else (set /a a =1 if !a! LSS 10 (set aStr=000!a! ) else if !a! LSS 100 (set aStr=00!a! ) else if !a! LSS 1000 (set aStr=0!a!)) ) else (ren "%%i" "!file!.%newFExt%" 2>> batResult.txt if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!.%newFExt%"-- >>batResult.txt ) else (set /a a =1 if !a! LSS 10 (set aStr=000!a! ) else if !a! LSS 100 (set aStr=00!a! ) else if !a! LSS 1000 (set aStr=0!a!)) ) ) rem echo a=!a!,oldFile-^> newFile:"%%i" -^> "!file!.%newFExt%" ) ) ) :end set /a a-=1 echo 总处理文件数:!ttl! ,处理成功数:!a! endlocal pause goto :eof

程序实现方法二:与方法一区别在于判断字符的合法性方面

@echo off rem 切换为utf-8编码 ::Chcp 65001 ::切换中文 Chcp 936 > nul rem 启用延缓环境变量扩展 setlocal EnableDelayedExpansion echo **可修改当前文件夹下所有的文件名或字符及扩展名** echo *****将此批处理文件拷入需要修改的文件夹中****** set option=9 set cnt=0 set skipChar=/\*?:"<>|^! echo ** 请选择操作... ** echo ** 1 替换部分字符或扩展名 ** echo ** 0 换成全新文件名 ** :selectOption set /p option= 》》请选择操作选项: if %option%==1 ( goto endSelectOption ) else if %option%==0 (goto endSelectOption) set /a cnt =1 if %cnt% GTR 2 (goto end) else (goto selectOption) :endSelectOption echo ------要修改的原文件信息,回车结束输入------- set retVal=0 :inputOfi set oldFile= set retVal=0 if %option%==1 ( set /p oldFile="》》请输入要替换的文件名/文字(必输入):" ) else (set /p oldFile="》》请输入要修改的文件名/文字(不输入则包括当前文件夹下所有文件): ") if %option%==1 (if "%oldFile%"=="" echo 被替换字符不能为空!& goto inputOfi ) else (if "%oldFile%"=="*" goto inputOfe if "%oldFile%"=="" ( set oldFile=* goto inputOfe)) rem echo oldFile="!oldFile!",!oldFile!,"%oldFile%",%oldFile% rem call :checkFileChar "%oldFile%" %skipChar% oldFile中如有!用%会出错. call :checkFileChar "!oldFile!" %skipChar% if !retVal!==1 (echo !!输入的文件名/字符"%oldFile%"错误!! rem 如果用%skipChar%则出现“此时不应有 >”的错误提示。 echo !!字符中不能包含"!skipChar!"中的字符 !! goto inputOfi) :inputOfe set oldFExt= set retVal=0 set /p oldFExt= 》》请输入原文件扩展名(不输入则包括所有扩展名): if "%oldFExt%"=="*" goto endInputOfe if "%oldFExt%"=="" (set oldFExt=* ) else (call :checkExtChar "!oldFExt!" "O" if !retVal!==1 (echo !!扩展名"%oldFExt%"错误!! echo !!请输入0-9,A-Z,a-z之间的字符!! goto inputOfe)) :endInputOfe echo === 查找范围:"*!oldFile!*.!oldFExt!" === echo -----新的文件信息,回车结束输入------ :inputNfi set newFile= set retVal=0 set /p newFile="》》请输入新的文件名/字符(必输入):" if not "%newFile%"=="" (call:checkFileChar "!newFile!" %skipChar% if !retVal!==1 (echo !!输入的文件名/字符"%newFile%"错误!! echo !!字符中不能包含"!skipChar!"中的字符 !! goto inputNfi) ) else (echo 新的文件名/字符不能为空!& goto inputNfi) :inputNfe set newFExt= set retVal=0 if %option%==1 (if "!oldFExt!"=="*" (goto endInputNfe )) set /p newFExt="》》输入新的文件扩展名(不输入则保留原扩展名):") if "!newFExt!" neq "" (call:checkExtChar "!newFExt!" "N" if !retVal!==1 (echo !! 扩展名"%newFExt%"错误 !! echo !!请输入0-9,A-Z,a-z之间的字符!! goto inputNfe) ) :endInputNfe echo === 查找范围: "*!oldFile!*.!oldFExt!" === if %option%==1 (if "%newFExt%" neq "" (echo === 替换为: "*!newFile!*.!newFExt!" === ) else (echo === 替换为: "*!newFile!*.原扩展名" === ) ) else (if "%newFExt%"=="" (echo === 新的内容为:"!newFile!000N.原扩展名" === ) else (echo === 新的内容为:"!newFile!000N.!newFExt!" ===)) rem --------------------处--理------------------------- set ttl=0 set a=1 set aStr=0001 set /p confirm=请确认是否修改?(输入:Y-是,N-否) if "%confirm%" neq "Y" (if "%confirm%" neq "y" goto end) rem set oft="%oft%.%oldFExt%" for /f "delims=" %%i in ( 'dir /b /a-d "*%oldFile%*.%oldFExt%" ' ) do ( if "%oldFile%"=="*" (@echo %%~ni | findstr /i ".%oldFile%" > nul ) else (@echo %%~ni | findstr /iL /c:"%oldFile%" > nul) if "!errorlevel!"=="0" ( rem 不需修改当前批处理文件名(本身) if not "%%~ni"=="%~n0" ( set /a ttl =1 ::只有文件名,如test.txt时,只有test set file=%%~ni ::只有扩展名,如test.txt时,只有.txt set ext=%%~xi if !option!==1 (set file=!file:%oldFile%=%newFile%! if "%newFExt%"=="" ( ren "%%i" "!file!!ext!" 1>> batResult.txt 2<&1 if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt ) else (set /a a =1) ) else (ren "%%i" "!file!.%newFExt%" 2>> batResult.txt if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!.%newFExt%"-- >>batResult.txt ) else (set /a a =1)) ) else (set file=%newFile%!aStr! if "%newFExt%"=="" ( ren "%%i" "!file!!ext!" 1>> batResult.txt 2<&1 if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt ) else (set /a a =1 if !a! LSS 10 (set aStr=000!a! ) else if !a! LSS 100 (set aStr=00!a! ) else if !a! LSS 1000 (set aStr=0!a!)) ) else (ren "%%i" "!file!.%newFExt%" 2>> batResult.txt if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!.%newFExt%"-- >>batResult.txt ) else (set /a a =1 if !a! LSS 10 (set aStr=000!a! ) else if !a! LSS 100 (set aStr=00!a! ) else if !a! LSS 1000 (set aStr=0!a!)) ) ) rem echo a=!a!,oldFile-^> newFile:"%%i" -^> "!file!.%newFExt%" ) ) ) :end set /a a-=1 echo 总处理文件数:!ttl! ,处理成功数:!a! endlocal pause goto :eof rem ---------检查文件名-------参数带双引号--------------- :checkFileChar rem set proVar=%1& set proVar=!proVar:~1,-1! 也可写成这样 set proVar=%~1 rem set proVar=!proVar:~1,-1! 去掉双引号,效果同%~1 set noCharTmp=%2 set cnt2=0 set retVal=0 :cyc2 if "!proVar:~%cnt2%,1!"=="" (set retVal=0&goto :eof) rem echo 第%cnt2%个字符:"!proVar:~%cnt2%,1!","!proVar!",%proVar%,传入参数:%1,最后一个字符:'%proVar:~-1%' set cnt3=0 :cyc3 rem echo 第%cnt3%个排除字符:"!noCharTmp:~%cnt3%,1!" if "!noCharTmp:~%cnt3%,1!"=="" (set retVal=0&set /a cnt2 =1 &goto cyc2) if "!noCharTmp:~%cnt3%,1!"=="!proVar:~%cnt2%,1!" ( set retVal=1 goto :eof ) else (set /a cnt3 =1 goto cyc3 ) rem ---------检查扩展名-------参数带双引号-------------- rem ----- 只能输入0-9,A-Z,a-z,(原文件加:*、?) ----------- :checkExtChar rem set proVar2=%1 & set proVar2=!proVar2:~1,-2! 此行问题:在%1 &间有一个空格 set proVar2=%~1 rem set proVar2=%proVar2:~1,-1% set flag=%2 set cnt4=0 set retVal=0 :cyc4 rem echo %cnt4% "!proVar2:~%cnt4%,1!" if "!proVar2:~%cnt4%,1!"=="" (set retVal=0&goto :eof) rem if %flag%=="O" ( rem if "!proVar2:~%cnt4%,1!"=="*" (set /a cnt4 =1&goto cyc4 rem ) else if "!proVar2:~%cnt4%,1!"=="?" (set /a cnt4 =1&goto cyc4) ) if "!proVar2:~%cnt4%,1!" LSS "0" (set retVal=1&goto :eof) if /i "!proVar2:~%cnt4%,1!" GTR "z" (set retVal=1&goto :eof) if "!proVar2:~%cnt4%,1!" GTR "9" ( if /i "!proVar2:~%cnt4%,1!" LSS "a" (set retVal=1&goto :eof)) rem if "!proVar2:~%cnt4%,1!" GTR "Z" ( rem if "!proVar2:~%cnt4%,1!" LSS "a" (set retVal=1&goto :eof)) rem echo %cnt4% "!proVar2:~%cnt4%,1!" 4 set /a cnt4 =1 goto cyc4

注意:要确保将此批处理文件放在需要改名的文件夹下,尽量不要输入DOS命令中的特殊字符,如 *./^ \ | 等。

findstr相关补充说明:默认启用正则表达式

"/L" :如果要关闭正则表达式,即把^$\<.*等当成普通的字符,则用/L开关

批处理如何逐行提取文件内容(电脑里批量修改文件名)(12)

此处的$不在用于判断“ese”是最后字符了

".*" : 代表所有

"/c:" : 使用指定字符串作为文字搜索字符串,搜索字符串中的"."或"*"等符号不再是通配符,只是普通的句号、星号字符

批处理如何逐行提取文件内容(电脑里批量修改文件名)(13)

"/d" : 从用分号分隔的指定目录里查找所有txt文件里的“hello”字符串

批处理如何逐行提取文件内容(电脑里批量修改文件名)(14)

"/a:attr" : 指定目录和文件名的颜色,有两个字符时,第一个表示底色,第二个表示字体颜色,查color /?可知颜色代码如下:

批处理如何逐行提取文件内容(电脑里批量修改文件名)(15)

另外,下一目录为什么接在上一行的后面?是因为上一行最后没有回车引起的

"/f:file" : 将要查找范围内的文件名存放在一个文件中“file”中,从这些文件中搜索“hello”

批处理如何逐行提取文件内容(电脑里批量修改文件名)(16)

批处理如何逐行提取文件内容(电脑里批量修改文件名)(17)

findstr的详细用法请参照:DOS批处理命令:强大的find及findstr查找字符串搜索,坑也不少

批处理如何逐行提取文件内容(电脑里批量修改文件名)(18)

,