Skip a command line in batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
falcios
Posts: 43
Joined: 02 Mar 2017 05:38

Skip a command line in batch file

#1 Post by falcios » 20 Mar 2017 08:25

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.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Skip a command line in batch file

#2 Post by penpen » 20 Mar 2017 10:09

You could use a command line parameter (option 1) or choice to get user input, so this "test.bat" may help you:

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

falcios
Posts: 43
Joined: 02 Mar 2017 05:38

Re: Skip a command line in batch file

#3 Post by falcios » 20 Mar 2017 16:17

Thanks it works great.

Can you please explain what this line does:
setlocal enableExtensions disableDelayedExpansion

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Skip a command line in batch file

#4 Post by penpen » 20 Mar 2017 17:59

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

Post Reply