Why is this section of my batch script not being executed? - batch-file

I'm having a problem calling a specific section of my batch script. The goal is to run this script and have it install the given executables and run some commands:
#ECHO OFF
ECHO 1. 32-bit
ECHO 2. 64-bit
SET /P INSTALL="Please choose the correct installation: "
SET /P PROXY="Enter proxy gateway path if applicable to your environment: "
2>NUL CALL :CASE_%INSTALL%
IF ERRORLEVEL 1 CALL :ERROR
REM CASE_%INSTALL% or ERROR returns to here
ECHO Done.
PAUSE
EXIT \B
:CASE_1
REM Install NodeJS 32-bit
CALL node-v4.4.7-x86.msi
REM Install Ruby
CALL rubyinstaller-2.3.1.exe
GOTO NEXT_TASK
:CASE_2
REM Install NodeJS 64-bit
CALL node-v4.4.7-x64.msi
REM Install Ruby
CALL rubyinstaller-2.3.1-x64.exe
GOTO NEXT_TASK
:NEXT_TASK
REM Install SASS CSS precompiler (v3.4.19)
CALL gem install sass --http-proxy %PROXY%
REM Install Compass CSS plug-in (v1.0.3)
CALL gem install compass --http-proxy %PROXY%
REM Run Node package install
CD ..
IF NOT "%PROXY%"=="" (
CALL npm config set proxy %PROXY%
CALL npm config set https-proxy %PROXY%
)
CALL npm install
REM Instal grunt globally
CALL npm install -g grunt
REM Install jshint globally because of warning
CALL npm install -g jshint
GOTO END_CASE
:ERROR
ECHO Unable to complete installation of tools
GOTO END_CASE
:END_CASE
VER > NUL
GOTO:EOF
This will execute either CASE_1 or CASE_2 perfectly, depending on what the user chooses. NodeJS is installed, and so is Ruby. Then, when it reaches the end of one of those sections, I will get this output:
Done.
Press any key to continue . . .
The script never follows the GOTO to run NEXT_TASK, and thus never installs SASS or Compass and also doesn't run the npm commands.
I have noticed that running this script twice and skipping the NodeJS installation on the second run actually executes the NEXT_TASK section correctly... - this leads me to believe there's some sort of timing issue going on here.
Other things I have tried:
Replacing the GOTOs with CALLs (does not behave correctly)
Replacing the section names referenced by GOTO to use the format :{Section} (no change)
Replacing the CALLs before the Node and Ruby executable with START /WAIT (doesn't seem like that makes any difference)
So, I'm stumped. Any help would be appreciated.

I ended up solving my problem (using a workaround) by breaking the script into two separate scripts. Unfortunately, I'm unable to call one script from the other and be certain that the gem and npm commands will be set (even through the use of START to create a new environment, which was surprising to me), so this is my solution. Thanks to #aschipfl for pointing out the unnecessary use of CALL when referencing executables.
install_tools.bat (Run first)
#ECHO OFF
ECHO 1. 32-bit
ECHO 2. 64-bit
SET /P INSTALL="Please choose the correct installation: "
2>NUL CALL :CASE_%INSTALL%
IF ERRORLEVEL 1 CALL :ERROR
REM CASE_%INSTALL% returns to here
PAUSE
EXIT
:CASE_1
REM Install NodeJS 32-bit
START "" /W node-v4.4.7-x86.msi
REM Install Ruby
rubyinstaller-2.3.1.exe
GOTO SUCCESS
:CASE_2
REM Install NodeJS 64-bit
START "" /W node-v4.4.7-x64.msi
REM Install Ruby
rubyinstaller-2.3.1-x64.exe
GOTO SUCCESS
:ERROR
ECHO.
ECHO Unable to complete installation of tools
GOTO END_CASE
:SUCCESS
ECHO.
ECHO Done. Please run init_tools.bat.
GOTO END_CASE
:END_CASE
VER > NUL
GOTO:EOF
init_tools.bat (Run second)
#ECHO OFF
SET /P PROXY="Enter proxy gateway path if applicable to your environment: "
REM Install SASS CSS precompiler (v3.4.19)
CALL gem install sass --http-proxy %PROXY%
REM Install Compass CSS plug-in (v1.0.3)
CALL gem install compass --http-proxy %PROXY%
REM Run Node package install
CD ..
IF NOT "%PROXY%"=="" (
CALL npm config set proxy %PROXY%
CALL npm config set https-proxy %PROXY%
)
CALL npm install
REM Instal grunt globally
CALL npm install -g grunt
REM Install jshint globally because of warning
CALL npm install -g jshint
ECHO.
ECHO Tools initialized.
PAUSE
EXIT

Try this code and let me know if it works, and what the error is. You might need to increase timeout after 1st call.
#ECHO OFF
setlocal EnableExtensions EnableDelayedExpansion
ECHO 1. 32-bit
ECHO 2. 64-bit
SET /P INSTALL="Please choose the correct installation: "
SET /P PROXY="Enter proxy gateway path if applicable to your environment: "
CALL :CASE_%INSTALL%
IF ERRORLEVEL 1 (ECHO Unable to complete ruby installation & GOTO :END
) ELSE (TIMEOUT /t 3 /nobreak >nul
set PATH="%PATH%;C:\Ruby23-x64\bin;C:\Program Files\nodejs\"
CALL :NEXT_TASK
IF ERRORLEVEL 1 ECHO Unable to complete other tools installation & GOTO :END)
ECHO Installation completed.
:END
TIMEOUT /t 5 /nobreak >nul
EXIT /B
:CASE_1
REM Install NodeJS 32-bit
START "" /W node-v4.4.7-x86.msi
REM Install Ruby
rubyinstaller-2.3.1.exe
EXIT /B
:CASE_2
REM Install NodeJS 64-bit
START "" /W node-v4.4.7-x64.msi
REM Install Ruby
rubyinstaller-2.3.1-x64.exe
EXIT /B
:NEXT_TASK
REM Install SASS CSS precompiler (v3.4.19)
START "" /W gem install sass --http-proxy %PROXY%
REM Install Compass CSS plug-in (v1.0.3)
START "" /W gem install compass --http-proxy %PROXY%
REM Run Node package install
CD ..
IF NOT "%PROXY%"=="" (
START "" /W /B npm config set proxy %PROXY%
START "" /W /B npm config set https-proxy %PROXY%
)
START "" /W npm install
REM Instal grunt globally
START "" /W npm install -g grunt
REM Install jshint globally because of warning
START "" /W npm install -g jshint
EXIT /B

Related

MSI installer does not install when executed from a batch file

I am currently creating an improvised installer for a cople software packages. To do this I have to install a couple MSI packages first before doing a couple file operations.
To install an MSI package I am using the following command:
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
This command works and installs the package instantly and witout any problems via CMD.
But when I put this command in my batch file and execute it as an administrator, I get the following error:
This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package
What cold be the problem? Using the same command via the console works flawlessly, only the batch file throws the error...
EDIT: I have also tried the /a parameter in order to install it as an administrator and it does not work either. Full command in batch file:
start /wait msiexec /qn /a "Myinstaller V2.1.msi"
EDIT2: I just realized that it only does not work when I start the batch file with Right click > Run as administrator
When I open a console with administrative rights and start my batch file it works for some reason...
Is there a way to make it work with the Right click > Run as administrator method?
SOLUTION: Thanks to RGuggisberg's answer I now know that the directory changes once the file is executed as an administrator. With a small change the installer gets fired up as an admin and works perfectly starting the installer from a relative path in the same directory:
#echo off
pushd %~dp0
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
pause
I've now also implemented a feature to detect wether or not the installation fails or not:
#echo off
pushd %~dp0
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
if %ERRORLEVEL% EQU 0 echo SUCCESSFULL
if NOT %ERRORLEVEL% EQU 0 echo MyProgram installation FAILED
pause
The current directory changes when you run as administrator. If you want to prove that to yourself, see this post
Difference between "%~dp0" and ".\"?
Include the full path to your filename and it will work.

Build Go Project with jenkins and it report an error that did't report when building in cmd or goland

When I build a go project using a bat in jenkins, it reports an error:
go tool: no such tool "asm"
however, when I build the go project alone with cmd or goland, the error does't happen, Why this error happen?
the Command in Jenkins is
C:/Users/Administrator/Desktop/build.bat
and the bat content is:
#echo off
cd C:/Users/Administrator/Desktop/GOPATH/src/Flipped_Server/main
go build main.go
xcopy main.exe "C:/Users/Administrator/Desktop/Flipped-Server" /s /e /c /y /h /r
echo "Succeed to build and deploy the flipped-Server"
When I uncheck the option
Set up Go Programming language tools
It works! build Successfully

How to install kb* Windows updates with batch script if it's not installed already?

I have tons of FortiClient clients that I need to install on individual computers, each client has it's own PC at home and some of them have Windows 7 32-bit. (Yes, I even saw an XP one.)
I've managed to create a batch script that will install the free FortiClient with all the configuration inside by importing the registry key at the beginning of the script and it's working great, but on Windows 7 32-bit, you can install it only if you have some Windows updates installed before.
So, I've came to a conclusion that I can create a batch file that will check if the KB is installed or not.
So here is the code:
#echo off
Pushd "%~dp0"
wmic qfe get hotfixid | find "KB3033929"
if %errorlevel% neq 1 ECHO KB3033929 Found
if %errorlevel% equ 1 ECHO KB3033929 NOT Found, installing KB3033929
(
wusa .\Windows6.1-KB3033929-x86.msu /quiet /norestart
)
regedit /s .\korona.reg
msiexec.exe /qb /i "%~dp0FortiClient.msi" /norestart INSTALLLEVEL=3
pause
But each time I am running the code, I am getting an error that the syntax is wrong, even though I run only the line with the wusa command.
PS: If you have any other ideas, this would be great - I don't want to use PowerShell.
Your if syntax is wrong:
if %errorlevel% equ 1 ECHO KB3033929 NOT Found, installing KB3033929
(
wusa .\Windows6.1-KB3033929-x86.msu /quiet /norestart
)
should probably be
if %errorlevel% equ 1 (
ECHO KB3033929 NOT Found, installing KB3033929
wusa .\Windows6.1-KB3033929-x86.msu /quiet /norestart
)
3 Months later: in the meantime, this KB is superseeded several times. So actually KB3033929 might not be installed, but a successor is that prevents KB3033929 from installing (which is ok from a technical point of view as the successor includes the function, but ruins your logic).
Source and workaround

How to check if the process was executed successfully in batch

I try to create a windows batch script that install nodeJs, express and bower, but for now doesn't work and I cant know what is the problem because the terminal closed after some seconds.
If I run all the instructions of the batch script one by one into a CMD all work OK.
How can I validate that every process was executed successfully? I mean if the install of express fail, get a message like "fail express framework install"
I think that the problem is the script doesn't wait finished install the fisrt block of code when try to install the next block.
Any idea for resolve?
This is my script:
::Windows Batch Script for Install nodeJs, express framework for nodeJs, bower and Ionic
#setlocal
#echo off
:: Install nodeJs & NPM
echo Instalando NodeJs ^& NPM (Node Package Manager)...
echo.
start /wait msiexec /i C:\EMWA\installers\node.msi /passive /norestart
::Install express framework
echo Instalando express framework nodejs
echo.
npm install express-generator -g
:: Install bower
echo Install bower
echo.
npm install -g bower
:: create a test project
IF NOT EXIST C:\EMWA\projects
mkdir C:\EMWA\projects
IF NOT EXIST C:\EMWA\projects\emwa_test
mkdir C:\EMWA\projects\emwa_test
pause > null
This is the purpose of the ERRORLEVEL keyword in the IF statement. See IF /? for details:
C:\...>if /?
Performs conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
....
NOT Specifies that Windows should carry out
the command only if the condition is false.
ERRORLEVEL number Specifies a true condition if the last program run
returned an exit code equal to or greater than the number
specified.
....
%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL, provided that there is not already
an environment variable with the name ERRORLEVEL, in which case you
will get its value instead. After running a program, the following
illustrates ERRORLEVEL use:
goto answer%ERRORLEVEL%
:answer0
echo Program had return code 0
:answer1
echo Program had return code 1
You can also use numerical comparisons above:
IF %ERRORLEVEL% LEQ 1 goto okay
....
C:\...>
This works as long as the commands set an exit status, which many do.

How to implement condition for java installation

I have to check whether java is installed or not ?
If java is installed then i have to skip it and if not then to install it in silent mode.
I know how to install java in silent mode
**cls
C:\TEMP>jdk-7u4-windows-x64.exe /quiet
echo installation complete
pause**
but how to define condition above in batch file though i'm very new to scripting.
Any link or reference will also work.
This works in Windows 8 to launch the installer:
#echo off
java >nul 2>nul & if errorlevel 9009 jdk-7u4-windows-x64.exe /quiet
REM get the current installed Java version
for /f "tokens=3" %%a in ('java -version 2^>^&1 ^| find /i "version"') do SET "JAVA_current=%%~a"
IF NOT DEFINED JAVA_current (ECHO Java NOT installed.) ELSE ECHO Java version %JAVA_current% installed.

Resources