Newbie: reading in arguements

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 32
Joined: 17 Mar 2014 08:43

Newbie: reading in arguements

#1 Post by OM3 » 03 Nov 2014 22:20

I have a batch file that I've put together that an ISO image

I want the ability to add an extra parameter
Which if (and ONLY if) it's there and it's a number, it adds itself to the batch file code

Specifically, the code is:

Code: Select all

"c:\Program Files\ImgBurn\ImgBurn.exe" /MODE write /SRC "E:\my iso images\abcdef.iso" /DEST D: /COPIES 1 /EJECT YES /START /CLOSESUCCESS /WAITFORMEDIA 


Let's say the file is called my-iso.bat

If I just type my-iso.bat, then the above code gets run

But... if I type "myiso.bat 3", then "/COPIES 1" becomes "/COPIES 3"

So, I need code that says, if no arguement is supplied, then use "/COPIES 1" (or actually... can just leave out)

I'm not sure what the code is to check for an arguement...
I can write code... but the DOS code seems to be a programming language of its own :)

Any help would be most appreciated

Thanks


OM

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Newbie: reading in arguements

#2 Post by ShadowThief » 03 Nov 2014 22:52

OM3 wrote:the DOS code seems to be a programming language of its own :)

That's because it is :D

Arguments to the script are in the format %n, where n is a number between 1 and 9. %1 is the first argument, %2 is the second argument, and so on. (There is also a %0, but that refers to the full path of the script itself.)

Code: Select all

@echo off
setlocal enabledelayedexpansion

:: Check that there is an argument. If there is not, use the default value of 1.
set arg=%1
if "%arg%"=="" (
   set copies=1
) else (
   :: Ensure that the parameter is a number
   for /f "tokens=1 delims=1234567890" %%A in ("%arg%.") do set checknum=%%A
   if "!checknum!"=="." (
      set copies=%arg%
   )
)

:: Run ImgBurn
start "" "c:\Program Files\ImgBurn\ImgBurn.exe" /MODE write /SRC "E:\my iso images\abcdef.iso" /DEST D: /COPIES %copies% /EJECT YES /START /CLOSESUCCESS /WAITFORMEDIA
Last edited by ShadowThief on 05 Nov 2014 03:53, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Newbie: reading in arguements

#3 Post by foxidrive » 04 Nov 2014 00:22

At a very simple level you could use something like this:

It has no errorchecking as in the above, but it's a little easier to see how this can work.

Code: Select all

@echo off
set num=1
if not "%~1"=="" set num=%1
"c:\Program Files\ImgBurn\ImgBurn.exe" /MODE write /SRC "E:\my iso images\abcdef.iso" /DEST D: /COPIES %num% /EJECT YES /START /CLOSESUCCESS /WAITFORMEDIA

OM3
Posts: 32
Joined: 17 Mar 2014 08:43

Re: Newbie: reading in arguements

#4 Post by OM3 » 04 Nov 2014 09:58

@foxidrive @ShadowThief
awesome
thanks for both solutions
u guys are just pure genius :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Newbie: reading in arguements

#5 Post by foxidrive » 04 Nov 2014 22:10

ShadowThief wrote:

Code: Select all

start "" "c:\Program Files\ImgBurn\ImgBurn.exe" /MODE write /SRC "E:\my iso images\abcdef.iso" /DEST D: /COPIES %arg% /EJECT YES /START /CLOSESUCCESS /WAITFORMEDIA


I just noticed that the %arg% should be %copies% methinks

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Newbie: reading in arguements

#6 Post by ShadowThief » 05 Nov 2014 03:53

Oh, geez. What in the world possessed me to use arg? Thanks...

Post Reply