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.
Related
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.
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
How can I make a batch file that automatically installs java,office and adobe stuff without gui, instead of that I want to let whole proces take place inside the command prompt. For example I want to have a bar inside the command prompt telling me how far the install process is. How do I make that, I can't find it on the internet. Here is a example of what I already have:
#echo off
echo Installing application...
msiexec.exe /passive /i "%~dp0skypesetup.msi"
echo Install failed.
pause
Here I have the msi file if you wanna help me: http://www.skype.com/go/getskype-msi
Does anyone knows how to make a program with percent bar inside the command promt?
For the external GUI you can check MsiSetExternalUI function (follow the links). For installing silently the basic msiexec.exe command line is:
msiexec.exe /I "C:\MsiFile.msi" /QN /L*V "C:\msilog.log"
Quick explanation:
/I is for install
/QN is silent mode
/L*V is verbose logging
Some links:
Silent Install of MSI
What is different about a silent installation session
Common msiexec.exe command lines
Here is an answer that discusses alternative ways to install a package without using msiexec.exe
How to disable an extra window to restart system even after selecting not to do so in files in use dialog in installshield
Stupid me :( , I'v searched a lot and finally found it. I thought I had to use msi but I can also do it with .exe files. Stupid me.
Here's the code if someone wants it or needs it:
#ECHO OFF
echo Do you want to install ccleaner
pause
ccleaner.exe /S /L=1043
echo You've succesfully installed ccleaner
pause
I need to figure out this seemingly very simple issue on windows .bat file. I have been using Linux for past 10 years full-time, so I am in pretty unfamiliar territory when it comes to .bat scripts.
We have some units tests that need to run on from this .bat file, and a build needs to be generated after the tests have run.
The bat file itself is very simple, I was thinking of just chaining the commands:
cls
echo "Running test suite - CRMSync"
echo
echo
REM from command: --static-backup
phpunit --bootstrap test/bootstrap_test.php --verbose --log-junit
echo "Running phploc for CRMSync"
phploc --count-tests --verbose > C:\CRMsync\testResults\phploc\index.html
echo "Executing phing"
phing
Now, simple enough except nothing is executed past phpunit command. How do I deal with this?
The unit tests run fine, but I am suspecting it could even be in the unit test lib that process is killed. Is there a way to fork the process somehow or get the other commands below to run?
Thanks SO'ers, as always, any help greatly appreciated.
I had the same problem for a development script that I made. And I tried all given solutions without being successful. At the end, I did it with cmd /C.
From the windows docs it will:
Run Command and then terminate
So for example, you can use it as follows:
cls
echo "Running test suite - CRMSync"
echo
echo
REM from command: --static-backup
cmd /C phpunit --bootstrap test/bootstrap_test.php --verbose --log-junit
echo "Running phploc for CRMSync"
cmd /C phploc --count-tests --verbose > C:\CRMsync\testResults\phploc\index.html
echo "Executing phing"
cmd /C phing
I hope you find this useful.
Similar to the post ujifgc, I use "start /b ..." in these situations. If you encapsulate the call to phpunit in another batch file, you can use "call".
Is phpunit itself a batch file (I don't do much PHP, so am not familiar with the tool)? If so, try using:
call phpunit --bootstrap test/bootstrap_test.php --verbose --log-junit
Try start /WAIT phpunit ... to fork process and wait for it or just start phpunit ... to fork and continue. Help is here: start /?
I used
start /b code .
cmd /k ng serve --build-optimizer --aot
to start Visual Studio Code and then a node js server.
But the Command line glitched with the % update text and more.
I then used
cmd /C code .
cmd /k ng serve --build-optimizer --aot
and the glitching stopped for the server progress update.
I have a batch file that will detect if the user has the .Net framework installed by going to the directory and checking if the CONFIG directory exists.
If the directory doesn't exist then the user doesn't have the .Net framework installed. The batch file will then continue on to install the .Net framework. However, there is a problem as the .Net frameworks need to be installed before running the setup to install my dialer. So I have put a PAUSE statement so the user will press any button to continue after the framework has been installed.
However, our client doesn't like this as some of their customers don't understand and they press a key before the framework has finished installing. This then causes the setup to fail as the framework haven't been installed first.
I am using the PAUSE that will wait for the user input. However, is there a way that the batch will wait until the framework is finished automatically instead of using the PAUSE command?
Many thanks for any suggestions,
#ECHO OFF
REM Copy the configuration file
copy config.xml "%AppData%\DataLinks.xml"
REM Search for the CONFIG file, if this doesn't exit then the user doesn't have the .Net framework 2.0
SET FileName=%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG
IF EXIST %FileName% GOTO INSTALL_DIALER
ECHO.You currently do not have the Microsoft(c) .NET Framework 2.0 installed.
ECHO.This is required by the setup program for MyApplication.
ECHO.
ECHO.The Microsoft(c) .NET Framework 2.0 will now be installed on you system.
ECHO.After completion setup will continue to install MyApplication on your system.
ECHO.
REM Install the .Net framework and wait for the user to input before install the dialer
PAUSE
ECHO Installing... Please wait...
SET FileName =
Start .\NetFx20SP2_x86.exe
ECHO Once the .Net Framework has completed. Press any key to continue to install the dialer.
PAUSE
Start .\setup.exe
ECHO ON
EXIT
REM .Net framework has been skipped contine to install the dialer.
:INSTALL_DIALER
ECHO *** Skiped Dotnet Framework 2.0.50727 ***
ECHO Installing... Please wait...
SET FileName=
Start .\setup.exe
ECHO ON
EXIT
you can use
START /WAIT NetFx20SP2_x86
too.
remember that
REM comment
is equal to
::comment
and that using .\ is unnecessary, and file extensions are too, unless there are dirs/files with conflicting names.
You also do not need cleaning the "filename" variable twice (the "=" pointing nothing twice) and
ECHO.something
is equal
ECHO something
only except for blank lines
you could also use
START /WAIT NetFx20SP2_x86.exe
the slash is to indicate that it is an option to the start command, not the target file. to see more of these options, look here (i would refer to it as a 'forward slash')
Just remove the START line from the call, like so:
.\NetFx20SP2_x86.exe
Start .\setup.exe
This will make the installation blocking, as in the batch file will stop processing until the NetFx20SP2_x86.exe program terminates.
Ok I optimized your script, sort of:
#echo off
rem Copy the configuration file
copy config.xml "%AppData%\DataLinks.xml"
rem Search for the CONFIG file, if this doesn't exist then the user doesn't have the .Net framework 2.0
if not exist "%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG" (
echo You currently do not have the Microsoft(c) .NET Framework 2.0 installed.
echo This is required by the setup program for MyApplication.
echo.
echo The Microsoft(c) .NET Framework 2.0 will now be installed on you system.
echo After completion setup will continue to install MyApplication on your system.
echo.
Start /w .\NetFx20SP2_x86.exe
)
Start /w .\setup.exe
1: As you only use the CONFIG file for test purpose once, using a variable for it is useless. moreover, the "=" sign must be stuck to the variablename, otherwise you will create a variable with a space in it. "set filename =" and "set filename=" are two different things. Yes, a variable name can contaion multiple words and spaces, but one need to be extremely cautious about it, it can be hard to handle.
2: I would not recomment using '::' as a comment, sorry Camilo, because it does not work well within parenthesis. It would here, but if you take this track for all your batches, you might get into a problem later and you will wonder why your code is broken if you don't know this.
3: You don't need to end a script with exit, nor resetting to echo on, when a script reaches the end, it does reset the echo state and exit by itself.
Hope this helps.