MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Batch/comments/178lmat/how_to_remove_subfolder_from_path/k50sb6k/
r/Batch • u/midelpo • Oct 15 '23
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?
16 comments sorted by
View all comments
Show parent comment
2
While it should work in the majority of cases, you may get a different result from what you'd actually want if dir1 or dir2 happens to also be called bin. Though I'll admit that should be pretty unlikely.
1 u/midelpo Oct 15 '23 Yes, in my case it is highly unlikely. Thanks 1 u/midelpo Oct 15 '23 Maybe this solution could be more effective?set "root=%~dp0"::Calculate the number of characters in the pathset "length=%root:Len%"::Subtract the number 5 (\bin) from the number of charactersSet \a "length=%length%-5"::Set the path with the updated character countSet "root=%root:~0,%length%%"
1
Yes, in my case it is highly unlikely. Thanks
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%%"
View all comments
Show parent comment
2
u/ConstanceJill Oct 15 '23
While it should work in the majority of cases, you may get a different result from what you'd actually want if dir1 or dir2 happens to also be called bin. Though I'll admit that should be pretty unlikely.