Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the absolute path of file in batch script

I am using batch script to invoke java application. Java app takes an absolute file path as an argument.
I need my batch script to be invoked with the relative or absolute path of the file as an argument and then to pass the absolute path of the file for java execution. Example:

Running script myscript from location c:/folderA

myscript  folderB/file.txt  
myscript  c:/folderA/folderB/file.txt

should be able to get full absolute path c:/folderA/folderB/file.txt in both cases. How do I do that?
Just to be as much concrete as possible: I need only BATCH SCRIPT code to retrieve absolute file path string after passing either relative or absolute path to ti as an argument. Not the actual part when I invoke the java app with it.

like image 918
ps-aux Avatar asked Dec 26 '22 21:12

ps-aux


2 Answers

You can use %~dpnx1, which expands to the drive, path, name and extension of the first argument. Don't forget to quote the name when passing it to another command, though.

like image 140
Joey Avatar answered Jan 05 '23 13:01

Joey


Absolute path in "for...do" statement is %~f{variable_letter}.

Absolute path in procedure call for argument %1 is %~f1 or %~dpnx1 but for an environment variable no simple solution. Workaround is create procedure with set statement e.g (for Windows XP and newer):

@echo off
cd C:\Program Files\Microsoft Games
call :getabsolute "Purble Place\PurblePlace.exe"
echo %absolute%
call :getabsolute "Purble Place"
echo %absolute%
pause
goto :eof

:getabsolute
set absolute=%~f1
goto :eof
like image 24
Olamagato Avatar answered Jan 05 '23 11:01

Olamagato