luciantd
luciantd

Reputation: 3

Path parsing from variable in cmd

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!

Upvotes: 0

Views: 327

Answers (2)

MC ND
MC ND

Reputation: 70943

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%

Upvotes: 1

Carlo M.
Carlo M.

Reputation: 331

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!

Upvotes: 1

Related Questions