Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2008-10-21
12
1,079 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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
 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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