A reliable way to check if two variables in a cmd

2019-07-01 17:17发布

问题:

It can't be checked 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 make up a sophisticated construct with pushd and popd and additional variables but isn't there a simpler way?

回答1:

Similar to jeb's solution, but using FOR instead of called subroutine

for %%A in ("%d1%") do for %%B in ("%d2%") do if "%%~fA"=="%%~fB" (echo true) else (echo false)


回答2:

You could normalize the variables with a small 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


回答3:

dont know if it will suite to your need but you could create a file on 1st dir anc check if it exists in second :

echo test > %d1%\checkthisfile.txt
if exist %d2%\checkthisfile.txt (echo true)