tags:

views:

45

answers:

2

Following http://stackoverflow.com/questions/659647/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
A: 

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

PP
A: 

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
Patrick Cuff
I tried this without success - admittedly it was on WINE on Linux so perhaps the interpreter hadn't been fully compatible with Microsoft Windows...
PP
@PP: Hmmmm, this works on my XP, Windows 7, Server 2003 and Server 2008 boxes.
Patrick Cuff
works on win7, i like this but learning the CALL way was a mind opener for me, so i give it the V
yoshco