user1411397
user1411397

Reputation: 31

how to give relative path in a batch file?

I am facing an issue with below batch script where i have to give a relative path of the hard-coded path in my batch file.

below is my soapUI cmd line execution file where soapui-settings.xml is the file which has all my soap settings and project.xml is the one with my testcases. I have hard-coded path here. since i am going to check in this file, if any other person execute this file will not work because the path will not exist on their machine. How do i achieve that on windows? Is there a way I can use a relative path to hard-coded one in my batch file and run it??

here is my sample file:

cd C:\soapui4.5\soapUI-Pro-4.5.0\bin  
testrunner.bat -tC:\Users\jvihol\soapui-settings.xml C:\Users\jvihol\Documents\April-RTM-soapui-project.xml

any help would be really appreciated. thanks. :)

Upvotes: 3

Views: 24190

Answers (3)

ixe013
ixe013

Reputation: 10171

Here is the trick I use to solve changing paths. In short,

  1. Make all path relative to the batch file location, and
  2. Make the batch file change its own working directory.

It helps if the tools you call are in the path, or in a location defined by an environment variable.

Something like this :

@echo off

pushd %~dp0

REM Here you are executing in the same directory as the batch file
REM You can make your path relative to here

popd

For your project, you can use the same %~dp0 as a place holder for the absolute path.

pushd C:\soapui4.5\soapUI-Pro-4.5.0\bin 
testrunner.bat -EDefault -I -t%~dp0soapui-settings.xml %~dp0April-RTM-soapui-project.xml
popd

Upvotes: 5

Andriy M
Andriy M

Reputation: 77677

Maybe you want this:

cd C:\soapui4.5\soapUI-Pro-4.5.0\bin  
testrunner.bat -t%USERPROFILE%\soapui-settings.xml %USERPROFILE%\Documents\April-RTM-soapui-project.xml

USERPROFILE is a system environment variable containing the path to the current user's home directory. In your session it will evaluate to

C:\Users\jvihol

and in someone else's, to

C:\Users\someone else's user name

Upvotes: 1

Chuck Kollars
Chuck Kollars

Reputation: 2175

Getting an absolute path from a relative path requires somebody to do some calculation. The three options I know of are: i) an add-on program that does nothing but path calculations, ii) use the "current directory", and iii) smash the two paths together. Here are rough illustrations of methods ii) and iii):

REM example "givens"
set DRIVE=C:
set ROOTPATH=\fee\fie\fo
set RELPATH=funky\stuff
set FILENAME=blarf.txt

REM method ii) using the "current directory" functionality
%DRIVE%
cd %ROOTPATH%
cd %RELPATH%
more %FILENAME%

REM method iii) using explicit concatenation
set FULLPATH=%DRIVE%%ROOTPATH%\%RELPATH%
set PATHFILENAME=%FULLPATH%\%FILENAME%
more %PATHFILENAME%

REM DOS/BAT handling of drive letter is odd (is it part of the path, or not?)
REM It may be necessary to use "cd /D ..."

REM Path calculations are easier 
REM so long as DOS/BAT understands that "\\" is the same as "\"

Upvotes: 1

Related Questions