I am running a Batch script for processing some files. When it determines that the current file needs no processing I would like to skip the rest of the commands for that file and move directly to the next one. How? Goto :EOF
and exit /b
both exit the entire script.
Here is the script. It takes a directory and renames all the files to include all the words from parent directory names except the ones already included in the filename.
@echo offsetlocal enabledelayedexpansionrem set input directory (add check for validity)set "inputDir=%~1"set "confirmation=false":proceedfor /r "%inputDir%" %%F in (*.*) do ( rem use path to file as keywords set "words=%%~pF" rem replace known useless marks with spaces set "words=!words:\= !" set "words=!words:_= !" set "words=!words:,= !" set "words=!words: = !" rem remove known useless words set "words=!words: The = !" set "words=!words: A = !" set "words=!words: An = !" set "words=!words: Video = !" set "words=!words: New Folder = !" rem remove word that already exists in the filename for %%W in (!words!) do echo %%~nF | find /i "%%W" >nul && set "words=!words: %%W = !" rem replace leading and trailing spaces with brackets set "words=[!words:~1,-1!]" rem if all words already included end task for current file if "!words!"=="[]" exit /b rem build the new filename set "newName=%%~nF !words!%%~xF" rem fix "] [" caused by repeated renaming, causes fusion with non-related bracketed sets, which is fine set "newName=!newName:] [= !" rem task for displaying the name change for confirmation if /i not "%confirmation%"=="yes" echo old "%%F" && echo new "!newName!" rem task for doing the actual rename if confirmed if /i "%confirmation%"=="yes" echo ren "%%~nxF" "!newName!" & echo ren "%%F" "!newName!")rem fails to rename files with ! mark in the filename. no other trouble, just won't rename those. error: the syntax of the command is incorrectrem if coming from second (completed) run then exitif /i "%confirmation%"=="yes" exit /brem ask for confirmation to run again for realset /p confirmation="confirm you want to perform all these rename tasks (type yes or no)?: "if /i not "%confirmation%"=="yes" echo confirmation deniedif /i "%confirmation%"=="yes" goto proceedendlocal
I am also trying an alternate method of conditionally running the last commands like so
@echo offsetlocal enabledelayedexpansionrem set input directory (add check for validity)set "inputDir=%~1"set "confirmation=false":proceedfor /r "%inputDir%" %%F in (*.*) do ( rem use path to file as keywords set "words=%%~pF" rem replace known useless marks with spaces set "words=!words:\= !" set "words=!words:_= !" set "words=!words:,= !" set "words=!words: = !" rem remove known useless words set "words=!words: The = !" set "words=!words: A = !" set "words=!words: An = !" set "words=!words: Video = !" set "words=!words: New Folder = !" rem remove word that already exists in the filename for %%W in (!words!) do echo %%~nF | find /i "%%W" >nul && set "words=!words: %%W = !" rem replace leading and trailing spaces with brackets set "words=[!words:~1,-1!]" rem if all words not already included in the filename do if not "!words!"=="[]" ( rem build the new filename set "newName=%%~nF !words!%%~xF" rem fix "] [" caused by repeated renaming, causes fusion with non-related bracketed sets, which is fine set "newName=!newName:] [= !" rem task for displaying the name change for confirmation if /i not "%confirmation%"=="yes" echo old "%%F" && echo new "!newName!" rem task for doing the actual rename if confirmed if /i "%confirmation%"=="yes" echo ren "%%~nxF" "!newName!" & echo ren "%%F" "!newName!" ))rem fails to rename files with ! mark in the filename. no other trouble, just won't rename those. error: the syntax of the command is incorrectrem if coming from second (completed) run then exitif /i "%confirmation%"=="yes" exit /brem ask for confirmation to run again for realset /p confirmation="confirm you want to perform all these rename tasks (type yes or no)?: "if /i not "%confirmation%"=="yes" echo confirmation deniedif /i "%confirmation%"=="yes" goto proceedendlocal
But this is a disaster, resulting in filenames that are cut from seemingly random parts of the filename with words
added.