简体   繁体   English

获取当前路径的一部分

[英]Get a part of the current path

I would like to get a part of the current directory where my batch script is from.我想获得我的批处理脚本所在的当前目录的一部分。

The location is something like this: Y:\abc\def\ghi\jkl\script.bat位置是这样的:Y:\abc\def\ghi\jkl\script.bat

I just want to keep what's after Y:\abc\def\ (that is \ghi\jkl)我只想保留 Y:\abc\def\ 之后的内容(即 \ghi\jkl)

How to do this?这个怎么做?

I'm using the code below for getting the full path but how to make a delimitation?我正在使用下面的代码来获取完整路径,但如何进行分隔?

for /f %%a in ("%CD%") do set CURR=%%a
echo %CURR%

Thank you for your precious help.感谢您的宝贵帮助。

I believe that maybe you could put the section to be cut inside a txt file and then manipulate the string from the loop in the file, like this:我相信也许您可以将要剪切的部分放在 txt 文件中,然后从文件中的循环中操作字符串,如下所示:

echo %cd% > path.txt

for /f "tokens=3,* delims=\" %%a in (path.txt) do echo %%b

Determining the depth with the argument tokens=3 with the delimiter character being "\" .使用参数tokens=3确定深度,分隔符为"\"

Based upon your stated "directory where my batch script is from", the following should suffice, (the last line is added for demonstration purposes, please change it as necessary) :根据您所说的“我的批处理脚本来自的目录”,以下内容就足够了(最后一行是出于演示目的,请根据需要进行更改)

@Set "x=%~dp0"&SetLocal EnableDelayedExpansion
@Set "i=0"&Set "x!i!=%x:\="&Set /A i+=1&Set "x!i!=%"
@Set /A i-=1,y=i-1
@If %i% Lss 1 (Set "z=%x0%\")Else (If %i% Equ 1 (Set "z=%x0%\%x1%"
        )Else Set "z=!x%y%!\!x%i%!")
@EndLocal&Set "y=%z%"
@Echo %x% becomes %y%&Pause

I have made it so that if the scripts directory isn't deep enough, the full path will still be output.我已经做到了,如果脚本目录不够深,完整路径仍然是 output。

If you want to use the current directory instead of the scripts location, change %~dp0 on line 1 to %__CD__% or %CD%\ as needed.如果要使用当前目录而不是脚本位置,请根据需要将第1行的%~dp0更改为%__CD__%%CD%\

@echo off
setlocal

set "reversed="
set "fromdir=%~dp0"

for %%A in ("%fromdir:\=" "%") do call set "reversed=%%~A\%%reversed%%"
for /f "tokens=1-2 delims=\" %%A in ("%reversed%") do set "result=\%%B\%%A"

echo %result%
pause

If the path segments can be reversed, then getting the last 2 segments is a known number for setting the tokens option of 1-2 as they would become the 1st 2 segments.如果路径段可以反转,那么获取最后 2 个段是一个已知数字,用于设置1-2的标记选项,因为它们将成为第 2 个段。 The 1st for loop does the reversing.第一个for循环进行反转。 The 2nd for loop gets the 1st 2 tokens and is set to result in reverse order, which is the original order.第二个for循环获取第一个 2 个标记并设置为result顺序,即原始顺序。

The fromdir is set for the script directory %~dp0 , though it can be set with %cd% if is wanted. fromdir是为脚本目录%~dp0设置的,但如果需要,可以使用%cd%设置。

View set /?视图set /? for how "%fromdir:\=" "%" does replacement of \ with " " so that the path segments become individual arguments ie "C:\dir1\dir2\dir3" becomes "C:" "dir1" "dir2" "dir3" .关于"%fromdir:\=" "%"如何用" "替换\以便路径段成为单独的 arguments 即"C:\dir1\dir2\dir3"变为"C:" "dir1" "dir2" "dir3" .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM