Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Excuse my newbie question but I haven't had to touch bat scripts since the days of the autoexec.bat... and that one was even called from the system directly.

So I wonder how I can pass in arguments to my bat script?

For example I want to create n directories with a certain name. I would like to use a construct such as this one:

for %%N in (1 to $arg1$) do mkdir $arg2$-%%N

As a result I would like to have N directories called

  • name-1
  • name-2
  • name-3

... and so on.

Thanks!

share|improve this question
    
Sorry for the newbie question but I think at least it is in sync with Joels wish to have canonical Q&A on serverfault.com –  raoulsson Sep 2 '09 at 0:00
add comment

3 Answers

up vote 3 down vote accepted

it is just %1 %2 ... %n for argument1, argument2, ..., argumentn

see this for example.

The for loop I did like this:

FOR %%a IN ("filename1" "filename2" "filename3") DO something %%a -clean Lib -log errors >> logfilename
share|improve this answer
    
Looks like it's your lucky day as I am on the system :-) Do you know about the loop too? –  raoulsson Aug 26 '09 at 13:28
    
Lets look. I have a script here... –  StampedeXV Aug 26 '09 at 13:30
    
FOR %%a IN ("filename1" "filename2" "filename3") DO something %%a -clean Lib -log errors >> logfilename –  StampedeXV Aug 26 '09 at 13:32
    
Hm. Your lucky day also I'd say. :) –  StampedeXV Aug 26 '09 at 13:46
    
Looks like it ;-) Thanks! –  raoulsson Aug 26 '09 at 13:48
add comment

Batch files use positional parameter numbers (%1, %2, %3) to indicate arguments. For your example, it would be:

for %%N in (1 to %1) do mkdir %2-%%n

Here is a reference guide.

But the construct of your for loop is incorrect too - there is no "to" keyword in batch files as far as I know. The thing in parentheses is supposed to be a set (such as: 1 2 3 4) - I'm not sure you can loop like you want to do without using labels and goto...

Here is the syntax for the FOR command.

Here is how you would do the loop using labels and goto - its not pretty from a programmer's point of view - but what about a batch file is?


set counter=%1
set count=1
:LOOP
if %count% GEQ %counter% GOTO END
mkdir %2-%count%
set /a count = %count% + 1
goto LOOP
:END
share|improve this answer
    
Yes, the "to" for ranges does not exists... Since i am dealing with numbers, a standard for(i = 0; i < %1; i++) would do... how to? –  raoulsson Aug 26 '09 at 13:55
    
I edited my answer to add an example of how you would do this - like I said, it isn't possible with a FOR loop as far as I know. –  MattB Aug 26 '09 at 14:53
add comment

I think what you want is :

@echo off
for /l %%a in (1,1,%1) do mkdir Folder-%%a
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.