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 8894937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:38:17+00:00 2026-06-14T23:38:17+00:00

I am having a simple problem that I cannot figure out: I want to

  • 0

I am having a simple problem that I cannot figure out: I want to find a parent folder that contains a certain file, and generate an absolute path of that parent folder – and this needs to be done inside a batch script – no powershell, no perl, no python…

Example, say this is my directory tree:

  root_parent_path
    dir0
    dir1
      file1
      dir2
        dir21
        dir22
      dir3
        dir31
        dir32

In example above, root_parent_path is in form c:\folder1\folder2\.

The result should be stored in an environment variable, say RESULT_PATH.

So if I call any of the following:

script.batch file1 root_parent_path\dir1\dir2
script.batch file1 root_parent_path\dir1\dir2\dir21
script.batch file1 root_parent_path\dir1\dir3\dir31

the variable RESULT_PATH should be set to root_parent_path\dir1, because that is a parent that contains that file1. (I don’t care if there is more then one or no parent folder – I will make sure there is only one.)

Help is much appreciated, so much time has been wasted on this…

Note: I would appreciate if code is also explained! If two answers offer working solution, I will pick up the one with better explanation.

  • 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-06-14T23:38:21+00:00Added an answer on June 14, 2026 at 11:38 pm

    Excuse me. There are a couple points in your question that are not clear to me.

    • If root_parent_path is a folder placed inside the root folder of the disk, as you indicated in your directory tree, then it must include a backslash before its name this way: \root_parent_path, right? If this is true, then the result root_parent_path\dir1 is not a relative path, but an absolute one that start from the root folder of the disk this way: \root_parent_path\dir1, right? Please note that the Batch file below assume this point and insert a backslash before the second parameter.

    • As I understand it, you want the first folder in the path given by second parameter that contain the file given in first parameter. This Batch file do that:

    EDIT: This Batch file have been modified to accept a fully qualified path in the second parameter

    @echo off
    setlocal EnableDelayedExpansion
    rem Get drive from second parameter, given or implied
    for %%a in (%2) do set drive=%%~Da
    rem Extract each partial path from second parameter and search first parameter into it
    set return_path=
    set param2=%2
    rem Delete drive from second parameter, if any
    set param2=%param2:*:=%
    for %%a in (%param2:\= %) do (
       set return_path=!return_path!\%%a
       if exist %drive%!return_path!\%1 goto continue
    )
    set return_path=PATH NOT FOUND
    :continue
    echo %drive%%return_path%
    

    Remember that this result is an absolute path. A relative path result in your examples above would be these values:

    script.batch file1 root_parent_path\dir1\dir2           ->      ..
    script.batch file1 root_parent_path\dir1\dir2\dir21     ->      ..\..
    script.batch file1 root_parent_path\dir1\dir3\dir31     ->      ..\..
    

    Please note that each folder in the path can not contain spaces. This may be fixed, if required.

    Test the program and report the result…

    Antonio

    PD – In your ORIGINAL question you said you want a relative path as output and put an example with a relative path as input. I noted you that your answer was not relative, but absolute with no drive, and that my program assume this situation. If you would answer in a comment that you want absolute path as both input and output, I would do that immediately, but you don’t answer anymore…

    You must note that relative and absolute path management is entirely different and also that if the disk drive is given or implied. If your first question would included the second parameter as it really is: c:\folder1\folder2\, this point wouldn’t be a problem.

    EDIT: New version that accept spaces in the second parameter.

    @echo off
    setlocal EnableDelayedExpansion
    rem Get drive from second parameter, given or implied
    for %%a in (%2) do set drive=%%~Da
    rem Get second parameter removing enclosing quotes, if any
    set param2=%~2
    rem Delete drive from second parameter (from beggining of path until first colon)
    set param2=%param2:*:=%
    rem Change possible spaces in the path by another character (I used dollar sign)
    rem to avoid separate names with spaces at that point
    set param2=%param2: =$%
    rem ... of course, dollars must be returned back to spaces later
    rem Extract each partial path from second parameter and search first parameter into it
    set return_path=
    for %%a in (%param2:\= %) do (
       set return_path=!return_path!\%%a
       rem Get back spaces converted into dollars
       set return_path=!return_path:$= !
       rem Enclose file name in quotes (required if path contain spaces)
       if exist "%drive%!return_path!\%1" goto continue
    )
    set return_path=PATH NOT FOUND
    :continue
    echo %drive%%return_path%
    

    In this case, use quotes to enclose the path if it contain spaces:

    script.bat file1 "c:\first dir\second dir\dir1\dir2"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a simple problem that I cannot find a simple solution to.
I am having one hell of a problem that I cannot figure out for
hey guys having this really simple problem but cant seem to figure out have
I'm having problems with something very simple, that i can't quite figure out why
I'm having a problem getting a simple ASP.NET webpage to display. The page contains
I'm coding a simple SDL program with VC10. The problem that I am having
I am having the problem that I cannot select a specific XML node which
I am having the following problem that I cannot solve. Let's say I have
I have probably a simple math problem that I'm having surprisingly hard time figuring
I am getting frustrated over a simple problem where I cannot find the issue:

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.