将路径提取到批处理中最后一次出现的“\”

时间:2013-07-23 21:53:27

标签: batch-file path

任务很简单:给定路径:C:\ Auto \ proj输出:C:\ Auto。即提取给定路径,直到但不包括批次中最后一次出现的“\”。

2 个答案:

答案 0 :(得分:1)

试试这个:

set "givenpath=C:\Auto\proj"
for /f "tokens=1,2 delims=\" %%a in ("%givenpath%") do set "givenpath=%%a\%%b"
echo %givenpath%

答案 1 :(得分: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%"