IF EXIST two directories - do nothing - 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.

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

Related

What :PROMPT means in dos batch file? [duplicate]

I am trying to write a bat file for a network policy that will install a program if it doesn't exist as well as several other functions. I am using GOTO statements depending on whether or not certain criterion are met. However, it seems that the labels are not firing correctly as all of them do.
I have simplified my script so as to grasp some idea of what may be happening.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
:MISSING
echo file missing
ping localhost -n 5 >NUL
Basically it checks to see that the file "test.txt" exists in folder "c:\test" which id does. So it should echo file exists to the console. However, both "file exists" and "file missing" are echoed to the console. I find that if I remove the file from the folder or simply rename it, it only echoes "file missing"
Why is it running running both labels?
Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:
#ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :NEXTBIT
:MISSING
echo file missing
:NEXTBIT
ping localhost -n 5 >NUL
It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:
#ECHO OFF
IF EXIST c:\test\test.txt (
ECHO File exists
) ELSE (
ECHO File missing
)
ping localhost -n 5 >nul
...so you can eliminate your GOTOs entirely.
It's because you need to skip over the "missing" bit if it exists:
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :COMMON
:MISSING
echo file missing
:COMMON
ping localhost -n 5 >NUL
You may also want to keep in mind that the current cmd.exe batch language is a fair bit more powerful than that which came with MS-DOS. I would prefer this one:
#echo off
if exist c:\test\test.txt (
echo file exists
) else (
echo file missing
)
ping localhost -n 5 >nul
After you echo file exists the next command is
echo file missing
You need to do something to skip the missing case. Perhaps another goto to a :PING label?
When you're debugging it helps to keep the echo on.
Because GOTO statement moves the execution to that label. To use it in the situation like yours, you need to add another GOTO label.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO MISSING
:EXISTING
echo file exists
GOTO END
:MISSING
echo file missing
GOTO END
:END
ping localhost -n 5 >NUL
#echo off
IF EXIST "c:\test\test.txt" ( :: warning double quotes
GOTO EXISTING
) ELSE ( :: this format best in batch
GOTO MISSING
) :: don't forget
:EXISTING
echo file exists
goto OTHER :: if file exist jump OTHER
:MISSING
echo file missing
:: label is not required
:OTHER
timeout /t 5 >nul
pause

batch file - looking for way if both folder exist then proceed else skip the operation

I have a below batch file where if both folder1 and folder2 both exist then I want to execute else don't want to do anything.
Here even my folder NOT exist, I am getting echo message Both folders exist. What I am doing wrong here?
#ECHO OFF
set folder1="C:\Temp1"
set folder2="C:\Temp2"
IF EXIST %folder1% IF EXIST %folder2% goto bothfound
:bothfound
echo Both folders exist.
goto end
:end
echo Done.
pause
This is because you don't have a GOTO to bypass the :bothfound block. After the if exist statements, it continues on because it never invoked the goto. After you do your if exist, you need a goto end or a goto to a not found block.
example:
IF EXIST %folder1% IF EXIST %folder2% goto bothfound
goto end
:bothfound

How to use parentheses in .bat files, set command and IF EXIST

Ok, I have batch file, just a simple one that hides and unhides folders.
I don't see why it cannot seem to execute accordingly;
Here is extended sample code:
#echo off
color a
title Folder/Drive hider Service
:jiki
echo Loading...
TIMEOUT /T 2 >nul
goto inputs
:inputs
echo Enabling security...
TIMEOUT /T 2 >nul
cls
goto menu
:menu
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)
if EXIST "%~dp0\Logan_Documents" (set status=Folder is unlocked, to open it, enter open as your `action.)`
cls
echo.
echo STATUS: %status%
echo.
echo ----------------------------------------
echo FOLDER PROTECTOR by Logan
echo ----------------------------------------
echo.
echo Lock = Lock the folder(s)
echo Unlock = Unlock the folder(s)
echo Credits = For more info
echo V = Display your current version
echo Exit = Close the program
echo.
echo ----------------------------------------
echo For more info, just ask Logan!
echo ----------------------------------------
echo.
echo Select your action, %USERNAME%.
echo.
set /p "menu=>"
if /I %menu%== lock goto lock
if /I %menu%== unlock goto unlock
if /I %menu%== credits goto credits
if /I %menu%== v goto version
if /I %menu%== exit goto exit
goto invalid
and also a lot more, and every time I go to execute the script, it just leaves the status variable blank.
Here's what I've tried.
Reconfiguring all variables through a port, which then sorts based on if exist. doesn't work, just leaves status blank.
Using different variables. (Kinda stupid but I didn't want to think that I have all these errors because of a small typo.) Still left error blank.
Appreciate all efforts to resolve my problem and get this program working!
-Logan
if exist should work fine exactly as you use it. You don't strictly need the quotes, since the names don't include spaces. Also you don't need the parentheses since it is a single command.
But then again, it should work with them as well (I actually tested this), so the only thing I can imagine is that the files or folders are not found because the script is running in the wrong directory. After all you use just the names without any path, so the current directory should contain those files.
The 'current directory' isn't necessarily the directory in which the script is saved. If you are in 'C:\Foo' and you call 'C:\Bar\Script.bat', the current directory will still be 'C:\Foo'. The same goes for starting scripts through a shortcut.
To try this, you can use echo %CD% in your script to echo the current directory.
As a possible solution, you can use %~dp0 to use the actual directory in which the batch script is saved, so you always have a starting point to start from:
REM Check if 'Encryption' exists in the same folder as the batch file.
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)
probably neither of the ifs are true, maybe because the active directory is not what you think it is. you can test this easily by inserting a set status=none above the ifs. or insert dir to see what the scrips actually sees at this point.

How to mention If else condition in batch file

Is there any code system like below :
#echo off
set /p location=Type Folder Location
copy "%location%\file.txt" "c:\Folder"
if copy is done goto ok
if not goto failed
:ok
echo File is copyed succesfully
:failed
echo File is not copyed
echo.
pause
exit
#echo off
set /p location=Type Folder Location
copy "%location%\file.txt" "c:\Folder"
if errorlevel 1 goto failed
:ok
echo File is copied succesfully
goto done
:failed
echo File is not copied
:done
echo.
pause
exit
Normally, when a command succeeds, the "magic" variable errorlevel is set to zero; if it fails, to non-zero.
The syntax if errorlevel n will be true if errorlevel is n or greater than n (this last point is important - if errorlevel 0 will always be true (in normal circumstances).
Unlike many languages, batch has no concept of the end of a "procedure" - it simply continues execution line-by-line until it reaches the end-of-file. Consequently, you need to goto :eof after completing the mainline, otherwise execution will continue through the subroutine code. :EOF is a predefined label understood by CMD to mean end of file. The colon is required.
(in this case, the goto done skips over the 'failed' message - often you want to terminate the batch under certain circumstances. there you'd use goto :eof)
Like #Magoo saids in your case you just need 1 condition to go forward.
But to answer your question: How to mention If else condition in batch file
#echo off
set /a $var=1
if %$var%==1 (goto:ok) else (goto:nok)
:ok
echo OK
exit/b
:nok
echo NOK
You can change the value of $var to check it

Folder creation using bat command

I have a bat file to create folder.
:X3Main
IF EXIST "%1%\JBoss" goto Test
IF EXIST "%1%\DB" goto DB
goto end
:Test
mkdir "%DESTINATION%\IX3\COSMIC\JBoss"
goto end
:DB
mkdir "%DESTINATION%\IX3\COSMIC\DB"
goto end
here first folder is created successfully(IF EXIST "%1%\JBoss" goto Test) but second function is not working. If I remove first function then second function is working.
Please can you someone explain reason behind this?
Some extra info: In the NT line of Windows for a reliable test for a folder you need to end the foldername with a backslash
and you may have meant %~1 instead of %1%
IF EXIST "%~1\JBoss\" do task
and in your case you can always use this which will do nothing if the folder already exists: the 2>nul eliminates a harmless error message when the folder exists.
mkdir "%DESTINATION%\IX3\COSMIC\JBoss" 2>nul
mkdir "%DESTINATION%\IX3\COSMIC\DB" 2>nul
try this:
:X3Main
IF EXIST "%1%\JBoss\" call:Test
IF EXIST "%1%\DB\" call:DB
goto :eof &rem or goto:end or where you want
:Test
mkdir "%DESTINATION%\IX3\COSMIC\JBoss"
goto :eof
:DB
mkdir "%DESTINATION%\IX3\COSMIC\DB"
goto :eof
use call to return from function and goto:eof to end a function.
Edited concerning foxidrive's answer.

Resources