欢迎来到小居数码网-一家分享数码知识,生活小常识的网站,希望可以帮助到您。

当前位置:生活小常识 > 数码知识 >
优质

批量修改文件夹名称批处理(文件夹批量重命名)

数码知识

丁佳皓优秀作者

原创内容 来源:小居数码网 时间:2024-07-30 09:06:01 阅读() 收藏:35 分享:59

导读:您正在阅读的是关于【数码知识】的问题,本文由科普作家协会,生活小能手,著名生活达人等整理监督编写。本文有3814个文字,大小约为13KB,预计阅读时间10分钟。

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

文件夹批量重命名

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

实现逻辑

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

字符替换:

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

原文件

运行批处理文件batRename2.bat:

结果:

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

操作:

结果:

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

操作:

结果:

程序实现方法一

@echo offrem 切换为utf-8编码::Chcp 65001::切换中文Chcp 936 > nulrem 启用延缓环境变量扩展setlocal EnableDelayedExpansionecho **可修改当前文件夹下所有的文件名或字符及扩展名**echo *****将此批处理文件拷入需要修改的文件夹中******set option=9set cnt=0set skipChar=/*?:"<>|^!echo **   请选择操作...        **echo ** 1 替换部分字符或扩展名 **echo ** 0 换成全新文件名       **:selectOptionset /p option= 》》请选择操作选项:if %option%==1 ( goto endSelectOption) else if %option%==0 (goto endSelectOption) set /a cnt+=1if %cnt% GTR 2 (goto end) else (goto selectOption):endSelectOptionecho ------要修改的原文件信息,回车结束输入-------set retVal=0:inputOfiset oldFile=set retVal=0if %option%==1 (  set /p oldFile="》》请输入要替换的文件名/文字(必输入):") else (set /p oldFile="》》请输入要修改的文件名/文字(不输入则包括当前文件夹下所有文件): ")if %option%==1 (if "%oldFile%"=="" echo 被替换字符不能为空!& goto inputOfi ) else (if "%oldFile%"=="*"  goto inputOfeif "%oldFile%"=="" ( set oldFile=*     goto inputOfe))@echo !oldFile! | findstr "[!skipChar!]" > nulif "!errorlevel!"=="0" (echo !!输入的文件名/字符"!oldFile!"错误!!rem 如果用%skipChar%则出现“此时不应有 >”的错误提示。echo !!字符中不能包含"!skipChar!"中的字符 !!goto inputOfi):inputOfeset oldFExt=set retVal=0set /p oldFExt= 》》请输入原文件扩展名(不输入则包括所有扩展名):if "%oldFExt%"=="*" goto endInputOfeif "%oldFExt%"=="" (set oldFExt=*) else (@echo !oldFExt!| findstr "[^0-9A-Za-z]" > nulif "!errorlevel!"=="0" (echo !!扩展名"%oldFExt%"错误!!echo !!请输入0-9,A-Z,a-z之间的字符!!goto inputOfe)):endInputOfeecho === 查找范围:"*!oldFile!*.!oldFExt!" ===echo -----新的文件信息,回车结束输入------:inputNfiset newFile=set retVal=0set /p newFile="》》请输入新的文件名/字符(必输入):"if not "%newFile%"=="" (@echo !newFile! | findstr "[!skipChar!]" > nulif "!errorlevel!"=="0" (echo !!输入的文件名/字符"%newFile%"错误!!echo !!字符中不能包含"!skipChar!"中的字符 !!goto inputNfi)) else (echo 新的文件名/字符不能为空!& goto inputNfi):inputNfeset newFExt=set retVal=0if %option%==1 (if "!oldFExt!"=="*" (goto endInputNfe ))set /p newFExt="》》输入新的文件扩展名(不输入则保留原扩展名):")if "!newFExt!" neq "" (@echo !newFExt!| findstr "[^0-9A-Za-z]" > nulif "!errorlevel!"=="0" (echo !!   扩展名"%newFExt%"错误   !!echo !!请输入0-9,A-Z,a-z之间的字符!!goto inputNfe)):endInputNfeecho === 查找范围: "*!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=0set a=1set aStr=0001set /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时,只有.txtset ext=%%~xiif !option!==1 (set file=!file:%oldFile%=%newFile%!if "%newFExt%"=="" (ren "%%i" "!file!!ext!" 1>> batResult.txt 2<&1if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt) else (set /a a+=1)) else (ren "%%i" "!file!.%newFExt%" 2>> batResult.txtif "!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<&1if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt) else (set /a a+=1if !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.txtif "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!.%newFExt%"-- >>batResult.txt) else (set /a a+=1if !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!))))remecho a=!a!,oldFile-^> newFile:"%%i" -^> "!file!.%newFExt%"    )    )):endset /a a-=1echo 总处理文件数:!ttl! ,处理成功数:!a!endlocalpausegoto :eof

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

@echo offrem 切换为utf-8编码::Chcp 65001::切换中文Chcp 936 > nulrem 启用延缓环境变量扩展setlocal EnableDelayedExpansionecho **可修改当前文件夹下所有的文件名或字符及扩展名**echo *****将此批处理文件拷入需要修改的文件夹中******set option=9set cnt=0set skipChar=/*?:"<>|^!echo **   请选择操作...        **echo ** 1 替换部分字符或扩展名 **echo ** 0 换成全新文件名       **:selectOptionset /p option= 》》请选择操作选项:if %option%==1 ( goto endSelectOption) else if %option%==0 (goto endSelectOption) set /a cnt+=1if %cnt% GTR 2 (goto end) else (goto selectOption):endSelectOptionecho ------要修改的原文件信息,回车结束输入-------set retVal=0:inputOfiset oldFile=set retVal=0if %option%==1 (  set /p oldFile="》》请输入要替换的文件名/文字(必输入):") else (set /p oldFile="》》请输入要修改的文件名/文字(不输入则包括当前文件夹下所有文件): ")if %option%==1 (if "%oldFile%"=="" echo 被替换字符不能为空!& goto inputOfi ) else (if "%oldFile%"=="*"  goto inputOfeif "%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):inputOfeset oldFExt=set retVal=0set /p oldFExt= 》》请输入原文件扩展名(不输入则包括所有扩展名):if "%oldFExt%"=="*" goto endInputOfeif "%oldFExt%"=="" (set oldFExt=*) else (call :checkExtChar "!oldFExt!" "O"if !retVal!==1 (echo !!扩展名"%oldFExt%"错误!!echo !!请输入0-9,A-Z,a-z之间的字符!!goto inputOfe)):endInputOfeecho === 查找范围:"*!oldFile!*.!oldFExt!" ===echo -----新的文件信息,回车结束输入------:inputNfiset newFile=set retVal=0set /p newFile="》》请输入新的文件名/字符(必输入):"if not "%newFile%"=="" (call:checkFileChar "!newFile!" %skipChar%if !retVal!==1 (echo !!输入的文件名/字符"%newFile%"错误!!echo !!字符中不能包含"!skipChar!"中的字符 !!goto inputNfi) ) else (echo 新的文件名/字符不能为空!& goto inputNfi):inputNfeset newFExt=set retVal=0if %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)):endInputNfeecho === 查找范围: "*!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=0set a=1set aStr=0001set /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时,只有.txtset ext=%%~xiif !option!==1 (set file=!file:%oldFile%=%newFile%!if "%newFExt%"=="" (ren "%%i" "!file!!ext!" 1>> batResult.txt 2<&1if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt) else (set /a a+=1)) else (ren "%%i" "!file!.%newFExt%" 2>> batResult.txtif "!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<&1if "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!!ext!"-- >>batResult.txt) else (set /a a+=1if !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.txtif "!errorlevel!" neq "0" ( echo --原文件"%%i",新文件名"!file!.%newFExt%"-- >>batResult.txt) else (set /a a+=1if !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!))))remecho a=!a!,oldFile-^> newFile:"%%i" -^> "!file!.%newFExt%"    )    )):endset /a a-=1echo 总处理文件数:!ttl! ,处理成功数:!a!endlocalpausegoto :eofrem ---------检查文件名-------参数带双引号---------------:checkFileCharrem set proVar=%1& set proVar=!proVar:~1,-1!也可写成这样set proVar=%~1rem set proVar=!proVar:~1,-1! 去掉双引号,效果同%~1set noCharTmp=%2set cnt2=0set retVal=0:cyc2if "!proVar:~%cnt2%,1!"==""  (set retVal=0&goto :eof)rem echo 第%cnt2%个字符:"!proVar:~%cnt2%,1!","!proVar!",%proVar%,传入参数:%1,最后一个字符:'%proVar:~-1%'set cnt3=0:cyc3rem 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=1goto :eof) else (set /a cnt3+=1goto cyc3 )rem ---------检查扩展名-------参数带双引号--------------rem ----- 只能输入0-9,A-Z,a-z,(原文件加:*、?) -----------:checkExtCharrem set proVar2=%1 & set proVar2=!proVar2:~1,-2! 此行问题:在%1 &间有一个空格set proVar2=%~1rem set proVar2=%proVar2:~1,-1%set flag=%2set cnt4=0set retVal=0:cyc4rem echo %cnt4% + "!proVar2:~%cnt4%,1!"if "!proVar2:~%cnt4%,1!"==""     (set retVal=0&goto :eof)rem if %flag%=="O" (remif "!proVar2:~%cnt4%,1!"=="*" (set /a cnt4+=1&goto cyc4rem) 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!" + 4set /a cnt4+=1goto cyc4

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

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

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

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

".*" : 代表所有

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

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

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

  • 0 = 黑色 8 = 灰色
  • 1 = 蓝色 9 = 淡蓝色
  • 2 = 绿色 A = 淡绿色
  • 3 = 浅绿色 B = 淡浅绿色
  • 4 = 红色 C = 淡红色
  • 5 = 紫色 D = 淡紫色
  • 6 = 黄色 E = 淡黄色
  • 7 = 白色 F = 亮白色
  • 另外,下一目录为什么接在上一行的后面?是因为上一行最后没有回车引起的

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

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

    上面就是小居数码小编今天给大家介绍的关于(文件夹批量重命名)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。

    94%的朋友还想知道的:

    (387)个朋友认为回复得到帮助。

    部分文章信息来源于以及网友投稿,转载请说明出处。

    本文标题:批量修改文件夹名称批处理(文件夹批量重命名):http://sjzlt.cn/shuma/152779.html

    猜你喜欢