开发者

IF EXIST two directories - do nothing

开发者 https://www.devze.com 2023-02-02 09:55 出处:网络
I am playing around with the IF EXIST batch file command but ran into a scenario. What i am trying to do is

I am playing around with the IF EXIST batch file command but ran into a scenario. What i am trying to do is

IF EXIST C:\Windows\system32 call batchfile2

IF EXIST C:\WINNT\system32 call batchfile3

But there are scenarios where both directories exist on PCs if win2k was upgraded to XP instead of a fresh XP install. What i want it to do if it detects both directories is to "do nothing" since the first two options above already takes care of what I want to do. Can someone tell me how i can manipulate this?

Besides the above, I believe I can also call subroutines within the same batch but how can I create a subroutine to end the script if it detects both "Windows\system32" and "WINNT\system32"?

IF EXISTS C:\Windows\system32 goto sub1 else goto s开发者_如何学Goub2

:sub1

:sub2

Many thanks in advance.


I'm not sure when exactly you want which option to execute, but you can combine gotos and labels as much as you want. A bit elaborate, maybe, but at least structured:

@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.

I think it would be possible to check for both on one row as well:

@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.


One simple way is:

if exist c:\windows\system32 if exist c:\winnt\system32 goto morestuff
if exist c:\windows\system32 call batchfile2
if exist c:\winnt\system32 call batchfile3
:morestuff
...


you can remove the "@ECHO OFF" ... the REM are just comments in the file.. and the ECHO are just things it outputs.. (if you delete the echo off it will show all of it..)

essentially, you can jump to different sections of the file with the goto statment.. you just reference a goto label.. and then later in the file use a colen and the label name as the anchor/target/label...

@ECHO OFF
REM Check to see if windows\system32 exists.. if so skip to the part 2 section
IF EXIST C:\WINDOWS\system32 goto parttwo

REM if windows\system32 didnt exist, it will check for the other dir...
IF EXIST C:\WINNT\system32 goto partthree


REM if we get to this point.. neither directory existed...  so skip to a message about that
goto neither

:parttwo
echo windows\system32 existed
REM because it was not checked earlier, check to see if the second directroy exists
IF EXIST C:\WINNT\system32 goto end

echo windows\system32 existed, but winnt\system32 does not...
echo do or call whatever for part 3....



goto end



:partthree
echo winnt\system32 existed
echo do or call whatever for part three




goto end



:neither
echo Could not find windows or winnt \system32


:end
echo goodbye

You can always hit up MS for more info: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true

0

精彩评论

暂无评论...
验证码 换一张
取 消