Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I emulate a "wrapper script" on Windows?

Tags:

windows

cmd

dos

On Linux, when I want to prepare the environment of a program before starting it, I'm using a wrapper script:

 export JAVA_HOME=...
 cd /some/folder
 exec ./tool "$@"

How would I do the last line, especially the "$@", on Windows?

Note: I'm wrapping commands which take a lot of arguments (like java or mvn), so I need a solution which works for > 10 arguments.

Being able to exec would be nice but is only a minor concern.

like image 324
Aaron Digulla Avatar asked Sep 27 '26 12:09

Aaron Digulla


1 Answers

Probably something like:

@echo off
setlocal enableextensions
set JAVA_HOME=...
cd ...
start "" .\sometool %*
endlocal

Setlocal localizes environment changes to the script (see setlocal /? for details), and %* means 'all script parameters'. The start command is a cmd.exe command that spawns an executable.

The start command has odd parsing rules; the first quoted string on its command line is considered to be a console window title, which is superfluous if you are not spawning a console window. This means that a command like this won't work as expected:

start "C:\Program Files (x86)\Microsoft Office\Office14\excel.exe"

If you run this command, you will spawn a new instance of cmd.exe with the quoted string as its console window title, which is clearly not what was intended. Instead, you have to write this:

start "" "C:\Program Files (x86)\Microsoft Office\Office14\excel.exe"
like image 66
Bill_Stewart Avatar answered Sep 29 '26 04:09

Bill_Stewart