For the life of me I can't figure out why I can't echo test exit with this simple batch script using GOTO. The path exists. This should be VERY simple, but something missing. If I run this script I should echo "Test Exit", but I'm getting "Test Install" even if the path exists. Any Help?
#echo off
:TestInstall
Echo Test Install
pause
IF EXIST "C:\Program Files\Microsoft Office\root\Office16\Excel.exe" (
GOTO TestExit
)ELSE{
GOTO TestInstall
}
:TestExit
Echo Test Exit
pause
)ELSE{
GOTO TestInstall
}
Huh? Why do you use different parantheses/brackets? Also batch is quite picky about spaces - there have to be one before and after else:
) ELSE (
GOTO TestInstall
)
You want something like this:
#echo off
:install
echo install
pause
IF EXIST "C:\Program Files\Microsoft Office\root\Office16\Excel.exe" (
goto install
) ELSE (
goto exit
)
:exit
echo exit
pause
Well, you have )ELSE{which is not correct, it must be translated to ) ELSE ( and close with a parenthesis and not a curly brace. Also do forget to put the spaces.
Be aware though that if the file doesn't exists you'll have an infinite loop running between the :install and the goto install instructions.
Moreover, at the beginning of your script, because the :install label is right in the top, it'll run the install part before doing any check.
You should move the install part under the if statement if you don't want this to happen.
Related
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
bat file via Jenkins. I have a part of code which looks as below
IF %Status% == Completed (
Echo Process has executed successfully
)
ELSE (
<Code to be added>
)
My requirement is that if Status has a value other than 'Completed' then the bat file must throw an exception or something so that the Jenkins Build Fails. Is there any way of assertions or throwing exceptions in bat files? Please help me with this?
Each line of a batch file is seen as a new command. Else on its own is not, so as it is on a new line, it is seen as one. You need to put the parenthesis on the same line as else.
Additionally, you need to double quote your matches to ensure you eliminate any possible whitespace. I have added /i switch to the if statement to make the match case insensitive.
IF /i "%Status%" == "Completed" (
echo Process has executed successfully
) else (
echo Failed to build
pause
exit /b 1
)
A different way is to not use else.
IF not /i "%Status%" == "Completed" goto :error
echo Process has executed successfully
echo other success code goes here
goto :eof
:error
echo Your code has failed.
pause
goto :eof
Have a weird issue, firstly am using batch files to install applications..the installations are working fine without any issue.
I have at the end of the batch if the error level is NEQ 0 output the error code to a file, but the output is always 9009.
From searching the web for 9009 it says that the executable cannot be found, but surely it can as the application installs fine.
Here is a sample of one of my batch scripts to install:
IF exist %windir%\LogFolder\BoxSync4.0x86.txt ( goto eof ) ELSE ( goto BoxSyncInstall )
:BoxSyncInstall
msiexec /i "\\servername\InstallFolder\BoxSync\SyncMSI32.msi" /qn
if %ErrorLevel% EQU 0 (
>>"\\servername\gpolog\BoxSync4.0x86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Box Sync 4.0 x86 Installed"
>>"%windir%\gpologs\BoxSync4.0x86.txt" echo "Box Sync 4.0 x86 Installed"
)
else if %ErrorLevel% NEQ 0(
>>"\\servername\gpolog\BoxSyncErrorsx86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Error trying to install/upgrade to BoxSync4.0x86"
)
:eof
Does anyone have any ideas why I might be getting this error constantly?
Thanks
Mikoyan
Try using setlocal enabledelayedexpansion at the start of your batch file, and !ERRORLEVEL! inside your IF.
See also:
ERRORLEVEL inside IF
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/setlocal.mspx?mfr=true
http://batcheero.blogspot.ru/2007/06/how-to-enabledelayedexpansion.html
The problem is syntax, you need a space between your 0 and (, like so:
IF exist %windir%\LogFolder\BoxSync4.0x86.txt ( goto eof ) ELSE ( goto BoxSyncInstall )
:BoxSyncInstall
msiexec /i "\\servername\InstallFolder\BoxSync\SyncMSI32.msi" /qn
if %ErrorLevel% EQU 0 (
>>"\\servername\gpolog\BoxSync4.0x86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Box Sync 4.0 x86 Installed"
>>"%windir%\gpologs\BoxSync4.0x86.txt" echo "Box Sync 4.0 x86 Installed"
)
else if %ErrorLevel% NEQ 0 (
>>"\\servername\gpolog\BoxSyncErrorsx86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Error trying to install/upgrade to BoxSync4.0x86"
)
:eof
EDIT: If the installer needs admin privileges and this is a Win8+ OS, you might have security restrictions, in that case, copy the msi installer into the %TEMP% folder and run it from there. The reason this happens is because when you run the command prompt in Administrator mode, it restricts use of unc paths (for "security" reasons).
Try also pushd and popd as #dbenham keeps reminding us: LINK
found where the issue lied it is under the ELSE IF as I watched the install script run by not being silent.
After removing ELSE the script ran fine. Thanks again for your help as you have guided me with other tricks.
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.
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