Pass arguments of .BAT file to executed .EXE file

1

I need a DOS .bat script that just transfers ALL arguments to a .exe program.

For example the DOS script main.bat that calls the .exe program.exe:

program.exe ????

The question is what ??? should be. The arguments must ALL be passed quoted: if there are filenames with spaces, these must be left intact. Under UNIX/POSIX this is called "quoted array", because the arguments form an array (ARGV[x]), and each argument must be quoted.

Francky Leyn

Posted 2018-11-13T10:52:05.977

Reputation: 57

Question was closed 2018-11-14T10:21:29.450

Hi there, and welcome to SuperUser. We're not a "Please write me a script" kind of website. If you share us your script, we can help you narrow down why things aren't working though. That said, it is a bit hard to understand your goal, and an accompanying script would definitely help with that. – LPChip – 2018-11-13T11:07:09.967

The basic problem is that there are 2 .exe's: one for the 32 bit version, and one for the 64 bit version. Let's call them program32.exe and program64.exe. These names can't be changed at the moment. However, I want to write generic scripts that call on of thes .exe's depending on the installation. I just have to know how in DOS you can pass arguments in a quoted way. I don't need a real script. I need 1 onliner. Eg: if i type: – Francky Leyn – 2018-11-13T11:41:18.873

'main.bat "filename with spaces.jpg"', this must be , for the 64 bit version, reult in the DOS system call 'program64.exe "filename with spaces.jpg"' – Francky Leyn – 2018-11-13T11:44:49.397

Answers

0

You can simply use %* to pass everything that was passed to the .bat file to anything else.

Note that if you pass "words with spaces" to the batch file, it will be seen as 1 parameter in quotes and passed as that. If you ommit the "", the batch will still forward it to the program, but the program will see it as separate parameters. It really depends on how the batch file is called to know if those quotes will be there or not and if needed. For example, if you drag and drop a file in explorer onto the batchfile, explorer will add the quotes for you. If you type manually from the command prompt, its up to the user to check for the quotes.

So:

program.exe %0

should be enough in your case.

Also, from commandline, if you autocomplete filenames with the tab, quotes are automatically added if they are necessary, and even while the quotes may make it seem that you have to edit them out in order to continue typing, you really don't have to. Command prompt is smart enough.

Example: here I type prog, press tab twice, then continue typing and press tab once more.

C:\>_

C:\>cd prog_  (tab)

C:\>cd "Program Files"_  (tab)

C:\>cd "Program Files (x86)"_

C:\>cd "Program Files (x86)"\Micro_    (tab)

C:\>cd "Program Files (x86)\Microsoft Office"_    (enter)

C:\Program Files (x86)\Microsoft Office>_

LPChip

Posted 2018-11-13T10:52:05.977

Reputation: 42 190

If you have a .bat script, must I then return a status code? look at this: – Francky Leyn – 2018-11-15T10:39:17.390

No, you do not need to return a status code. By default, any command in the script will alter the statuscode depending on its outcome, and that is stored in %errorlevel%^. Bat files themselves don't alter this unless you specifically do so, and its not necessary. – LPChip – 2018-11-15T10:45:54.253

Comments are not meant to post code. Your original question has been answered. I suggest posting a new question with the code in a codeblock and we can help you why things are not working as expected. – LPChip – 2018-11-15T11:29:20.743

Only in questions can you post them. A codeblock is written by typing 4 or more spaces at the start of a line. See also the formatting help when you make a new question. – LPChip – 2018-11-15T13:16:48.087