hw-135
hw-135

Reputation: 172

Get absolute path of a file using batch script

I have a Java application that produces an output which is stored in a myfile.txt file using batch script. Now I would like to pass the absolute path of this myfile.txt file to another java application as a command line argument.

So something like:

java -jar "path/to/jar/MyJar.jar" > myfile.txt

<Something to get and store absolute path of myfile.txt>

java -jar "path/to/jar/MyOtherJar.jar" <absolute path of myfile.txt>

Now I found this answer which states the use of %~dpnx1 but I can't understand how to apply this. Any suggestions?

Upvotes: 1

Views: 402

Answers (1)

jeb
jeb

Reputation: 82217

The use of the %~dpnx1 or simply %~f1 syntax requires that the filename is in the arguments.
dpnx=(D)rive (P)ath (N)ame e(X)tension = Full(F)ilename

That can be done via CALL :func <argument> or via FOR.

call :getAbsolutePath resultVar "myFile.txt"
echo %resultVar%
exit /b


:getAbsolutePath <returnVar> <filename>
set "%1=%~f2"
exit /b

Or via FOR

FOR /F "delims=" %%X in ("myFile.txt") DO set "absPath=%%~fX"

Upvotes: 1

Related Questions