5

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.

ps-aux
  • 11,627
  • 25
  • 81
  • 128
  • Does this answer your question? [Resolve absolute path from relative path and/or file name](https://stackoverflow.com/questions/1645843/resolve-absolute-path-from-relative-path-and-or-file-name) – Melebius Jun 18 '21 at 10:03

2 Answers2

11

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.

Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
Joey
  • 344,408
  • 85
  • 689
  • 683
  • Sweet. This is exactly what I was looking for, but much simpler than expected. Thanks. – ps-aux Sep 05 '13 at 17:19
  • `%~dpnx1` is exactly equivalent to `%~f1`. Either (attempts to) return a fully-resolved absolute path to whatever was given as the first argument of a batch file. To get the fully-resolved path to the currently executing batch file itself, use `%~f0`. – Glenn Slayden Nov 18 '22 at 05:56
2

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
Olamagato
  • 21
  • 1