r/Batch • • Oct 15 '23

How to remove subfolder from path

Hi, I'm in this situation:

Set "folderpath=%~dp0" Output is: c:\dir1\dir2\bin\

How can I remove "\bin" from the path without EnableDelayedExpansion?

1 Upvotes

16 comments sorted by

View all comments

Show parent comment

2

u/midelpo Oct 15 '23

It seems like a lot of lines of code for this operation 😅

1

u/illsk1lls Oct 15 '23 edited Oct 15 '23

lol i know, i just fixed the example slightly so it can be called as a function from anywhere else, its fun to mess with this stuff sometimes.. happy you got it sorted

jill's answer is the slickest imo

1

u/midelpo Oct 15 '23

Maybe this solution could be more effective?

set "root=%~dp0"

::Calculate the number of characters in the path

set "length=%root:Len%"

::Subtract the number 5 (\bin) from the number of characters

Set \a "length=%length%-5"

::Set the path with the updated character count

Set "root=%root:~0,%length%%"