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.
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
I am executing a windows bat script through jenkins. Batch file is giving the desired output,however build is failing.My batch file is..
cd /d D:\\Bank\\Member\\ID
if %errorlevel% neq 0 exit /b %errorlevel%
mkdir OTP
if %errorlevel% neq 0 exit /b %errorlevel%
robocopy C:\Corporate D:\\Bank\\Member\\ID\ /E /XF *.bat
if %errorlevel% neq 1 exit /b %errorlevel%
cd /d D:\\Bank\\Staff\\ID
ROBOCOPY GIVES EXIT CODE 1 AFTER SUCESSFULLY COPYING FILES.
BUT JENKINS FAILS BUILD AND GIVING BELOW ERROR:
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
I Want the build to be successful if robocopy exits code 1.
My best advise would be to use jenkins-pipeline, try/catch block, and use bat commands as few as possible (or do not use at all).
But considering your case there's a simple solution as well: just set the field "ERRORLEVEL to set build unstable" to 1 (or other suitable number). The field appears if you click "Advanced" button under the "Execute Windows batch command" block:
This method will check your build as "Unstable", but will continue to execute.
please use like following to avoid:
bat "robocopy /s source dest & EXIT /B 0"
The above will continue the jenkins build even if robocopy returns non-zero errorlevel. Robocopy does not return 0 for various reasons even after successfull copy, as it compared the two folders. Please lookup for it's return code to know more details
As mentioned here, the first criteria to check is the account used to run Jenkins.
Type services.msc to open the Windows services and look for the Jenkins service.
Instead of "Local Service Account", use your own account: that will avoid any right issue.
But: the other criteria is to display the error code.
As mentioned here:
All exit codes up to '3' are fine.
So after robocopy, you can add:
#echo robocopy exit code: %ERRORLEVEL%
#if %ERRORLEVEL% GTR 3 ( echo robocopy ERROR )
#if %ERRORLEVEL% GTR 3 ( exit %ERRORLEVEL% )
#set ERRORLEVEL=0
REM at the end:
exit /b 0
That will ensure Jenkins don't fail the batch step, even though the original error level for robocopy was 1.
Trying to create a batch file that will check the iTunes version and then will update if the version is lower than the version listed in the script.
The issue I'm having is what is the best way to get the value from the registry key to my IF value.
I have looked around on Google a bit and can't find something that matches what I want to do.
::Finds the value of the Version key
REG QUERY "HKLM\SOFTWARE\Apple Computer, Inc.\iTunes" /v Version
This is where I am stuck. How do I use the value from Version? Do I need to use a FOR loop for this? I have tried playing with it but not su
::If the version matches the number below iTunes is up to date
IF Version==12.5.4.42 #echo Up to date! && goto end
::If the version is not equal to the number below
IF NOT Version==12.5.4.42 && goto install
::Installs the current version from the repository
:install
msiexec.exe ALLUSERS=true reboot=suppress /qn /i "%~dp0appleapplicationsupport.msi"
msiexec.exe /qn /norestart /i "%~dp0applemobiledevicesupport.msi"
msiexec.exe /qn /norestart /i "%~dp0itunes.msi"
echo Cleaning Up Installation
del C:\Users\Public\Desktop\iTunes.lnk
:end
exit
I feel like a tool that I can't get this figured out. Haven't dealt with FOR statements before. Apologies in advance for my stupidity.
One specific problem with your script is that you've got an extra && in this line:
IF NOT Version==12.5.4.42 && goto install
Temporarily remarking out #echo off can help you find these simple syntax errors. And as Magoo points out, Version is a string that will never equal 12.5.4.42. Variables in batch are surrounded by % when you want to evaluate them (or sometimes !).
More generally, when comparing version numbers, it'd be better to use a language that can objectify the version number and can understand major.minor.build.revision. You don't want to trigger the install if the installed version is, for example, 12.10.0.0. Comparing that against 12.5.4.42 in batch would trigger the install. Even though 12.10.x.x is numerically greater than 12.5.x.x, it is alphabetically less, and is treated as the lower value by if comparisons.
As an illustration, from a cmd console, enter this and see what happens:
if 12.10.0.0 leq 12.5.4.42 #echo triggered!
I'd use PowerShell for the heavy lifting here. Here's an illustration using a Batch + PowerShell hybrid script. I haven't tested it since I don't have iTunes installed, so you might need to salt to taste.
<# : batch portion (begin multiline PowerShell comment block)
#echo off & setlocal
set "installer_version=12.5.4.42"
powershell -noprofile "iex (${%~f0} | out-string)" && (
echo Up to date!
goto :EOF
)
:install
msiexec.exe ALLUSERS=true reboot=suppress /qn /i "%~dp0appleapplicationsupport.msi"
msiexec.exe /qn /norestart /i "%~dp0applemobiledevicesupport.msi"
msiexec.exe /qn /norestart /i "%~dp0itunes.msi"
echo Cleaning Up Installation
del C:\Users\Public\Desktop\iTunes.lnk
goto :EOF
: end batch / begin PowerShell hybrid code #>
$regkey = "HKLM:\SOFTWARE\Apple Computer, Inc.\iTunes"
$installed = (gp $regkey Version -EA SilentlyContinue).Version
if (-not $installed) {
"iTunes not installed."
exit 1
}
# exits 0 if true, 1 if false (-le means <=)
exit !([version]$env:installer_version -le [version]$installed)
To answer the question you asked, though, how to capture the output of reg or any other command, use a for /F loop. See for /? in a cmd console for full details.
IF Version==12.5.4.42 #echo Up to date! && goto end
The string Version is never going to be equal to the string 12.5.4.42. You need the contents of Version, so the code should be
IF %Version%==12.5.4.42 #echo Up to date!&goto end
(a single & concatenates commands)
The following if is superfluous. To reach that statement, the version must be not-12.5.4.42 otherwise execution would have been transferred to :end
BTW, goto :eof where the colon in :eof is required means 'go to physical end-of-file'.
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.
recently i'm working on some project with arm but no OS in it.
Now when i compile it, i must open keil.
but keil is weak in edit, so i'm considering to write a script to execute complier of keil, to build the project.
but i know little about keil, so i want to know if it is possble, for avoiding useless work.
thanks for any help.
As mentioned by artless noise you can call armcc directly on the command line or integrate it into a build system like make, scons, etc . A good starting point is letting Keil uVision create a batch file for you: CREATING A PROJECT BATCH FILE.
Another possibility is to call Keil from the command line with the project file as command line parameter with options to build the project.Keil uV4 command line.
BTW I use this command line options also for automated unit testing in the simulator and for flash download.
Edit 2021:
Revisited this answer as Kiel still hasn't improved their command-line tools.
Corrected a small typo in the script.
Fixed the link to the Kiel command line documentation.
Added the script to a Github gist, so it can easier be forked.
Original 2019 answer:
Here's my attempt at scripting the Keil uVision4 compiler.
Keil's Commandline interface basically hasn't changed since at least 2011, and it's very limited - especially for using the compiler on a build server.
Additionally, the Keil uVision4 compiler is very strange, not least when it comes to creating output, but in theory supports two methods - however sometimes none, only one, the other, or both output files are created. This batch script attempts to handle all of these situations.
Another issue is when the Flex license server is in use. This script allows you to define the number of times you want to retry a build and to set the interval between attempted builds. It also successfully recover if the license is suddenly withdrawn during a build.
If you have additions to this script, please post them here.
#echo off
setlocal
:: KeiluVisionBuilder.cmd
:: Written by Flemming Steffensen, 2019.
:: Free for use and abuse by anyone.
:: ======================
:: Configuration
::
set WaitForLicenseTimeout=60
set BuildAttemptsMax=10
set "ProjectFileName=bootloader.uvprojx"
set "ProjectPath=MyProject\bootloader\"
set "Compiler=C:\Keil_v5\UV4\UV4.exe"
set "OutFolder=OutputFolderDefinedIn_MyProject\"
::
:: ======================
:: Do not edit below this line
set BuildAttempt=0
pushd %ProjectPath%
:PerformBuild
echo:
echo:Performing Keil Build...
if exist build.log del build.log
if exist %OutFolder%*.build_log.htm del %OutFolder%*.build_log.htm
start /wait %Compiler% -j0 -b %ProjectFileName% -o build.log
set ReportedError=%ERRORLEVEL%
:: Scan build.log to determine if the license is locked.
find /c "Error: *** All Flex licenses are in use! ***" build.log >nul
if %errorlevel% equ 0 (
set /a BuildAttempt=%BuildAttempt% + 1
echo:Error: *** All Flex licenses are in use!
if %BuildAttempt% equ %BuildAttemptsMax% goto NoLicenseAvailable
echo:Retrying ^(%BuildAttempt% of %BuildAttemptsMax%^) in %WaitForLicenseTimeout% seconds
waitfor SignalNeverComming /T %WaitForLicenseTimeout% >nul 2>&1
goto PerformBuild
)
:: Scan alternative build.log to determine if the license is locked.
find /c "Failed to check out a license" %OutFolder%*.build_log.htm >nul
if %errorlevel% equ 0 (
set /a BuildAttempt=%BuildAttempt% + 1
echo:Error: Failed to check out a license
if %BuildAttempt% equ %BuildAttemptsMax% goto NoLicenseAvailable
echo:Retrying ^(%BuildAttempt% of %BuildAttemptsMax%^) in %WaitForLicenseTimeout% seconds
waitfor SignalNeverComming /T %WaitForLicenseTimeout% >nul 2>&1
goto PerformBuild
)
goto NoLicenseProblem
:NoLicenseAvailable
echo:Error: After %BuildAttempt% attempts, the Flex license still appear to be unavailable. Failing the build!
echo:
popd
exit /b 1
:NoLicenseProblem
:: Parse exit codes
set KnownErrors=0 1 2 3 11 12 13 15 20 41
echo:Kiel compiler exited with error code %ReportedError%
for %%a in (%KnownErrors%) do (
if [%ReportedError%] equ [%%a] goto Error%ReportedError%
)
goto UnknownError
:Error0
echo Compilation successful
goto ExitButContinueJob
:Error1
echo Warnings were found
goto ExitButContinueJob
:Error2
echo Errors were found
goto ExitCritical
:Error3
echo Error 3 = Fatal Errors
goto ExitCritical
:Error11
echo Error 11 = Cannot open project file for writing
goto ExitCritical
:Error12
echo Error 12 = Device with given name in not found in database
goto ExitCritical
:Error13
echo Error 13 = Error writing project file
goto ExitCritical
:Error15
echo Error 15 = Error reading import XML file
goto ExitCritical
:Error20
echo Error 20 = Can not convert the project file.
goto ExitCritical
:Error41
echo Error 41 = Can not create the logfile requested using the -l switch.
goto ExitCritical
:UnknownError
echo Error %ReportedError% = Unknown error
goto ExitCritical
:ExitCritical
echo:
if [%ReportedError%] neq 0 exit /b %ReportedError%
:ExitButContinueJob
echo:
popd
exit /b 0