批处理文件搜索当前绝对路径以查找文件或目录?

发布于 2024-12-06 18:48:02 字数 1295 浏览 0 评论 0原文

我试图搜索当前目录路径并找到与该路径相邻的某个文件或目录。例如:如果脚本的当前目录是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浸婚纱 2024-12-13 18:48:02

思路大致如下:

  1. 获取批处理脚本的路径作为当前工作目录。

  2. 连接子目录名称。

  3. 如果结果路径存在,则返回路径并退出。

  4. 如果当前工作目录实质上是根目录,则返回未找到并退出。

  5. 获取当前工作目录的父目录并从步骤 #2 重复。

这里是:

@ECHO OFF

SET "subdir=%~1"
SET "dir=%~f0"

:loop
  CALL :getdir "%dir%"

  IF EXIST "%dir%\%subdir%\" (
    ECHO %dir%\%subdir%
    GOTO :EOF
  )

  IF "%dir:~-1%" == ":" (
    ECHO Directory "%subdir%" not found.
    GOTO :EOF
  )
GOTO loop

:getdir
SET "dir=%~dp1"
SET "dir=%dir:~0,-1%"

The idea is roughly as follows:

  1. Get the path to the batch script as the current working directory.

  2. Concatenate the subdirectory name.

  3. If the resulting path exists, return the path and exit.

  4. If the current working directory is essentially the root directory, return Not found and exit.

  5. Get the parent directory of the current working directory and repeat from step #2.

Here goes:

@ECHO OFF

SET "subdir=%~1"
SET "dir=%~f0"

:loop
  CALL :getdir "%dir%"

  IF EXIST "%dir%\%subdir%\" (
    ECHO %dir%\%subdir%
    GOTO :EOF
  )

  IF "%dir:~-1%" == ":" (
    ECHO Directory "%subdir%" not found.
    GOTO :EOF
  )
GOTO loop

:getdir
SET "dir=%~dp1"
SET "dir=%dir:~0,-1%"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文