IF EXIST two directories - do nothing

I play with the IF EXIST batch file command, but run into a script. What I'm trying to do is

IF EXIST C: \ Windows \ system32 batchfile2 call

IF EXIST C: \ WINNT \ system32 call batchfile3

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

In addition to the above, I believe that I can also call routines in the same group, but how can I create a routine to end a script if it detects both "Windows \ system32" and "WINNT \ system32"?

IF EXISTS C: \ Windows \ system32 goto sub1 else goto sub2

: sub1

: sub2

Thank you very much in advance.

+3
source share
3 answers

I'm not sure exactly when you want which option to execute, but you can combine gotos and shortcuts as much as you want. A bit complicated, 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 that one could check on one line:

@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.
+3
source

One easy way:

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
...
+3
source

you can remove "@ECHO OFF" ... REM is just comments in the file .. and ECHO is just what it outputs .. (if you delete the echo, it will show it all.)

essentially, you can go to different sections of the file using goto statment .. you just reference the goto .. label, and then in the file use colen and the label name as the binding / label / 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 MS for more information: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true

-1
source

Source: https://habr.com/ru/post/1783243/


All Articles