电脑里批量修改文件名,批处理实现及详细说明,findstr补充说明( 二 )

程序实现方法二:与方法一区别在于判断字符的合法性方面
@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 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):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时 , 只有testset file=%%~ni ::只有扩展名 , 如test.txt时 , 只有.txt set 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!))) )rem echo 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+=1 goto 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" (rem if "!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" (remif "!proVar2:~%cnt4%,1!" LSS "a" (set retVal=1&goto :eof))rem echo %cnt4% + "!proVar2:~%cnt4%,1!" + 4set /a cnt4+=1goto cyc4


推荐阅读