如何获得目录路径的一部分在批处理文件 [英] How to get a Part of the Directory path in a batch file
问题描述
我有一个目录中的一个BAT文件
I have a BAT file in a directory
D:\dir1\dir2\getpath.bat
当我运行与下面$ C $蝙蝠c将其打印
when i run the bat with the below code it prints
D:\dir1\dir2\
我想只有路径 D:\\ DIR1 \\
的目录结构是不固定的,需要比在其中BAT文件所驻留的当前目录以外的完整目录路径。
The directory structure is not fixed , need the complete directory path other than the current directory in which BAT file resides.
@echo off
SET SUBDIR=%~dp0
ECHO %SUBDIR%
试图用delims在for循环中,但它没有帮助。
tried using delims in a for loop but it didnt help.
推荐答案
如果这是你的脚本驻留在你想要的目录的父目录,然后试试这个:
If it's the parent directory of the directory your script resides in you want, then try this:
@echo off
SET batchdir=%~dp0
cd /D "%batchdir%.."
echo %CD%
cd "%batchdir%"
(未经测试,请评论,如果有问题)
(untested, please comment if there are problems)
请注意,这将,当然,如果你的批量驻留在您的驱动器根目录改变什么(如 F:\\
);)如果你想要一个特殊的输出,如果是那样的话,你应该回声之前测试%CD%
对%batchdir%
。
Note that this will, of course, change nothing if your batch resides in your drive root (as in F:\
) ;) If you'd like a special output if that's the case, you should test %CD%
against %batchdir%
before the echo.
编辑:应用补丁,请参阅@RichardA评论
Applied patch, see comment by @RichardA
这篇关于如何获得目录路径的一部分在批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!