Windows CMD: Get relative path from absolute path
Asked Answered
G

1

8

How do i obtain the relative path from an absolute path, e.g. convert 'C:\absolute\path\file.ext' to 'path'?

Thanks for your consideration

edit: i'd like to recursivly scan a directory for a certain filetype and then create symlinks for these folders containing this filetype. the symlinks should be named like the directory containing the filetype.

Giguere answered 27/1, 2011 at 12:54 Comment(4)
can you use external utilities like gnuwin utilities in your batch file?Heartwarming
i surely could but if possibly i'd stick to built-in routines as far as possible. shouldn't be a huge problem with grep then, i guess?Giguere
What precisely is the "relative" path? Because absolute\name seems to be an equally valid result. It's the path relative to the root directory, while path is the path relative to `C:\absolute`. Or did you mean the name of the parent directory?Iceblink
in fact, i need to get the directory, which actually contains the file, e.g. 'path' contains file.ext. i just need this one directory cropped from the rest.Giguere
B
10

This function works
DosTips.com :MakeRelative

It compares both pathes and removes the identical front.
Then it adds "..\" for each non matching part.

Bayard answered 28/1, 2011 at 0:8 Comment(3)
I'm always amazed at the fact that in Batch even the simplest programming task that goes beyond just launching a bunch of executables boils down to a series of incomprehensible for cycles. :S (+1, BTW)Donnettedonni
looks pretty complicated, guess it'll work then ;) how can i use this function in my batchfile? is it just a copy&paste and then just calling it?Giguere
@DapperDan: Yes, copy&paste is the style of such "Function Library", including is much more complicatedBayard

© 2022 - 2024 — McMap. All rights reserved.