We have already used the cyclic command to batch process the renaming of a large number of files. But to sum up, the batch process is not very robust.
Judging a program is good or bad, often not from the programmer's point of view, but from the user's point of view.
For example: What if you enter an incorrect path format when the user uses it? What if you enter a prefix that contains an illegal symbol? What if there is a problem with the extended name you entered? After changing the name, I don't see whether to perform successful feedback, and so on. With these thoughts, we will revise the original program again.
:::::: Batch modification file name. BAT:::.:: @echo off title batch modify filename setlocal enabledelayedexpansion:: Enable delay Variable expansion: GetPath set zpath=%cd%:: Initializes the variable to prevent the user from entering and skipping directly. Where%cd% represents the current path set/p zpath= Please enter the path to the destination file: If%zpath:~0,1%%zpath:~-1%== "" Set zpath=%zpath:~1,-1%:: Check variable Zpath the first and last characters are No is "", the words will be removed if not exist "%zpath%" Goto:getpath:: If the Zpath value of the path does not exist, you have to jump back to require re-enter: Getprefix set prefix= unnamed set/p pre fix= Please enter a filename prefix (cannot contain the following characters \/:*? " <>|) : for/f "delims=\/:*? <>| tokens=2 "%%i in" ("Z%prefix%z") do Goto:getprefix:: Here we check the variable perfix, and then jump to the illegal symbol: Getprefix:: In fact, there is no double quotation mark "to detect, because the double
Quotation marks cannot be escaped here as an available delimiter:: Even in this program, incorrect use of double quotes can cause program exceptions to exit. :: Therefore, it is not an easy thing to do to make it very human: Getext set ext=.* set/p ext= Please enter the file's extension (no input means all types): If not "%ext:~0,1%" = "." Set ext=.%ext%: : Checks whether the first of the variable ext is a period.
, is not to add:: Suggest here to the variable ext also check, found that there is an illegal symbol outside the * jump to: Getext set answer=n echo. echo you are trying to%zpath%\
All the%ext% types of files in the%prefix% are renamed in batches with the prefix name, do you want to continue? set/p answer= Continue Please enter Y, enter the other key to discard ... if "%answer%" = = "Y" goto:readytorename if "%answer%" == "Y" goto:readytorename echo Discard file Rename, press any key to exit ... & Goto:P ausethenquit:readytorename set/a num=0 Echo. If "%ext%" = = ". *" (For%%i in ("%zpath%\*%ext%") do (set/a num+=1 ren "%%i" "%prefix%!num!%%~xi" | |% echo file% I name failure && set/a num-=1)) Else (for%%i in ("%zpath%\*%ext%") do (set/a num+=1 ren "%%i" "%prefix") %!num!%ext% "| | echo file%%i name failure && set/a num-=1)) if%num%==0 echo%zpath%\ No files were found in the.
Press any key to exit ... & goto:P ausethenquit Echo file renamed complete, press any key to exit ...:P ausethenquit::::.::.::::,-------