任务很简单:给定路径:C:\Auto\proj 输出:C:\Auto。即提取给定路径,直到但不包括批处理中最后一次出现的“\”。
问问题
569 次
2 回答
1
尝试这个:
set "givenpath=C:\Auto\proj"
for /f "tokens=1,2 delims=\" %%a in ("%givenpath%") do set "givenpath=%%a\%%b"
echo %givenpath%
于 2013-07-23T22:12:09.477 回答
0
尝试这个:
@echo off
setlocal EnableDelayedExpansion
set "givenPath=C:\Auto\proj"
for %%f in (%givenPath%) do (
set "parentPath=%%~dpf"
set "parentPath=!parentPath:~0,-1!"
)
echo %parentPath%
另外的选择:
@echo off
setlocal
set "givenPath=C:\Auto\proj"
call :Dirname "%givenPath%"
goto :eof
:Dirname
set "parentPath=%~dp1"
set "parentPath=!parentPath:~0,-1!"
echo %parentPath%"
或者您可以将路径作为命令行参数传递:
@echo off
setlocal
set "parentPath=%~dp1"
set "parentPath=!parentPath:~0,-1!"
echo %parentPath%"
于 2013-07-23T22:17:09.233 回答