Why does my simple batch script quit unexpectedly after line 59? - windows

I made this batch file to execute certain commands for my kit for web dev, however, the bat file quits after line 59. Why is it quitting?
I noticed, however, that if I remove line 59 from the file and run it. It works fine. The only thing is, I have to physically type that line in after the bat file finishes.
You can try to reproduce the problem by downloading my kit from my GitHub and extracting the contents of the zip to a location that you prefer.
Just navigate to the Gulp/Starter Kit directory and run the setup.bat file.
Batch File:
#echo off
TITLE Starter Kit
runas /noprofile /user:Administrator cmd
goto :checkNodeVersion
:checkNodeVersion
cls
echo Current NodeJS version:
call npm -v
timeout 5
if errorlevel 1 goto :installNode
goto :updateNode
:installNode
cls
echo Installing NodeJS:
powershell -Command "(New-Object Net.WebClient).DownloadFile('https://nodejs.org/dist/v5.3.0/node-v5.3.0-x64.msi', 'node-v5.3.0-x64.msi')"
echo downloading file, node-v5.3.0-x64.msi, from https://nodejs.org/dist/v5.3.0/node-v5.3.0-x64.msi....
set "fileName=node-v5.3.0-x64.msi"
%fileName%
echo NodeJS was successfully installed.
timeout 5
goto :updateNode
:updateNode
cls
echo Updating NodeJS:
call npm cache clean -f
call npm install -g n
call n stable
cls
echo NodeJS was successfully updated to version #:
node -v
timeout 5
goto :installBower
:installBower
cls
echo Installing Bower:
call npm install -g gulp bower
echo Bower was successfully installed.
goto :installGulp
:installGulp
cls
echo Installing GulpJS:
call npm install -g gulp
echo GulpJS was successfully installed.
timeout 5
goto :installDependencies
:installDependencies
cls
echo Installing Project Dependencies:
call npm install && bower install
::Quits unexpectedly after line 59
call npm install --save-dev gulp-rucksack
call npm install --save-dev gulp-imagemin
call npm install --save imagemin-pngquant
echo Project Dependencies were successfully installed.
timeout 5
goto :primaryFunction
:primaryFunction
cls
echo Starter Kit Log:
echo NodeJS is installed.
echo GulpJS is installed.
echo Gulp Dependencies are installed.
set /p response="Would you like to continue? <y/n>"
if /i "%response%"=="y" (
cls
set "filePath=%~dp0"
cd %filePath%
gulp help
cmd /k
)
if /i "%response%"=="n" goto :exitFunction
:exitFunction
cls
echo Starter Kit is Closing
exit

Based on the following quote from https://www.jetbrains.com/webstorm/help/bower.html, bower is a batch script (.cmd file).
In this field, specify the location of the Bower executable file
(bower.cmd or other depending on the operating system used).
So you need to change line 59 to read as follows:
call npm install && call bower install
Without CALL, you never return from bower.

Related

Can't pass options to "DEL" from package.json script

Windows 11, Yarn 3.2.4
My package.json has this
"scripts": {
"predist": "del /Q \"WWW\\*\"",
Trying to run it gives this:
PS C:\MyProject> yarn predist
Could Not Find C:\/Q
C:\MyProject\WWW\*, Are you sure (Y/N)?
If I omit the /Q files are deleted as expected once I answer the confirmation prompt
How to stop the /Q being interpreted as a filename?
rimraf solves this completely. https://www.npmjs.com/package/rimraf
> yarn add --dev rimraf
"predist": "rimraf WWW/*",

How can I create a windows 10 script which runs a command under specified path?

I created a npmruns.bat file with a content:
C:\myfolder>npm run s
I wanted to create a script file which run a command: npm run s under specified location: C:\myfolder , but it doesn't work. I run it by double click (I have an admin rights).
I tried to create a script which can be executed from any location (other than C:\myfolder).
pushd and cd /d will be the options for this:
#echo off
pushd "C:\myfolder"
call npm.cmd run s
popd
This will push to the path of the batch-file as the working directory, popd is not required if you do not need to go back to the starting working directory, which as admin, will be "%systemroot%\system32"
Alternatively you can run:
cd /d "c:\myfolder"

Use of PUSH and POPD in windows batch script

I have a requirement where I need to run a batch script which would compile a custom module and then come back to the project's root location and run the project.
For this, I have written something like below
SET curDir=%~dp0
PUSHD %curDir%
echo %curDir%
cd modules\custom-module
yarn build
POPD
echo %CD%
yarn start
but after the yarn build command, it stays in the modules\custom-module directory.
Before changing the directory call 'setlocal'. So your script will look like following
cd <- prints current dir
setlocal
cd modules\custom-module
yarn build <- if yarn is script, do 'call yarn build' instead
endlocal
cd
yarn start

Check if IIS is installed via .BAT

I'm using the cmd commands bellow to install IIS on my machine, as suggested Here.
start /w pkgmgr /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-HttpRedirect;
IIS-ApplicationDevelopment;IIS-ASPNET;IIS-NetFxExtensibility;IIS-ASP;IIS-CGI;IIS-ISAPIExtensions;IIS-ISAPIFilter;IIS-ServerSideIncludes;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-LoggingLibraries;IIS-RequestMonitor;IIS-HttpTracing;IIS-CustomLogging;IIS-ODBCLogging;IIS-Security;IIS-BasicAuthentication;
IIS-WindowsAuthentication;IIS-DigestAuthentication;IIS-ClientCertificateMappingAuthentication;IIS-IISCertificateMappingAuthentication;IIS-URLAuthorization;IIS-RequestFiltering;IIS-IPSecurity;
IIS-Performance;IIS-HttpCompressionStatic;IIS-HttpCompressionDynamic;IIS-WebServerManagementTools;IIS-ManagementConsole;IIS-ManagementScriptingTools;IIS-ManagementService;IIS-IIS6ManagementCompatibility;IIS-Metabase;IIS-WMICompatibility;IIS-LegacyScripts;IIS-LegacySnapIn;IIS-FTPPublishingService;IIS-FTPServer;IIS-FTPManagement;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI
I want to check first if it's already installed using CMD to include an if in my batch script. How can I do this using cmd?
As I have no installed IIS I can't fully test this. You can use the registry entries to check the version, installation dir and so on. You can use this in order to see if the IIS is installed:
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\VersionString" >nul 2>&1 && (
echo installed
)||(
echo NOT installed
)
Based on #npocmaka said in the first comment of my question, and also by following the explanation on this WEBPAGE
I've created this .bat file.
#echo off
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp /v VersionString
if %ERRORLEVEL% EQU 1 goto NOT_EXISTS
:EXISTS
echo "IIS installed :-)"
goto:END
:NOT_EXISTS
echo "IIS not installed :-( ... Begin installation"
start /w pkgmgr /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-HttpRedirect;
IIS-ApplicationDevelopment;IIS-ASPNET;IIS-NetFxExtensibility;IIS-ASP;IIS-CGI;IIS-ISAPIExtensions;IIS-ISAPIFilter;IIS-ServerSideIncludes;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-LoggingLibraries;IIS-RequestMonitor;IIS-HttpTracing;IIS-CustomLogging;IIS-ODBCLogging;IIS-Security;IIS-BasicAuthentication;
IIS-WindowsAuthentication;IIS-DigestAuthentication;IIS-ClientCertificateMappingAuthentication;IIS-IISCertificateMappingAuthentication;IIS-URLAuthorization;IIS-RequestFiltering;IIS-IPSecurity;
IIS-Performance;IIS-HttpCompressionStatic;IIS-HttpCompressionDynamic;IIS-WebServerManagementTools;IIS-ManagementConsole;IIS-ManagementScriptingTools;IIS-ManagementService;IIS-IIS6ManagementCompatibility;IIS-Metabase;IIS-WMICompatibility;IIS-LegacyScripts;IIS-LegacySnapIn;IIS-FTPPublishingService;IIS-FTPServer;IIS-FTPManagement;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI
goto:END
:END
pause

Update Batch File: Implementing Conditional Arguments

I work in IT help-desk, new to coding but put together this batch file to get our nightly updates completed faster:
#echo off
echo.
echo RTC Customer Care - Variety Pre-Eigen Updater
echo (Continue along with 'ENTER' to reach desired update.)
echo.
pause
echo.
echo ====================================
echo Transfer Required Files to C: Drive?
echo ====================================
echo.
echo.
pause
cd /d h:\smsback
call vwiw3net2.bat
echo.
echo (Finished copying files)
echo Note: If transfer failed, re-run updater.
pause
echo.
echo ===========================
echo Execute Part 1/2 of Update?
echo ===========================
echo.
echo.
pause
echo (Follow prompts till completion)
start /d "c:\smsback\1_win3_1" WindowsInstaller-KB893803-v2-x86.exe
pause
echo.
echo ===========================
echo Execute Part 2/2 of Update?
echo ===========================
echo.
echo.
pause
echo (Follow prompts till completion)
echo Note: 2nd update takes a few minutes to display.
start /d "c:\smsback\2_net2" NetFx20SP2_x86.exe
pause
echo.
echo.
echo ==================================================
echo ATTENTION: Register will RESTART to finish update.
echo ==================================================
echo.
pause
shutdown.exe /r /t 05
(goto) 2>nul & del "%~f0"
What would be some good conditional arguments for verifying that an update installed? File size? Just don't want to have to run the script to get to update 2 and open/close out of previous steps.
First, opening a command prompt window and running there start /? or help start results in getting help displayed for this command. It can be read on output help about /Dpath and other options of this command. There is no space between /D and path.
Second, start interprets first double quoted string as title. Therefore "" or "useful title" should be specified on command line with start as first parameter if any other parameter is enclosed in double quotes.
Third, if start is used to install from within a batch file, it is better to use this command with parameter /wait as it is not possible to run multiple installations parallel. msiexec used to install each security update does not allow running more than one install/repair/uninstall operation at the same time.
Fourth, most executables of Windows security updates are console applications and can be run from within a batch file without the need to use command start at all. So instead of
start /d "c:\smsback\1_win3_1" WindowsInstaller-KB893803-v2-x86.exe
it would be better to use just:
C:\smsback\1_win3_1\WindowsInstaller-KB893803-v2-x86.exe
Fifth, the executables of Windows security updates all exit with a value greater 0 on an error and with 0 on success. Therefore after running a security update executable without command start as written above a line like the following could be used to check return value of the executable.
if errorlevel 1 echo Failed to install KB893803-v2-x86, error code %ERRORLEVEL%.
See Windows Installer Error Messages and MsiExec.exe and InstMsi.exe Error Messages.
And finally which updates are installed already can be queried from Windows registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Updates and its subkeys.

Resources