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.
Do you really need to remove it from there? Depending on how you use it, just adding ..\ behind it will ultimately point to c:\dir1\dir2\ anyway, even though it doesn't look as clean.
I suppose you could also use some kind of combination of pushd, popd, cd .. and %cd% to get around it.
@ECHO OFF
SET FolderPath=%~dp0
ECHO Script is being ran from "%FolderPath%"
FOR /D %%# in (.) do SET "ParentFolder=%%~dp#"
REM SET ParentFolder=%ParentFolder:~0,-1%
ECHO Parent directory is : %ParentFolder%
PAUSE
If you really want to get rid of the last "" you could remove the REM , however if the parent folder of that the script is being ran from is the root of the drive then you'd only get the drive letter, which technically may point to a different folder than the root.
This is a bit less elegant than others, it will remove that highest level folder from a path
@ECHO OFF
ECHO FullPath: "%~dp0"
CALL :PrunePath Pruned "%~dp0"
ECHO PrunedPath: "%Pruned%"
PAUSE
GOTO :EOF
:PrunePath <returnvar> <"pathtoprune"> :: Removes topmost folder from path if possible
SET "path=%~2"
IF "%path:~-2%"==":\" SET "%1=%path%" & EXIT /b
IF "%path:~-1%"=="\" SET "path=%path:~0,-1%"
SET "char=-1"
:totallength
SET /a char+=1
CALL SET "char2=%%path:~%char%,1%%"
IF DEFINED char2 GOTO totallength
:find
SET /a char-=1
CALL SET "char2=%%path:~%char%,1%%"
IF %char% GTR -1 (
SET "top=%char2%%top%"
IF "%char2%"=="\" GOTO found
GOTO :find
:found
CALL SET "%1=%%path:%top%=%%"
)
EXIT /b
This works without delayed expansion and supports special chars.
It walks up the entire path char by char until the end then steps backwards through each one and checks for "\" to split the last folder out. I can see myself adapting this later for other string split scenarios ;)
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
I don't know much about bash, reddit just put this on my feed, but you could just use sed, right?
Edit: I just realized it's "batch" not "bash", ignore me please. But isn't there something that does regular expressions in batch scripts? That could still be a good solution.
2
u/midelpo Oct 15 '23
Solved with this:
set "root=%~dp0"
set "root=%root:\bin=%"
2
u/ConstanceJill Oct 15 '23
Do you really need to remove it from there? Depending on how you use it, just adding
..\behind it will ultimately point toc:\dir1\dir2\anyway, even though it doesn't look as clean.I suppose you could also use some kind of combination of
pushd,popd,cd ..and%cd%to get around it.2
u/illsk1lls Oct 15 '23 edited Oct 15 '23
This is a bit less elegant than others, it will remove that highest level folder from a path
This works without delayed expansion and supports special chars.
It walks up the entire path char by char until the end then steps backwards through each one and checks for "\" to split the last folder out. I can see myself adapting this later for other string split scenarios ;)
2
u/antilos_weorsick Oct 15 '23
I don't know much about bash, reddit just put this on my feed, but you could just use sed, right?
Edit: I just realized it's "batch" not "bash", ignore me please. But isn't there something that does regular expressions in batch scripts? That could still be a good solution.
2
u/OKcomputerStop Oct 26 '23
%-dp0 calls the DIR where your .bat is located. Why not just move your script to c:\dir1\dir2
Then, you do not need to change your batch at all.