如何从变量获取路径

时间:2010-06-03 08:25:37

标签: windows batch-file cmd

关注How to get folder path from file path with CMD

我想从变量中删除路径(没有文件名)。按照上面讨论的方法的逻辑,我想使用批处理波纹管,这是行不通的。任何接受者?可能的?

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

2 个答案:

答案 0 :(得分:1)

您可以使用for命令,如下所示:

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

答案 1 :(得分:0)

经过测试: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

运行时输出以下内容:

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

另请参阅:http://ss64.com/nt/call.html