Resume batch script after computer restart - windows

I have a bunch of old machines running Windows 2000 Pro and IE 5.0 which I want to upgrade to IE 6 with Silverlight. I downloaded the IE6 and Silverlight installers from Microsoft's web sites and fortunately they both have command line options to allow them to run in "silent mode".
I put the two commands in a DOS batch script and ran it, but the IE6 installer requires makes an automatic computer restart so the question is how to resume the script and run the 2nd command (install Silverlight).
My batch file is very simple right now:
ie6setup.exe /Q
silverlight.exe /q
From what I know, batch files can't resume execution after restarting the computer. Is there a way to make them do that? of is there another way to accomplish what I need.
Thank you

Based on Tim's post which, when tested, appended "two" to the batch file resulting in a failure to find the batch label "onetwo", so amended to read & write the "current" variable from a seperate text file, leaving the batch file untouched;
#echo off
call :Resume
goto %current%
goto :eof
:one
::Add script to Run key
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
pause
shutdown -r -t 0
goto :eof
:two
echo three >%~dp0current.txt
echo -- Section two --
pause
shutdown -r -t 0
goto :eof
:three
::Remove script from Run key
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt
echo -- Section three --
pause
goto :eof
:resume
if exist %~dp0current.txt (
set /p current=<%~dp0current.txt
) else (
set current=one
)

You could put the second command in a exclusive batch file, and add an entry to regedit to execute this batch file automatically upon Windows' start, making silverlight be executed after the computer restarts.
Have you heard of msconfig? On some systems the regedit PATH you are looking for is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
But you may want to check that. If you want to make a batch file to write that key on the registry, you probably should take a look at this tutorial.

If you do the IE6 installation with the command ie6setup.exe /q /r:n then it won't reboot automatically (see this page for details). Then theoretically you could install SilverLight immediately, and then reboot afterwards; but there is a chance that the SL install will refuse due to the need of a reboot, but it won't hurt to try it...

I know its a bit old but this works amazingly:
#echo off
call :Resume
goto %current%
goto :eof
:one
echo two >>myfile.cmd
::REBOOT HERE
goto :eof
:two
echo resumed here
goto :eof
:resume
rem THIS PART SHOULD BE AT THE BOTTOM OF THE FILE
set current=one

#echo off
set "_RunOnce=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"
rem./ :: if no argument was passed, this line will be ignored, but if so, it will be executed here == ^> & %~1
:1st_command
ie6Setup.exe /Q
shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :2nd_command\"" /f & goto :eof
:2nd_command
SilverLight.exe /Q
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :3rd_command\"" /f & goto :eof
:3rd_command
Skype-8.92.0.401.exe /VerySilent /NoRestart /SuppressMsgBoxes /DL=1 & goto :eof
It is possible to do it without creating or manipulating readings in additional files, just writing and reading in the key and using arguments passed in the execution to control the command necessary for the relevant execution, using goto command as an argument %1
#echo off
set "_RunOnce=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"
rem./ if no argument was passed, below will be ignored, but if so, it will be executed here == ^> & %~1
:1st_command
mode con cols=50 lines=1 | title starting %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "stackoverflow.com" /new-tab
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :2nd_command\"" /f & goto :eof
:2nd_command
mode con cols=50 lines=1 | title starting %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "meta.stackexchange.com" /new-tab
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :3rd_command\"" /f & goto :eof
:3rd_command
mode con cols=50 lines=1 | title %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "www.amazon.com" /new-tab
goto :eof

Related

How to stop a cmd file from showing Network error messages

Error Message I want to remove:
I get this error when I run a code that runs a shortcut that's shared over network.
How can I stop cmd script from showing Network Errors like these, even if I know there is an error?
#echo off
echo.
echo 1. Add
echo 2. Remove
echo.
set /p var= Type option number here -
if %var%==1 (goto :add)
if %var%==2 (goto :remove)
if else (goto :EOF)
:add
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\ /v RestoreConnection /d 0
echo.
echo SUCESSFULLY ADDED!
timeout /t 2 /nobreak >nul &exit
:remove
reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\ /v RestoreConnection
echo.
echo SUCESSFULLY REMOVED!
timeout /t 2 /nobreak >nul &exit
This .bat script could come handy btw. Atleast it's easier than changing the registry manually. You can add and delete the value any time you want, so its convenient. Save this as a (.bat) file and use it...
Thanks #FiddlingAway for giving reference for the script : )

How to launch an application from a browser

The correct method is to register your custom URL Protocol in windows registry as follows:
enter image description here
Once the above keys and values are added, from the web page, just call "customurl:\parameter1=xxx&parameter2=xxx" . You will receive the entire url as the argument in exe, which you need to process inside your exe. Change 'customurl' with the text of your choice.
Not sure how this works, i am getting lost somewhere, can someone help me with this with an example of an app?
Thanks in advance!
You are on the right track, you just need to change the "ExeName.exe" part to the path of your .exe. Your .exe application needs to parse the command line, this is where the URL is.
Here is a example batch file that implements a protocol handler:
#echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
if not "%~1."=="." goto proto
choice /C IUE /M "Install? Uninstall? Example launch?"
if errorlevel 3 goto launch
if errorlevel 2 goto uninstall
if errorlevel 1 goto install
goto :EOF
:proto
echo Launched protocol with %*
pause
goto :EOF
:install
reg add "HKCU\Software\Classes\customUrl" /ve /d "customUrl handler" /f
reg add "HKCU\Software\Classes\customUrl" /v "URL Protocol" /f
reg add "HKCU\Software\Classes\customUrl\shell\open\command" /ve /d """"%~0""" %%1" /f
pause
goto :EOF
:uninstall
reg delete "HKCU\Software\Classes\customUrl" /f
pause
goto :EOF
:launch
start "" "customUrl:foo=bar"
goto :EOF
Save as myproto.cmd and run it and press i to install it. Then run it again and press e to launch a URL or make a dummy .html file with a customUrl: link you can click on.

Can I prevent a Windows batch file from execution through a double-click?

Is it possible to setup a Windows .BAT file in such a way so that it's possible to execute when called from CMD, but does not run when someone double-clicks the file?
The environment variable %cmdcmdline% contains the command line used to launch CMD.exe. If a batch file was launched from a click in Explorer or on the Desktop, it will be:
C:\Path\To\cmd.exe /c ""c:\path\to\batch\file.bat" "
The full path to the batch file can also be accessed as %~f0 within the batch file itself to compare with that variable.
Would you like get try this coding suggest doing this job...
Basically, use findstr to check the if the variable %cmdcmdline% value match this .cmd or .bat, if this return positively then goto :eof (exit), but, if not, then run your code...
#echo off & cd /d %~dp0"
echo/%cmdcmdline% | findstr /i "\.cmd \.bat" 2>nul && (
title... by click.. & echo/%cmdcmdline% | findstr "%0"
echo/ bat: "%0" running by click
timeout /t 5 /nobreak>nul & goto :eof
) || (
title... by command line.. && echo/%cmdcmdline%| findstr "%0"
echo/ bat: "%0" running by command line
timeout /t 5 /nobreak>nul & goto :run_code
)
:run_code
echo/ your code enter here
:: ....
goto :eof

Reboot & continue batch script

Below is the script created for increasing the swaps & mounting the driver. Now in this script I want to add the feature that after setting the page file the system will be rebooted & once reboot is done, it will resume with next step which is mounting driver. Can you please help in this.
#echo off
wmic pagefileset create name="D:\pagefile.sys"
wmic pagefileset where name="D:\\pagefile.sys" set InitialSize=20480,MaximumSize=25480
echo "Pagefile created.
Need to add script to reboot the windows & after reboot continue with next step
DISKPART /s C:\Users\Desktop\param_files\instructions.txt
echo "Drive mounted successfully"
Regards
You could mark where you want to restart your script like:
#echo off
REM Initialization here
if "%~1" neq "" goto :%~1
REM Do some stuff1 here
call :markReboot stuff2
REM Making sure to not execute some part of stuff2 before rebooting
goto :eof
:stuff2
REM Do some stuff2 here
call :markReboot stuff3
goto :eof
REM ...
:stuffn
REM Do some stuffn here
goto :eof
:markReboot
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce /t REG_SZ /d "\"%~dpf0\" %~1" /v RestartMyScript /f
shutdown /r /t 0
NOTE: The /f is not really needed in the reg add command.
EDIT: Adapting my answer to your specific should look like:
#echo off
if "%~1" neq "" goto :%~1
wmic pagefileset create name="D:\pagefile.sys"
wmic pagefileset where name="D:\\pagefile.sys" set InitialSize=20480,MaximumSize=25480
echo "Pagefile created.
call :markReboot stuff2
goto :eof
:stuff2
DISKPART /s C:\Users\Desktop\param_files\instructions.txt
echo "Drive mounted successfully"
goto :eof
:markReboot
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce /t REG_SZ /d "\"%~dpf0\" %~1" /v RestartMyScript /f
shutdown /r /t 0

Determining if batch script has been started/executed from the command line (cmd) -or- To pause or not to pause?

I like to have a typical "usage:" line in my cmd.exe scripts — if a parameter is missing, user is given simple reminder of how the script is to be used.
The problem is that I can't safely predict whether potential user would use GUI or CLI. If somebody using GUI double-clicks this script in Explorer window, they won't have chance to read anything, unless I pause the window. If they use CLI, pause will bother them.
So I'm looking for a way to detect it.
#echo off
if _%1_==__ (
echo usage: %nx0: filename
rem now pause or not to pause?
)
Is there a nice solution on this?
You can check the value of %CMDCMDLINE% variable. It contains the command that was used to launch cmd.exe.
I prepared a test .bat file:
#Echo Off
echo %CMDCMDLINE%
pause
When run from inside of open cmd.exe window, the script prints "C:\Windows\system32\cmd.exe".
When run by double-clicking, it prints cmd /c ""C:\Users\mbu\Desktop\test.bat" "
So to check if your script was launched by double-clicking you need to check if %cmdcmdline% contains the path to your script. The final solution would look like this:
#echo off
set interactive=1
echo %cmdcmdline% | find /i "%~0" >nul
if not errorlevel 1 set interactive=0
rem now I can use %interactive% anywhere
if _%1_==__ (
echo usage: %~nx0 filename
if _%interactive%_==_0_ pause
)
Edit: implemented fixes for issues changes discussed in comments; edited example to demonstrate them
:: exit if not interactive
echo %CMDCMDLINE% | find /i "/c"
if not ERRORLEVEL 1 goto:eof
Here, I wrote something...
Usage.bat
#echo off
if arg%1==arg goto help
goto action
:action
echo do something...
goto end
:help
set help1=This is Help line 1.
set help2=This is Help line 2.
cmd.exe /k "echo %help1% &echo %help2%"
goto end
:end
It's not perfect, but it works! :D
-joedf
This is only using the internal command. so effectively....
EnableDelayedExpansion
if "!cmdcmdline!" neq "!cmdcmdline:%~f0=!" pause >nul
or
if not "!cmdcmdline!" == "!cmdcmdline:%~f0=!" pause >nul
DisableDelayedExpansion
if "%cmdcmdline%" neq "%cmdcmdline:%~f0=%" pause >nul
or
if not "%cmdcmdline%" == "%cmdcmdline:%~f0=%" pause >nul
Start your batch checking for %WINDIR% in %cmdcmdline% like this:
echo "%cmdcmdline%" | findstr /ic:"%windir%" >nul && (
echo Interactive run of: %0 is not allowed
pause
exit /B 1
)
Please use findstr
echo %cmdcmdline% | findstr /ic:"%~f0" >nul && ( pause >nul )
or
setlocal EnableDelayedExpansion
.
.
echo !cmdcmdline! | findstr /ic:"%~f0" >nul && ( pause >nul )
.
.
endlocal
This is always worked...
for internal command
setlocal EnableDelayedExpansion
set "cmddiff=!cmdcmdline:~0,1!" & if !cmddiff! neq ^" ( pause >nul )
endlocal
or
setlocal EnableDelayedExpansion
set "cmddiff=!cmdcmdline:~28,1!" & if !cmddiff! neq ^" ( pause >nul )
endlocal
You can compare the different thing, but this is only worked within EnableDelayedExpansion. and I don't think that this will be always worked, cause windows version, etc...
Similar approach...
setlocal
set startedFromExplorer=
echo %cmdcmdline% | find /i "cmd.exe /c """"%~0""" >nul
if not errorlevel 1 set startedFromExplorer=1
...
if defined startedFromExplorer pause
goto :EOF
setlocal EnableDelayedExpansion
if "!cmdcmdline!" neq "!cmdcmdline:%comspec%=!" ( pause>nul )
Test is done in Windows 10. Using %windir%, it is a little dangerous or ambiguous. So %comspec% is super safe.

Resources