1.

Solve : is there a way to make windows batch files accept arguments??

Answer»

is there a way to make windows batch files accept arguments?
I know you can use %1 and so on, but I'd like to be able to use -a -g /f and all of those.

//RG0DYou can PROBABLY just use a variable to hold all of the arguments as well:

Code: [Select]set arg=command -a -g /f
...
call newprogram %arg%

What exactly is it you are trying to do that would require you to pass arguments?Well, I was thinking of trying to convert the linux cowsay command to a windows version (mostly for fun), and then, depending of the parameter, change the apperance of the ASCII image slightly. I can build the pictures and the input system, but I don't know how to get the aguments working.The easiest way is to have the receiving program accept more parameters and set them up in the right places.

Code: [Select]set arg1=/s
set arg2=/b

dir.bat %arg1% %arg2%

>>dir.bat<<
Code: [Select]dir %1 %2
With this, you can probably see that you can pass the command if you wanted to as well. For what you are trying to do, it MAY be easier to set up a series of IF tests to check the parameters being passed and then change the appearance based on that. It can be done, though it may require some extra work to do so.Wait, I just want to make sure if I got it?

If the code to activate will be like this:
Code: [Select]C:\> Mybatchfile -a
that would start Mybatchfile.bat and tell it "Here's a parameter called -a, What would you like to do with it?"Oh, I just got an answer in my PM inbox too. Now I think I've got it. Thx for your help guys!

PM from David Murphy:
Quote

C:\test>type argument.bat
echo off

rem argument can be passed to a batch file.
rem argument.bat one two

echo argument %1

echo argument %2

Output:

C:\test>argument.bat  one two
argument one
argument two

C:\test>
He is a Troll...and has been banned from the Forums...

Take the advice with a grain of salt. Quote from: Radiation G0D on OCTOBER 12, 2011, 10:03:16 AM
Wait, I just want to make sure if I got it?

If the code to activate will be like this:
Code: [Select]C:\> Mybatchfile -a
that would start Mybatchfile.bat and tell it "Here's a parameter called -a, What would you like to do with it?"

Exactly. That's how arguments are passed to other programs. It's up to the receiving program on how to use the arguments passed.I found this in the snippet closet. It mimics the VBScript named arguments feature. More of a demo, but it shows method and technique.

Code: [Select]echo off
setlocal
if .%1 EQU . goto Help

:ParseTheArgs
  if .%1 EQU . goto UserCode
  set argNamed=%1
  if "%argNamed:~0,1%" NEQ "/" echo Parameter Error [%argNamed%]...Job Canceled & goto :eof
  set argNamed=%argNamed:~1%
  for /f "tokens=1,2 delims=:" %%i in ("%argNamed%") do set %%i=%%j
  shift
  goto ParseTheArgs
 
:UserCode
  if defined month echo %month%
  if defined day echo %day%
  goto :eof
 
:Help
  echo.
  echo Named arguments have this format: /varName:varValue
  echo Exsmple: %0 /month:NOVEMBER /day:Thursday
  goto :eof

It allows variable names and values to be passed into the batch file. The batch file must be coded to handle the input, but the parameters are passed by name not position.

If you run the snippet with no parameters, a mini help screen will show the correct syntax to use.

Hope this helps.


Discussion

No Comment Found