yoshco
yoshco

Reputation: 231

How to get path from a variable

Following How to get folder path from file path with CMD

I want to strip the path (without the filename) from a variable. following the logic of the methods discussed above I would like to use batch bellow, which doesn't work. any takers? possible?

set cpp="C:\temp\lib.dll"
echo %cpp% 
"C:\temp\lib.dll"
echo %~dpcpp
"C:\temp\" > doesn't work

Upvotes: 0

Views: 264

Answers (2)

Patrick Cuff
Patrick Cuff

Reputation: 29786

You can use the for command, like so:

set cpp="C:\temp\lib.dll" 

:: Print the full path and file name:
echo %cpp%  

:: Print just the path:
for %%P in (%cpp%) do echo %%~dpP

Upvotes: 1

PP.
PP.

Reputation: 10864

Tested: demo.bat

@echo off
echo "Setting cpp"
set cpp="C:\temp\lib.dll"

echo "Calling JustGetPath"
call :JustGetPath %cpp%

echo "Returning result"
echo %_RESULT%

echo "Quitting"
goto :eof

:JustGetPath
echo "  +JustGetPath( %1 )"
set _RESULT=%~dp1

echo "  -JustGetPath()"
GOTO :eof

:eof

Outputs the following when run:

"Setting cpp"
"Calling JustGetPath"
"  +JustGetPath( C:\temp\lib.dll )"
"  -JustGetPath()"
"Returning result"
C:\temp\
"Quitting"

See also: http://ss64.com/nt/call.html

Upvotes: 0

Related Questions