Reliable way to check if two variables in CMD shell point to the same folder
There is no way to check just by comparing these variables:
C:\>set "d1=C:\"
C:\>set "d2=C:\Windows\.."
C:\>if %d1%==%d2% (echo true) else (echo false)
false
I can put together a complex construct with pushd
and popd
and additional variables, but isn't there an easier way?
+3
utapyngo
source
to share
3 answers
Similar to jeb's solution, but using FOR instead of a called subroutine
for %%A in ("%d1%") do for %%B in ("%d2%") do if "%%~fA"=="%%~fB" (echo true) else (echo false)
+4
dbenham
source
to share
You can normalize variables with a little function.
set d1=C:\
set d2=C:\Windows\..
call :normalize d1
call :normalize d2
if "%d1%"=="%d2%" (echo true) else (echo false)
exit /b
:normalize
setlocal EnableDelayedExpansion
for /F "delims=" %%M in ("!%1!") do (
endlocal
set "%1=%%~dpM"
)
exit /b
+5
jeb
source
to share
Don't know if it will suit your needs, but you can create the file in the 1st check loop if it exists in the second:
echo test > %d1%\checkthisfile.txt
if exist %d2%\checkthisfile.txt (echo true)
+1
Loïc MICHEL
source
to share