Solved

DOS GOTO labels and how to echo help text for invalid parameters for a cmd file

Posted on 2008-10-21
12
1,065 Views
Last Modified: 2010-04-21
I have the cmd file below to start and stop some services. I also have some other small files to do other tasks. I would like to have a smooth way to echo some help text in case an invalid argument is given by the user. The command below seem to work OK but maybe there is a more elegant way to do this?

Secondly, how can I echo the "help" message without the double quotes? I had to add these so that CMD should not try to interpret the pipe and brackets inside the message string.

Finally, this only works if there are no arguments, how do I handle the case where there is an invalid argument? There must be a better way than to check if %1 == "START" or "STOP", I think.
@echo off
 

if defined 1 goto :%1

echo "useage: [START|STOP]"

goto :EOF
 

:START

echo start....

goto :EOF
 

:STOP

echo stop....

goto :EOF

Open in new window

0
Comment
Question by:Sharp2b
  • 7
  • 4
12 Comments
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22765473
In batch files, you can just escape characters using a caret "^". For the other problem, I don't really think that's possible without doing a simple check.

Kind regards,

Matthias Vance
echo Usage: [START^|STOP]

Open in new window

0
 

Author Comment

by:Sharp2b
ID: 22765564
Thanks, I didn't think of trying the caret as an escape character.

For the other, I was hoping that there was a way so that I could have a "fixed" framework for my utility files and just add a new label at the bottom when I wanted to add a new parameter. I would like a way to mimic a "try...catch" or "on error goto".
0
 

Author Comment

by:Sharp2b
ID: 22765575
Sorry, too quick posting.
I also wanted to say that I will leave it for a few hours to see if there are any other hints. Otherwise I will close and assign you the points.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22774654
Like this?
@echo off

FINDSTR /i /c:":%1" %~nx0>NUL

IF NOT ERRORLEVEL 1 GOTO :%1

echo usage: %0 [START^|STOP]

goto :EOF
 

:START

echo Start...

GOTO :EOF
 

:STOP

ECHO Stop...

GOTO :EOF

Open in new window

0
 

Author Comment

by:Sharp2b
ID: 22774746
Thanks, I think we are getting closer...
I like the way you added %0 to the output string, then I don't even need to worry about the file name, I didn't know about the %0 varaible.

BUT, there is still something missing, regardless of what parameter (if any) I give, I get the follwing output (the command file is called test.cmd):
C:\>test
FINDSTR: Cannot open test.cmd
usage: test [START|STOP]

C:\>test xxx
FINDSTR: Cannot open test.cmd
usage: test [START|STOP]

C:\>test start
FINDSTR: Cannot open test.cmd
usage: test [START|STOP]

C:\>test stop
FINDSTR: Cannot open test.cmd
usage: test [START|STOP]

C:\>
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 250 total points
ID: 22777405
Hmm... is test.cmd in C:\ or did you put it somewhere else? Regardless we can fix that too.  I had a chance to sleep so I thought of other things.

This is so cool.
@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

FINDSTR /i /c:":%1" "%~dpnx0">NUL

IF NOT ERRORLEVEL 1 GOTO :%1

Set Usage=

FOR /f %%a in ('FINDSTR /b ":" "%~dpnx0"') DO Set Usage=!Usage!%%a
 

Set Usage=!Usage:~1!

echo usage: %0 [!Usage::=^|!]

goto :EOF
 

:START

echo Start...

GOTO :EOF
 

:STOP

ECHO Stop...

GOTO :EOF
 

:PAUSE

ECHO Pause...

GOTO :EOF

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Sharp2b
ID: 22781383
Cool, now I get it, you are actually searching the file itself to find the labels.
(btw. in my current case, the file is located in C:\util)

This is better than expected so I will assign the points now. If you feel you need to, there is still a little detail left. In the last case below, the situation when no argument is given is not handled. ;-)

Here are the results:
C:\>test start
Start...

C:\>test stop
Stop...

C:\>test pause
Pause...

C:\>test xxx
usage: test [START|STOP|PAUSE]

C:\>test
The system cannot find the batch label specified -
0
 

Author Closing Comment

by:Sharp2b
ID: 31508971
As easy as it is to understand the %~dpn...stuff.:-)
Thanks a lot for providing the answers and enhancing it as well as allowing me to learn some more!
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22781701
Simple.

Add another IF.

One more thing if you type in

test.cmd

It will echo back:

usage: test.cmd [START|STOP|PAUSE]

If you like to only echo test then:

echo usage: %~n0 [!Usage::=^|!]
@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

FINDSTR /i /c:":%1" "%~dpnx0">NUL

IF NOT ERRORLEVEL 1 IF "%1" NEQ "" GOTO :%1

Set Usage=

FOR /f %%a in ('FINDSTR /b ":" "%~dpnx0"') DO Set Usage=!Usage!%%a

 

Set Usage=!Usage:~1!

echo usage: %0 [!Usage::=^|!]

goto :EOF
 

:START

echo Start...

GOTO :EOF

 

:STOP

ECHO Stop...

GOTO :EOF

 

:PAUSE

ECHO Pause...

GOTO :EOF

Open in new window

0
 

Author Comment

by:Sharp2b
ID: 22783635
Thanks a lot!
I'm really starting to enjoy these command scripts / bat files again after having abandoned them for a couple of years in favor of "real" programs. But, if I just get some help with the syntax and all the tricks inside, they are actually very powerful. Can run on basically all Win OS machines and no installation required. Ad-hoc modifications are easy if required, no need to re-compile.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22786502
Yes. I use it for the same reasons. There are many times where a machine is broken and using other programming languages just can't fix the issue.

OK. A couple more things for you.

I don't want to limit your use of labels just because you want your help to look correct using this cool new way of dynamically getting the parameters. Simply add a space in front of the labels you don't want to appear in the usage help.

Also I put in a del "%~dpnx0" to delete the batch file if you want to use it temporarily. When do I use this? Using PSEXEC you can have it copy your batch file to a remote machine and execute it but PSEXEC leaves the batch file on the remote machine. The very last line of the script deletes it to leave no trace of it. It does produce an error though. The batch file cannot be found.

An example of using call. Note: :RESTART in particular and that it changes %1 for :RUN.
@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

FINDSTR /i /c:":%1" "%~dpnx0">NUL

IF NOT ERRORLEVEL 1 IF "%1" NEQ "" IF "%2" NEQ "" CALL :%1 %1 %2 & GOTO DELCMD

Set Usage=

FOR /f %%a in ('FINDSTR /b ":" "%~dpnx0"') DO Set Usage=!Usage!%%a

 

Set Usage=!Usage:~1!

echo usage: %0 [!Usage::=^|!] ["Service Name"]

goto :EOF
 

 :RUN

NET %1 %2

GOTO :EOF
 

:START

echo Start...

GOTO :RUN

 

:STOP

ECHO Stop...

GOTO :RUN
 

:RESTART

CALL :RUN STOP %2

CALL :RUN START %2

GOTO :EOF

 

:PAUSE

ECHO Pause...

GOTO :RUN
 

 :DELCMD

ECHO del "%~dpnx0"

Open in new window

0
 

Author Comment

by:Sharp2b
ID: 22786639
Even better, thanks a lot!
I did notice the fact that all labels were included in the usage but didn't have time to think more about it.
So, your answer preceded the question. Thanks again!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now