A reliable way to check if two variables in a cmd shell point to the same folder

对着背影说爱祢 提交于 2020-01-24 10:13:43

问题


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)


来源:https://stackoverflow.com/questions/14789391/a-reliable-way-to-check-if-two-variables-in-a-cmd-shell-point-to-the-same-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!