Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


Passing a variable to and returning a value from a called batch script

  • Please log in to reply
1 reply to this topic

#1 Will B.

Will B.

  • Members
  • 5 posts
  • Local time:05:23 AM

Posted 09 February 2010 - 12:35 PM

How do you pass a variable (or possibly more than one) to a called batch script?

An example of what I am trying to do is(in sudo code where // signifies a comment):

//assigns the value E: to variable DriveLetter

OS =call "GetOS.bat" \DriveLetter
//Returns either V for Vista, X for XP, 7 for Windows 7, or 0 if no OS is found on the
//drive DriveLetter wich in this case would be E:
//(I don't need the code for how to do this check, just how to pass the variable to and from)

if OS != 0 DO call "Cleanup%OS%.bat" \DriveLetter
//Runs one of 3 cleanup scripts name CleanupX.bat CleanupV.bat and Cleanup7.bat
//on the drive DriveLetter

I hope what I am trying to accomplish is clear, if not I would be more than happy to elaborate...

BC AdBot (Login to Remove)


#2 Romeo29


    Learning To Bleep

  • Members
  • 3,194 posts
  • Gender:Not Telling
  • Location:
  • Local time:06:23 AM

Posted 10 February 2010 - 10:59 AM

The arguments are taken as %1 %2 %3 and so on inside the called batch file.

Create a batch file sample.bat with following contents : @echo Hello %1. Your age is %2 years.
Now in the command prompt pass arguments like this: sample.bat Billy 16
This would produce an output like : Hello Billy. Your age is 16 years.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users