批处理文件搜索当前绝对路径以查找文件或目录?
我试图搜索当前目录路径并找到与该路径相邻的某个文件或目录。例如:如果脚本的当前目录是 C:\Temp\Dir1\Dir2\Dir3\Dir4\Dir5\Dir6\Test.bat ,并且如果“jars”是位于 < strong>C:\Temp\jars ,然后向上搜索找到“jars”所在目录。
这就是我实现它的方式,但我想知道是否有更简单/更短的方法来做到这一点?
@echo off
SET TITLE=%~nx0
SET SEARCHFOR=jars\Site.jar
SET MYDIR=%~p0
SET MYDRIVE=%~d0
SET DIRCHAIN=%MYDIR:\= %
:: searches first 4 levels of depth but can be increased if necessary
ECHO Searching in directory chain: %MYDRIVE% %DIRCHAIN%
FOR /F "tokens=1-4 delims= " %%G IN ("%DIRCHAIN%") DO (
if exist %MYDRIVE%\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%%H\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G\%%H\
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%%H\%%I\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G\%%H\%%I
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%%H\%%I\%%J\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G\%%H\%%I\%%J
GOTO APPHOMESET
)
GOTO FAILED
)
:FAILED
ECHO Did not discover location of APPHOME containing %SEARCHFOR%
ECHO Searched no deeper than %MYDRIVE%\%%G\%%H\%%I\%%J
:APPHOMESET
SET JREHOME=%APPHOME%\Javasoft\jre
echo APPHOME is %APPHOME%
echo JREHOME is %JREHOME%
pause
I am trying to search the current directory path and find a certain file or directory that is adjacent to that path. For example: if the current directory of the script is C:\Temp\Dir1\Dir2\Dir3\Dir4\Dir5\Dir6\Test.bat , and if "jars" is a directory located at C:\Temp\jars , then search upwards to find the directory where "jars" is located.
This is how I implemented it but I am wondering if there is an easier/shorter way to do it?
@echo off
SET TITLE=%~nx0
SET SEARCHFOR=jars\Site.jar
SET MYDIR=%~p0
SET MYDRIVE=%~d0
SET DIRCHAIN=%MYDIR:\= %
:: searches first 4 levels of depth but can be increased if necessary
ECHO Searching in directory chain: %MYDRIVE% %DIRCHAIN%
FOR /F "tokens=1-4 delims= " %%G IN ("%DIRCHAIN%") DO (
if exist %MYDRIVE%\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%%H\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G\%%H\
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%%H\%%I\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G\%%H\%%I
GOTO APPHOMESET
)
if exist %MYDRIVE%\%%G\%%H\%%I\%%J\%SEARCHFOR% (
SET APPHOME=%MYDRIVE%\%%G\%%H\%%I\%%J
GOTO APPHOMESET
)
GOTO FAILED
)
:FAILED
ECHO Did not discover location of APPHOME containing %SEARCHFOR%
ECHO Searched no deeper than %MYDRIVE%\%%G\%%H\%%I\%%J
:APPHOMESET
SET JREHOME=%APPHOME%\Javasoft\jre
echo APPHOME is %APPHOME%
echo JREHOME is %JREHOME%
pause
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
思路大致如下:
获取批处理脚本的路径作为当前工作目录。
连接子目录名称。
如果结果路径存在,则返回路径并退出。
如果当前工作目录实质上是根目录,则返回
未找到
并退出。获取当前工作目录的父目录并从步骤 #2 重复。
这里是:
The idea is roughly as follows:
Get the path to the batch script as the current working directory.
Concatenate the subdirectory name.
If the resulting path exists, return the path and exit.
If the current working directory is essentially the root directory, return
Not found
and exit.Get the parent directory of the current working directory and repeat from step #2.
Here goes: