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!

2 Answers 2

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%
Sign up to request clarification or add additional context in comments.

Comments

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!

2 Comments

I need to do that in a .bat file, not java; but thanks anyways.
@luciantd Sorry! Somehow I mis-read the question as being in C#! Is this of help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.