Sorry. There are a couple of questions in your question that I donβt understand.
If root_parent_path is a folder located inside the root folder of the drive, as indicated in the directory tree, it should include a backslash in front of its name: \root_parent_path , right? If so, then the result of root_parent_path\dir1 not a relative path, but an absolute one, which starts from the root folder of the disk as follows: \root_parent_path\dir1 , right? Note that the batch file below takes this point and inserts a backslash before the second parameter.
As I understand it, you need the first folder in the path specified by the second parameter, which contains the file specified in the first parameter. This batch file does this:
EDIT: This batch file has been modified to accept the full 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. The relative path will lead to the above examples:
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 cannot contain spaces. If necessary, this can be fixed.
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 that your answer is not relative, but absolute without a drive, and that my program suggests this situation. If you answered in the comment that you want an absolute path as input and output, I would do it immediately, but you no longer answer ...
You should note that the relative and absolute path management is completely different, and also that if the drive or drive is implied. If your first question would include the second parameter, as it really is: c:\folder1\folder2\ , this question will not be a problem.
EDIT : A new version that accepts 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 quotation marks to enclose the path if it contains spaces:
script.bat file1 "c:\first dir\second dir\dir1\dir2"