tags:

views:

58

answers:

2

Hello,

When, for example, i want a batch file to 'open' a file. when i for example drag and drop the file into the batch file, it should do some stuff with that file.

Now, i need to know the variable. I know there is a variable for this kind of stuff; i just forgot it.

Can someone give me the variable please?

Thanks.

+3  A: 

The first 9 parameters given to a batch file can be accessed by writing %1 through %9.

The complete command line argument is stored in %*.

For more information, see here.

SLaks
Thanks! works fine.
YourComputerHelpZ
Also you can use `shift` to remove `%1` and shift every remaining argument by one place. That way you can access arguments which would have been at place 12 or so before.
Joey
A: 

As above; also, don't forget that it might be a good idea to enclose the variable in double quotation marks (e.g. "%1"), since its value might contain whitespace characters.

stakx
i know, thanks for the reminder.
YourComputerHelpZ
If the arguments contain whitespace, then the quotes (which are mandatory, otherwise you couldn't pass it) are part of the `%1`. Another layer of quotes around it doesn't accomplish anything.
Joey
@Johannes: Some quick checks suggest that quotation marks are indeed superfluous. Nevertheless, I have often run into problems because of missing quotation marks. It appears to me that Windows/CMD does not always treat quotation marks the same way, therefore that's one of the first things I check when a batch file doesn't work as expected.
stakx
Agreed. The most prominent example where quotes are *not* automatically there is probably the `for` command. So putting quotes around `%%x` and not around `%1` is the correct way. And, of course, you'd have to put quotes around `%~1` again.
Joey
I didn't realise that there's actually a clear pattern about quote usage. I'll have to look deeper into this. Thank you for these hints!
stakx