Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how error-trap for missing argument?

215 views
Skip to first unread message

Rick Charnes

unread,
Sep 13, 2005, 4:14:14 PM9/13/05
to
How do I code in a .CMD file: if this .CMD file is run without an
argument ('%1'), display an error and then abort? Thanks much.

heath...@gmail.com

unread,
Sep 13, 2005, 5:01:33 PM9/13/05
to

Rick Charnes wrote:
> How do I code in a .CMD file: if this .CMD file is run without an
> argument ('%1'), display an error and then abort? Thanks much.

Try this:

@ECHO OFF
if [%1] == [] goto error
rem your batch code goes here
goto :eof
:error
for /l %%i in (1,1,12) do echo.
echo There has been an error!
for /l %%i in (1,1,11) do echo.
ping localhost >nul

foxidrive

unread,
Sep 13, 2005, 6:01:14 PM9/13/05
to

Or this

@echo off
if [%1]==[] echo an error&pause&goto :EOF

William Allen

unread,
Sep 13, 2005, 6:26:56 PM9/13/05
to
"Rick Charnes" wrote in message

> How do I code in a .CMD file: if this .CMD file is run without an
> argument ('%1'), display an error and then abort? Thanks much.

You can use a simple IF test, but this can be problematical should
the parameter may contain "quoted strings" including [Space]s or
other delimiters. This variant may help avoid such problems:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL&(SET CHECKpar=%1)
IF NOT DEFINED CHECKpar (ECHO. Parameter is needed.&GOTO :EOF)
ECHO. Parameter supplied was: %1

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/


Timo Salmi

unread,
Sep 13, 2005, 10:49:57 PM9/13/05
to
Rick Charnes <rickxyz--nosp...@yahoo.com> wrote:
> How do I code in a .CMD file: if this .CMD file is run without an
> argument ('%1'), display an error and then abort? Thanks much.

From 93) How do I check if my script was given a parameter?
133666 Sep 3 2005 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

Back to the very basics, with some additions. Below is an example
with three different options, demonstrating an important pitfall
("") and a useful feature (~) on the side

@echo off & setlocal enableextensions
::
if not [%1]==[] echo %1
if not [%1]==[] echo %~1
::
set par1=%1
if defined par1 echo %1
::
if not "%1"=="" echo %1
::
endlocal & goto :EOF

Example output
C:\_D\TEST>cmdfaq "a b"
"a b"
a b
"a b"
b""=="" was unexpected at this time.

The option, also pointed to by William (Allen), is the safest
set par1=%1
if defined par1 echo %1

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

0 new messages