#1 17 Nov 2006 05:25

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

Using parameters to launch a batch file

Is it possible to use a parameter to launch your batch file which then will goto a routine part? Im not sure if that is worded right, but heres the example

c:\myscript.cmd

:one
echo 1
more stuff
exit
:two
echo 2
more stuff
exit
:three
echo 3
more stuff
exit

start->run
c:\myscript.cmd "two"

output will be echo 2, more stuff, exit

hope you know what i mean, thanks, and is it possible to do this?


cmd, vbs, ps, bash
autoit, python, swift

Offline

#2 17 Nov 2006 10:19

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,005
Website

Re: Using parameters to launch a batch file

All you need is a goto

@echo off

goto %1
goto sub_error

:one
echo 1
more stuff
exit

:two
echo 2
more stuff
exit

:three
echo 3
more stuff
exit

goto :eof
:sub_error
echo none matched

To be extra sure the labels dont randomly match some other text in the script I usually add a prefix
like
:label_one

then you do
goto label_%1

Offline

#3 18 Nov 2006 05:36

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

Re: Using parameters to launch a batch file

That was nice, it still crashed if it didnt have a parameter when you launched it, but thats ok


cmd, vbs, ps, bash
autoit, python, swift

Offline

#4 18 Nov 2006 10:31

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,005
Website

Re: Using parameters to launch a batch file

^ Ah of course you need to start it with something like

if "%1"=="" goto sub_error
goto %1

Offline

Board footer

Powered by FluxBB