Check arguments in batch file - batch-file

I'm trying to make a batch script that behave almost like a Linux command in terms of arguments. Here is the idea of the script.
When I run the script with the scenarios described in the code it seems to work fine. The problem that I have is coming when I tried to test the program with wrong parameters. For the 1st parameter being either -manual or -automat and the 2nd parameter being wrong the behave is normal, the program prints "Invalid Argument".
The problem that I encounter is when the 1st argument is not -manual or -automat. In this case I get the error: goto was unexpected at this time.
Does any1 have any idea why this is happening and how can I solve the problem?
#echo off
IF %1!==! goto Result0
IF %1==-manual IF %2!==! goto Result1_manual
IF %1==-automat IF %2!==! goto Result1_auto
IF %1==-manual IF %2==1 goto Result2_manual
IF %1==-manual IF %2==2 goto Result3_manual
IF %1==-automat IF %2==1 goto Result2_auto
IF %1==-automat IF %2==2 goto Result3_auto
:done
echo "Invalid argument"
pause
cmd /k
:Result0
echo "Result0"
pause
cmd /k
:Result1_manual
echo "Result1_manual"
pause
cmd /k
:Result2_manual
echo "Result2_manual"
pause
cmd /k
:Result3_manual
echo "Result3_manual"
pause
cmd /k
:Result1_auto
echo "Result1_auto"
pause
cmd /k
:Result2_auto
echo "Result2_auto"
pause
cmd /k
:Result3_auto
echo "Result3_auto"
pause
cmd /k

If "%1"=="-manual" goto Result1_manual
If parameters might be missing enclose with another character. If %1 in blank and you don't
If ==-manual goto Result1_manual
an illegal syntax, and if you do
If ""=="-manual" goto Result1_manual
a legal syntax that resolves to false.
IF %2!==!
will only be true if %2 is blank. If that is your intention don't use the quote character. It sometimes has special meaning.

Related

Problem with exporting commands to other batch file

When I use the command echo pause >nul >>batchfile.bat from a cmd program it only prints out pause in batchfile.bat. How can I make it print pause >nul in batchfile.bat?
I've tried putting it in "", '', and doubling the arrows for nul >>nul and >>>nul but it printed either
pause; 'pause or showed an error "unexpected >`
at >>>nul.
echo pause >nul >>startserver.bat
I took it from my batch file which creates a command pause >nul and writes it into startserver.bat, however I only get a pause without >nul as output on startserver.bat
I need it to give output pause >nul in startserver.bat.
What I get is only pause, the >nul gets basically lost.
> is getting interpreted as redirection (and immediately overridden by the following >>). You have to escape it to make it interpreted literally. CMD's escape character is the caret, so do this:
echo pause ^>nul >>startserver.bat

Batch "if" statement not working

I seem to have a problem with my "if" statements. Currently after the pause in "start" the file just closes and nothing else happens. Chances are, I have no idea what I'm doing.
#echo off
set startnum=0
goto start
:start
pause
set startnum="!startnum!"+1
if "%startnum%"="%0%" goto fail
if "%startnum%"="%1%" goto success
goto start
:success
cls
echo It worked!
echo "%startnum%"
pause.
exit
:fail
cls
echo Failure
pause.
exit
First problem:
set startnum="!startnum!"+1
Evidently, you wish to add 1 to startnum.
Your set command would set startnum to "!startnum!"+1. Literally. To perform arithmetic, you need set /a.
set /A startnum="!startnum!"+1
well, this won't work as "!startnum! isn't numeric. Had you invoked delayedexpansion beforehand, then the value of startnum would have been substituted for !startnum! yielding set /A startnum="0"+1 which makes more, but still not much sense.
set /A startnum=startnum+1
adds 1 to startnum - see set /? from the prompt for documentation.
set /A startnum+=1
would also add 1 to startnum.
Next problem.
if "%startnum%"="%0%" goto fail
Well, you appear to have found lss and all that gang. Problem is that the simple comparison operator is ==, not =.
if "%startnum%"=="%0%" goto fail
Now - what will that do? It will compare "thecontentsofstartnum" to "thecontentsof0". Since both of these arguments are quoted, batch will perform a string comparison. With a string comparison, 123 is less than 89 because 1 is less than 8.
But - you are attempting an equivalence comparison (equ as the operator may be used instead of ==) so the preceding point is simply AAMOI.
The difficulty is %0% which you may believe attempts to extract the value of the variable 0 but actually it replaces %0 with the value of the 0th parameter to the batchfile, which is the batchfile name itself, so you get "batchfilename%" - probably not what you actually wanted.
if "%startnum%"=="0" goto fail
is the way to implement that test.
The first IF statement is preprocessed by cmd.exe to
if ""!startnum!"+1"="test.bat" goto fail
which is a completely invalid IF condition.
cmd.exe outputs a syntax error message because of "="test.bat"" and exits batch file processing. This can be seen by debugging the batch file.
The solution is using right syntax for
assigning a value to an environment variable,
an arithmetic expression,
and last but not least the IF condition itself.
The batch file code fixed:
#echo off
set "startnum=0"
goto Begin
:Begin
set /A startnum+=1
if "%startnum%" == "0" goto Fail
if "%startnum%" == "1" goto Success
goto Begin
:Success
cls
echo It worked!
echo "%startnum%"
pause
exit /B
:Fail
cls
echo Failure
pause
exit /B
It would be safe here to remove all double quotes on both IF conditions.
One more hint: Don't use the name of a command like START as label. It works, but it should be avoided in case of ever adding to batch file the command START and searching for either command or label.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains how to reference batch file arguments.
cls /?
echo /?
exit /?
goto /?
if /?
pause /?
set /?
Further read the answers on following questions:
Batch file comparison of variable with constant fails
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Where does GOTO :EOF return to?
Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files

Goto was unexpected at this time [BATCH]

So basically I was working on my Terminal I am creating in batch and this strange error pops up for a second and then the window closes:
"Goto was unexpected at this time"
I have no idea what's going on. Here's my code:
#ECHO off
set codename=Nature
echo Windows Bat Terminal
echo Codename "%codename%"
:terminal
set /p terminalcommand=Command:
if %terminalcommand%==help goto help
if %terminalcommand%==clr goto clear
if %terminalcommand%==exit goto exit
if %terminalcommand%==color goto color
if %terminalcommand%==time goto timedate
echo.
echo Bad command!
:terminal1
goto terminal`
To recreate simply run this in CMD.
You haven't told us what entry you made to generate this behaviour. The standard cure is to quote each side if the if comparison (if /i "%terminalcommand%"=="time" goto ... (the /i make the comparison case-insensitive)) because if you simply press enter then the command is resolved to "if ==time goto ..." and cmd will see goto where it expects a comparison operator like ==, hence the response. If you run this batch from the command prompt, the window won't close and you'll be able to see these messages better

How to use launch commands in a batch file

Okay, well, I want to make something like OS... But when I start the .bat file there's a set option for going to help and going to the GUI. I have shortcut with -gsys32 - directly to boot the command. Brhfd... Just take a look if the code, I leave some comments.
#ECHO off
echo GraphicalSystem
echo All rights reserved! 2016
echo PLEASE WRITE help TO OPEN THE HELP WINDOW!
set /p command=
if %command% ==help goto help
if %command% ==gsys32 goto interface
cmd /k
:help
cls
echo gsys32 - Open the graphical interface.
echo exithelp - exit the help
set /p command=
if %command% ==exithelp goto start
cmd /k
:interface
cls
color 17
#ECHO OFF
echo PROGRAMS - GSYS32
echo DRIVE A:\
dir
#ECHO OFF
echo *Write dir /name of the directory without the slashes/*
cmd /k
:start
cls
#ECHO off
echo GraphicalSystem
echo All rights reserved! 2016
echo PLEASE WRITE help TO OPEN THE HELP WINDOW!
set /p command=
if %command% ==help goto help
if %command% ==gsys32 goto interface // I want this to be executed with the shortcut.
cmd /k
And now the shortcut:
So... I want the command from the screenshot to be executed in the code...
Seems to be just a section of the code, so it's difficult to tell.
set "command=%1"
if not defined command goto noparms
if /i "%command%"=="gsys" goto interface
if /i "%command%"=="help" goto help
echo parameter "%1" not recognised&pause
rem don't know what you want to do now...
...
:noparms
rem there were no parameters supplied
....your posted code
%1 accesses the first parameter supplied to the routine.
you would need -gsys in place of gsys if you want to detect -gsys as a parameter. The match is literal (/i option makes it case-insensitive)
It is windows convention that switch-parameters use the format /gsys - but that's a convention, not a rule.
set /p "var=Promptstring" is the general form for accepting keyboard input. Parameters are read using %1..%9

Psshutdown not working in batch files

As I stated above Psshutdown refuses to work in a batch file but works fine in a command prompt. The script has some light logic to determine what group of PCs and such. Here is the script:
#ECHO OFF
cd "C:\temp\remote enable rdp"
goto :SET
:SET
set /p groupPC=pc or list?:
if %groupPC% == pc goto :PC
if %groupPC% == list goto :LIST
goto :SKIP
:PC
ECHO[
set /p pcName=Which PC?:
psshutdown -c -k \\%pcName% -r
PAUSE
goto :DONE
:LIST
ECHO[
set /p input=Which list?:
set list=%input%.txt
psshutdown #C:\Temp\Lists\%list% -r -f else goto :SKIP
PAUSE
goto :DONE
:SKIP
ECHO[
ECHO You probably typed something wrong. Starting from the top.
PAUSE
ECHO[
goto :SET
:DONE
ECHO Mischief Managed
TIMEOUT /t 10
EXIT /B
Every time I run either the PC logic or the List logic the prompt merely shows me the psshutdown syntax uses. I have tried every configuration of syntax I can find on the internet. Any thoughts?
Edit:It's worth noting that the #file syntax I'm using works almost verbatim with psexec.
Two things stand out to me. If your filename has a space in it, that would produce the results you mentioned. Try putting quotes around the file path.
I also can't make sense of the "else" statement at the end of your line. Was that a mistake? It should work with the below line instead.
psshutdown #"C:\Temp\Lists\%list%" -r -f

Resources