0

I need to write a cmd script that gets a path variable from registry and returns that path up some levels. So far I've managed to read the string from the registry and now I only need to turn this:

C:\dir\wasd\qwert\someotherdir

into this:

C:\dir\wasd\qwert\

and I'm stuck. Help is very apreciated. Thanks!

luciantd
  • 3
  • 1

2 Answers2

1

You can substring the path to remove someotherdir from it:

path = "C:\dir\wasd\qwert\someotherdir";
newPath = path.substr(0, path.LastIndexOf("someotherdir"));

The LastIndexOf will return the index within the string path where "someotherdir" is found. That way substring will operate between 0 (the start of the string) and the index of "someotherdir"

Hope this helps!

Carlo M.
  • 331
  • 1
  • 3
  • 12
  • I need to do that in a .bat file, not java; but thanks anyways. – luciantd Mar 18 '14 at 15:21
  • @luciantd Sorry! Somehow I mis-read the question as being in C#! Is [this](http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file) of help? – Carlo M. Mar 18 '14 at 15:23
1
rem The data retrieved from somewhere
    set "dir=C:\dir\wasd\qwert\someotherdir"

rem Get the parent folder 
    for %%a in ("%dir%") do set "dir=%%~dpa"

rem Remove the tailing backslash
    set "dir=%dir:~0,-1%"

    echo %dir%
MC ND
  • 69,615
  • 8
  • 84
  • 126