views:

126

answers:

3

I'm aware of %~dp0, which represents the fully expanded directory that contains the batch script, but what I'm looking for is either:

  • a method to invoke the batch script from Perl in such a manner that allows me to use the batch script without modification and have all directories in it be in relation to the batch script location
  • a single statement I can put in the batch script that is a flag to use the location of the batch script as the starting point for all directories

I'm currently invoking the batch script using this method in Perl:

`"../run.bat" -f $ARGV[$#ARGV]`;

and then capturing the output and doing processing to it.

+4  A: 

I am not quite sure I understand your question. But why don't you simply change the current directory in your perl script to the batch script's directory and then call the batch script?

innaM
I feel like an idiot for not even thinking of this...it's perfect.
Thomas Owens
+3  A: 

Use pushd and popd. Add the line

@pushd %~dp0

to the beginning of the batch script. This will change the working directory to the base directory of the batch file. For the sake of completeness (and in case the batch file is going to be used by other batch files), you should add

@popd

at the end.

Pesto
A: 

Here's a bat script that starts a catalyst server perl script contained in the location relative to the .bat script: MyPhp/script/

set bindir=%~dp0
set perlpath=%bindir%perl\bin
set buildpath=%bindir%\bin
set PATH=%PATH%;%perlpath%;%buildpath%
"%perlpath%\wperl.exe" "%bindir%MyPhp\script\myphp_server.pl" -p 35900

Here's a VBS script to start this same script without spawining a cmd window:

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) &  fso.GetParentFolderName(wscript.ScriptFullName) & "\perl shell.bat"& Chr(34), 0
Set WshShell = Nothing

Really horrible stuff, but it works

singingfish