My batch file consists of xcopy commands that backup certain folders. There is one folder that doesn't get updated as often so I want have the option to skip that line when the batch file gets to it.
Is there a way to setup for user input to skip the command line, if possible?
Or any other suggestions?
Thanks in advance.
Skip a command line in batch file
Moderator: DosItHelp
Re: Skip a command line in batch file
You could use a command line parameter (option 1) or choice to get user input, so this "test.bat" may help you:
Result:
penpen
Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
echo Option 1:
if not "%~1" == "skipNextLine" ^
echo This is the next line.
echo(
echo Option 2:
:choice
choice /C YN /M "Do you want to skip the next line?" && goto :choice || if errorlevel 2 ^
echo This is the next line.
endlocal
goto :eof
Result:
Code: Select all
Z:\>check.bat "skipNextLine"
Option 1:
Option 2:
Do you want to skip the next line? [Y,N]?Y
Z:\>check.bat
Option 1:
This is the next line.
Option 2:
Do you want to skip the next line? [Y,N]?N
This is the next line.
penpen
Re: Skip a command line in batch file
Thanks it works great.
Can you please explain what this line does:
setlocal enableExtensions disableDelayedExpansion
Can you please explain what this line does:
setlocal enableExtensions disableDelayedExpansion
Re: Skip a command line in batch file
The "setlocal" command primarily starts localization of the environment:
So it kinda saves all environment variables at that time, and if you use endlocal then this saved environment is restored.
The parameter "enableExtensions" tells the "setlocal" command to enable an extended commands:
So the commands have a basic behaviour, and an additional behaviour which can be switched on and off.
For example the basic batch argument accessor "%1" doesn't support the "~", but there is an extended behaviour, so "%~1" is recognized as valid.
With "disableDelayedExpansion" you disable the (delayed) variable expansion using exclamation marks ("!variable!").
penpen
So it kinda saves all environment variables at that time, and if you use endlocal then this saved environment is restored.
The parameter "enableExtensions" tells the "setlocal" command to enable an extended commands:
So the commands have a basic behaviour, and an additional behaviour which can be switched on and off.
For example the basic batch argument accessor "%1" doesn't support the "~", but there is an extended behaviour, so "%~1" is recognized as valid.
With "disableDelayedExpansion" you disable the (delayed) variable expansion using exclamation marks ("!variable!").
penpen