Start a program with Linux path argument from a batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Start a program with Linux path argument from a batch

#1 Post by sambul35 » 11 Jun 2016 13:57

I need to start a microApache server from a batch with its .conf file path as an argument. The problem is, to be recognized by microApache, the path must be in Linux format with forward slashes, which results in error when running the batch. I can't even find an escape character for "/" forward slash on the web, if such can help. Any combination of extra "" anywhere doesn't seem to work:

Code: Select all

@echo off
start /b "C:\Program Files (x86)\some_program\microapache\mapache.exe" -f "C:/Users/user/AppData/some_program/microApache/httpd_conf.txt"
exit /b

:: Error

The system cannot find the file -f.


Alternatively, I can start from a batch a Windows shortcut with target: "C:\Program Files (x86)\some_program\microapache\mapache.exe" -f "C:/Users/user/AppData/some_program/microApache/httpd_conf.txt", and in this case the argument is processed correctly:

Code: Select all

@echo off
start "" "C:\Program Files (x86)\some_program\microapache\mapache"
exit /b


But starting the (run minimized) shortcut results in microApache window left open or minimized, when it should ideally remain closed, while the server is running. Is there a way to resolve such issue using a batch? It looks like leaving microApache window closed requires using %comspec%, like shown here.
Last edited by sambul35 on 11 Jun 2016 14:24, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Start a program with Linux path argument from a batch

#2 Post by aGerman » 11 Jun 2016 14:21

I guess your problem is caused by the fact that the first parameter of START enclosed into quotes will be used as window title. Try

Code: Select all

start  "" /b "C:\Program Files (x86)\some_program\microapache\mapache.exe" -f "C:/Users/user/AppData/some_program/microApache/httpd_conf.txt"

Regards
aGerman

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Start a program with Linux path argument from a batch

#3 Post by sambul35 » 11 Jun 2016 14:40

Hmmm... I didn't expect that using /b switch isn't enough, but it did the trick. :wink: So it looks like Cmd can recognize forward slashes in arguments containing a Linux path. This syntax works well now from a batch:

Code: Select all

@echo off
start "" /d "C:\Program Files (x86)\some_program\microapache" /b "C:\Program Files (x86)\some_program\microapache\mapache.exe" -f "C:/Users/user/AppData/some_program/microApache/httpd_conf.txt" 2>nul
exit /b

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Start a program with Linux path argument from a batch

#4 Post by aGerman » 11 Jun 2016 15:54

So it looks like Cmd can recognize forward slashes in arguments

Forward slashes have no special meaning unless they are used in order to initiate an option. But even in that case it depends on the command/tool if it is a forward slash, a dash, or whatever the programmers decided to use in their command line parsers.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Start a program with Linux path argument from a batch

#5 Post by sambul35 » 11 Jun 2016 16:48

The above suggestion however only addresses my 1st question about not recognizing the program path. What about my 2nd question about keeping microApache Cmd window closed while starting it from a batch called for example by Task Scheduler or double click? Now microApache window gets open, even if start /b option is used in the batch, and if closed manually, microApache server exits. Can %comspec% be used for this in the batch, or any other approach?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Start a program with Linux path argument from a batch

#6 Post by aGerman » 11 Jun 2016 17:07

As I understood your microApache application runs in a console window. The window is open as long as the application runs. If you close the window the process will be terminated. That's normal behavior for console applications. START /B only means that the started console application runs in the same window as your batch script. The window remains opened until the last process (that uses this window) quits.
Thus, if you want to close the window it would also lead to quit microApache. You could hide the window using a hybrid script. But how would you quit the application if you're finished?

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Start a program with Linux path argument from a batch

#7 Post by sambul35 » 11 Jun 2016 17:35

MicroApache is controlled by another app service. But at times I need to re-start microApache in the background from Task Scheduler, if it fails to start due to target disk being inaccessible. I wonder if microApache has a commandline option to start in the background, so no hybrid batch is needed for that? Alternatively, how such hybrid may look like?
Last edited by sambul35 on 12 Jun 2016 07:25, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Start a program with Linux path argument from a batch

#8 Post by aGerman » 11 Jun 2016 18:11

sambul35 wrote:I wonder if microApache has a commandline option to start in the background, so no hybrid batch is needed for that?

Wasn't there any manpage that describes command line options?

sambul35 wrote:Alternatively, how such hybrid may look like?

Even if I can't test it I think that should work

Code: Select all

@if (@a)==(@b) @end /* Batch

@echo off &setlocal
cscript //nologo //e:jscript "%~fs0"
exit /b

:: JScript */

var objWShell = new ActiveXObject('WScript.Shell');
objWShell.Run('cmd.exe /c "start "" /d "C:\\Program Files (x86)\\some_program\\microapache" /b "C:\\Program Files (x86)\\some_program\\microapache\\mapache.exe" -f "C:/Users/user/AppData/some_program/microApache/httpd_conf.txt" 2>nul"', 0, false);

That code should be saved as .bat. You could also use only the last two lines and save as .js in order to avoid the Batch window flashing up.

Regards
aGerman

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Start a program with Linux path argument from a batch

#9 Post by sambul35 » 11 Jun 2016 19:00

Yes, there's a manpage with options, but... using them doesn't appear productive. I tried to run this, but it gives errors, since microApache is not registered as service in this application:

Code: Select all

%img% -d %dir% -n mapache.exe -f %cfg% -k restart

:: error
 No installed service named "mapache.exe".

As microApache is controlled by another service app, that app seems to have some additional options (directives) or extra code to keep it in the background. Though using a 2nd user account to start microApache in the background from Task Scheduler is mentioned here with option "Run whether user is logged on or not", as well as starting the exe with Powershell or VBS script from the batch to hide its window.

Since my batch that restarts microApache also does some other unrelated things, can you explain what the 1st line of your code does, may be with an example how to integrate your peace in a larger batch? :wink:
Last edited by sambul35 on 12 Jun 2016 07:38, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Start a program with Linux path argument from a batch

#10 Post by aGerman » 12 Jun 2016 05:31

can you explain what the 1st line of your code does

The goal is not having crashes or error messages if you run the same code twice - 1st as Batch, 2nd as JScript. In order to exclude the JScript portion from executing as Batch you simply have to quit the Batch code either with EXIT /B or GOTO :EOF beforehand.

Code: Select all

:: some Batch code
exit /b
Whatever here is was never seen.

To exclude the Batch portion from parsing as JScript you have to mark it as comment. That can be done with /* ... */ also for a block of multiple lines.

Code: Select all

/*
Whatever here is was never seen.
*/
// some JScript code

But now we have a /* in the first line that causes an error if parsed as Batch code. What we need is a harmless piece of code that is valid in both Batch and JScript. The only reason for having it is to initiate a JScript comment. Line ...

Code: Select all

@if (@a)==(@b) @end /*

... is a simple IF statement in Batch. Since (@a)==(@b) is FALSE the rest of the line was never seen.
The same line in JScript is a "Conditional Compilation Statement" that is always finished with an @end statement. It does absolutely nothing, JScript sees the /* at the end of the line and recognizes it as beginning of a comment though.

Thus, the structure for a JScript hybrid is simple.

Code: Select all

@if (@a)==(@b) @end /* JScript comment starts here
:: some Batch code here
:: pass this file to cscript.exe in order to run it as JScript:
cscript //nologo //e:jscript "%~fs0"
:: some Batch code there
:: quit Batch code execution:
exit /b

end of JScript comment: */

// some JScript code here

That's it :wink:

Regards
aGerman

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Start a program with Linux path argument from a batch

#11 Post by sambul35 » 12 Jun 2016 17:25

@aGerman

Thanks for examples and detail explanation. Your script works very fast. It also serves as a simple intro to hybrid code batching. :)

The script directly works with the batch variables as well:

Code: Select all

@if (@a)==(@b) @end /* Batch

@echo off &setlocal
set "img=C:\\Program Files (x86)\\some_program\\microapache\\mapache.exe"
set "dir=C:\\Program Files (x86)\\some_program\\microapache"
set "conf=C:/Users/user/AppData/Roaming/Contaware/some_program/httpd_conf.txt"

cscript //nologo //e:jscript "%~fs0"
exit /b

:: JScript */

var objWShell = new ActiveXObject('WScript.Shell');
objWShell.Run('cmd.exe /c "start "" /d "%dir%" /b "%img%" -f "%conf%" 2>nul"', 0, false);

Post Reply