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.
Excuse me. There are a couple points in your question that are not clear to me.
If
root_parent_pathis 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 resultroot_parent_path\dir1is 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
Remember that this result is an absolute path. A relative path result in your examples above would be these values:
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.
In this case, use quotes to enclose the path if it contain spaces:
script.bat file1 "c:\first dir\second dir\dir1\dir2"