Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:14:02+00:00 2026-05-26T01:14:02+00:00

I am trying to search the current directory path and find a certain file

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:14:03+00:00Added an answer on May 26, 2026 at 1:14 am

    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%"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to search the C:\ drive for all files with a certain
I'm trying to search rather big files for a certain string and return its
string[] files = Directory.GetFiles(tb_dir.Text, tb_filter.Text, SearchOption.AllDirectories); I'm trying to search through a directory and
Currently I'm trying to enhance my search algorithm. For better understanding, here's the current
I'm trying to build a search engine that will check a list and then
I'm trying to search through my inbox to find specific email's but there every
I am trying to change may current Tab. I search for fitting problems but
I am trying to search a drive letter (C drive) for a file in
I’m trying to build a parallel implementation of a min-max search . My current
I'm trying to search multiple attributes in XML : <APIS> <API Key=00001> <field Username=username1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.