我正在玩IF EXIST批处理文件命令,但遇到了一个场景.我想做的是
IF EXIST C:\ Windows\system32调用batchfile2
IF EXIST C:\ WINNT\system32调用batchfile3
但是有些情况下,如果win2k升级到XP而不是全新的XP安装,那么这两个目录都存在于PC上.如果它检测到两个目录,我想要它做什么是"什么都不做",因为上面的前两个选项已经处理了我想要做的事情.有人能告诉我如何操纵这个吗?
除了上面的内容之外,我相信我也可以在同一个批处理中调用子程序,但是如果它检测到"Windows\system32"和"WINNT\system32",我怎么能创建一个子程序来结束脚本呢?
如果EXISTS C:\ Windows\system32转到sub1,则转到sub2
:SUB1
:SUB2
提前谢谢了.
我不确定你何时想要执行哪个选项,但你可以根据需要组合gotos和标签.有点精心,也许,但至少结构化:
@echo off
IF EXIST C:\Windows\system32 goto windowsfound
:afterwindows
IF EXIST C:\WINNT\system32 goto winntfound
:afterwinnt
goto end
:windowsfound
IF EXIST C:\WINNT\system32 goto bothexist
echo Windows folder found, do something.
call batchfile2
goto afterwindows
:winntfound
echo WINNT folder found, do something.
call batchfile3
goto afterwinnt
:bothexist
echo Both folders already exist.
goto end
:end
echo Exiting.
Run Code Online (Sandbox Code Playgroud)
我认为也可以在一行中检查两者:
@echo off
IF EXIST C:\Windows\system32 IF EXIST C:\WINNT\system32 goto bothfound
IF EXIST C:\Windows\system32 goto windowsfound
IF EXIST C:\WINNT\system32 goto winntfound
:windowsfound
echo Windows folder found, do something.
call batchfile2
goto end
:winntfound
echo WINNT folder found, do something.
call batchfile3
goto end
:bothexist
echo Both folders already exist.
goto end
:end
echo Exiting.
Run Code Online (Sandbox Code Playgroud)