Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 提取路径直到最后一次出现"\&引用;成批_Batch File_Path - Fatal编程技术网

Batch file 提取路径直到最后一次出现"\&引用;成批

Batch file 提取路径直到最后一次出现"\&引用;成批,batch-file,path,Batch File,Path,任务很简单:给定路径:C:\Auto\proj输出:C:\Auto。i、 e.提取给定路径,直到但不包括批处理中最后出现的“\”。尝试以下操作: set "givenpath=C:\Auto\proj" for /f "tokens=1,2 delims=\" %%a in ("%givenpath%") do set "givenpath=%%a\%%b" echo %givenpath% 试试这个: @echo off setlocal EnableDelayedExpansion s

任务很简单:给定路径:C:\Auto\proj输出:C:\Auto。i、 e.提取给定路径,直到但不包括批处理中最后出现的“\”。

尝试以下操作:

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

@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%"

什么:设置“parentPath=%%~dpf”?
%%f
(循环变量)包含
%givenPath%
,即
C:\Auto\proj
%~dpf
只是驱动器(
d
)和路径
p
部分,即
C:\Auto\
。下一行中的字符串替换将删除尾随的反斜杠。因为变量替换仅适用于循环变量和命令行参数。对