SoFunction
Updated on 2025-04-09

Batch processing implements batch modification of file names

We've already used loop commands to batch process a large number of files to rename. But to sum up, the batch process is not very robust.

To judge the quality of a program is often not from the perspective of the programmer, but from the user's perspective.

For example: What should I do if I enter an incorrect path format when the user uses it? What if you enter a prefix containing an illegal symbol? What should I do if there is a problem with the extension entered? After changing the name, you cannot see feedback on whether the execution is successful, etc. With these ideas, we will modify the original program again.

:::::::Modify file names in batches.bat:::::::
@echo off
title Modify file names in batches
setlocal EnableDelayedExpansion
:: Enable delay variable expansion

:GetPath
set zpath=%CD%
:: Initialize variables,Prevent users from skipping without input。in%CD%Indicates the current path
set /p zpath=Please enter the path of the target file:
if %zpath:~0,1%%zpath:~-1%=="" set zpath=%zpath:~1,-1%
:: Check variables zpath Whether the first and last characters are "", if so, remove it
 if not exist "%zpath%" goto :GetPath
:: if zpath The path to the value does not exist,You have to jump back,Re-enter

:GetPrefix
set prefix=Unnamed
set /p prefix=Please enter the file name prefix(Cannot contain the following characters\/:*?"<>|):
for /f "delims=\/:*?<>| tokens=2" %%i in ("z%prefix%z") do goto :GetPrefix
:: Here is the variable perfix Conduct inspection,Illegal symbols are found to jump to :GetPrefix
:: In fact,There are no double quotes here " Perform testing,Because double quotes cannot be escaped here as available delimiters
:: Even in this program,Incorrect use of double quotes can also cause program exceptions and exit。
:: therefore,It's not easy to make it very human

:GetExt
set ext=.*
set /p ext=Please enter the file extension(If you don't enter, it means all types):
if not "%ext:~0,1%"=="." set ext=.%ext%
:: Check variables ext Is the first one a period . ,If not, add
:: 建议Here is the variable ext Check it out too,Discovery*The illegal symbols outside jump to :GetExt

set answer=N
echo.
echo You're trying to %zpath%\ All in %ext% Files of type %prefix% Batch rename for prefix,Whether to continue?
set /p answer=Please enter Y ,Enter other keys to give up...
if "%answer%"=="Y" goto :ReadyToRename
if "%answer%"=="y" goto :ReadyToRename

echo Give up file rename,Press any key to exit... & goto :PauseThenQuit

:ReadyToRename

set /a num=0
echo.

if "%ext%"==".*" (
  for %%i in ("%zpath%\*%ext%") do (
    set /a num+=1
    ren "%%i" "%prefix%!num!%%~xi" || echo document %%i Failed to change the name && set /a num-=1
  )
) else (
  for %%i in ("%zpath%\*%ext%") do (
    set /a num+=1
    ren "%%i" "%prefix%!num!%ext%" || echo document %%i Failed to change the name && set /a num-=1
  )
)

if %num%==0 echo %zpath%\ 里未发现任何document。Press any key to exit... & goto :PauseThenQuit

echo document改名完成,Press any key to exit...

:PauseThenQuit
pause>nul
::::::::::::::::::::::::::::::::