In a CMD batch file, it's easy to extract the path/folder name for an argument:
set folder=%~f0
set dirname=%~p0
It's described in help call and help for.
Now how do I do the same with a path stored in a variable?
The following doesn't work:
set VAR="c:\Program Files (x86)\Java\jdk1.6.0_29\bin\java.exe"
echo %~pVAR%
The alternative I found is to use either FOR or CALL:
for %%A in (%VAR%) do set P=%%~pA
for %%A in (%VAR%) do set N=%%~nA
for %%A in (%VAR%) do set E=%%~xA
and then use %P% (c:\path), %N% (appname), %E% (.exe) as I need.
For example, I recently needed to convert a java.exe variable to map to javaw.exe instead, so I end up writing this:
if %java_exe%=java (
set javaw_exe=javaw
) else (
for %%a in (%java_exe%) do set p=%%~pa
for %%a in (%java_exe%) do set n=%%~na
for %%a in (%java_exe%) do set x=%%~xa
set n=%n:java=javaw%
set javaw_exe=%p%%n%%x%
)
if not exist %javaw_exe% set javaw_exe=%java_exe%
Can someone shine in with a better solution that doesn't look totally hackish?