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.

DapperDan
  • 175
  • 2
  • 2
  • 7
  • can you use external utilities like gnuwin utilities in your batch file? – fardjad Jan 27 '11 at 15:24
  • 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? – DapperDan Jan 27 '11 at 19:24
  • 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? – MSalters Jan 28 '11 at 14:14
  • 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. – DapperDan Jan 28 '11 at 21:26

1 Answers1

10

This function works
DosTips.com :MakeRelative

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

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 8
    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) – Matteo Italia Jan 28 '11 at 00:13
  • 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? – DapperDan Jan 28 '11 at 21:57
  • @DapperDan: Yes, copy&paste is the style of such "Function Library", including is much more complicated – jeb Feb 10 '11 at 14:47