Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF EXIST two directories - do nothing

Tags:

batch-file

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 sub2

:sub1

:sub2

Many thanks in advance.

like image 366
molecule Avatar asked Feb 25 '23 11:02

molecule


2 Answers

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.
like image 93
GolezTrol Avatar answered Apr 19 '23 19:04

GolezTrol


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
...
like image 21
lavinio Avatar answered Apr 19 '23 19:04

lavinio