-1

This is my first attempt at a serious batch file. I am making this for my work and without going into too much detail, as I know there are better ways to do the process I want, I want this batch file to ask the user for permission to check for online updates, install them, then restart automatically. If the user does not approve, I want the file to ask close and ask again after 2 hours.

The Code:

@echo off

title Weekly Windows Updater

echo ###################################################################################
echo #                                                                                 #
echo #    In order to prevent common problems such as connection or display errors,    #
echo #    REDACTED is asking that updates be performed weekly!                         #
echo #                                                                                 #
echo #    Don't worry! This process is mostly hands off for our teachers and staff!    #
echo #    Just give this bot approval by typing y for yes, or n for no.                #
echo #                                                                                 #
echo #          NOTE: THIS PROCESS MAY TAKE 5-20 MINUTES TO COMPLETE!                  #
echo #          ANY DOCUMENTS OPEN WILL NEED TO BE SAVED PRIOR                         #
echo #          TO APPROVING THIS BOT TO RUN UPDATES!                                  #
echo ###################################################################################

REM Will ask the user if updates can run.

:choice
set /P c=May we run Windows Updates on your laptop?[Y/N]?
if /I "%c%" EQU "Y" goto :Approved
if /I "%c%" EQU "N" goto :Not_Approved
goto :choice

:Approved

call wuaclt.exe


:Not_Approved

REM close program and run again after x hours

pause

I have run through several online sources on ways to open the command prompt or powershell, as well as ways to have Windows Update run by going into System32 and launching the .exe file. I have tried functions like EXECUTE, call, and START. My most common error with this project has been "'NEEDS' is not recognized as an internal or external command, operable program or batch file." Yet all examples of ways to fix it have not worked for me.

As I said, I am quite new to building a file like this and am not sure what I am missing.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • `NEEDS` is not in your shown code, so the failure is in a part of your code which you didn't show. – Stephan Feb 03 '23 at 16:27
  • The command __CALL__ is for calling another batch file from within a batch file or a series of commands below a label in an own context like a subroutine. That is explained by the usage help of command __CALL__ output on running `call /?` in a command prompt window. The command __CALL__ should not be used for running an executable like `wuaclt.exe` which cannot be found by `cmd.exe` as described by my answer on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi Feb 03 '23 at 17:47
  • I recommend to read also [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) – Mofi Feb 03 '23 at 17:48
  • @Stephan The code you see is all the code I have. I am not leaving anything out haha – Danielle Eaton Feb 06 '23 at 15:43
  • 1
    Your code isn't able to generate this message. Run with `echo on` and watch where the error occurs (the faulty line should show below the error message) – Stephan Feb 06 '23 at 16:33

1 Answers1

0
@echo off

title Weekly Windows Updater

echo ###################################################################################
echo # #
echo # In order to prevent common problems such as connection or display errors, #
echo # REDACTED is asking that updates be performed weekly! #
echo # #
echo # Don't worry! This process is mostly hands off for our teachers and staff! #
echo # Just give this bot approval by typing y for yes, or n for no. #
echo # #
echo # NOTE: THIS PROCESS MAY TAKE 5-20 MINUTES TO COMPLETE! #
echo # ANY DOCUMENTS OPEN WILL NEED TO BE SAVED PRIOR #
echo # TO APPROVING THIS BOT TO RUN UPDATES! #
echo ###################################################################################

REM Will ask the user if updates can run.

:choice
set /P c=May we run Windows Updates on your laptop?[Y/N]?
if /I "%c%" == "Y" goto :Approved
if /I "%c%" == "N" goto :Not_Approved
goto :choice

:Approved

wusa /detectnow

echo Windows updates are running in the background...
echo Please do not turn off your computer.

:Not_Approved

REM close program and run again after 2 hours

timeout /t 7200

goto :choice

This code uses the same logic as the previous one.... but instead of using the wusa.exe command.... we will try the wusa command. The "timeout" command now is to simply wait for the specified amount of time without redirecting the output.

About the SER /P that Compo says... he can optimize it instead of pointing it put....

  • 2
    Please don't advise using `SET` with `/P` for requesting known input of single characters. There is a built-in `choice.exe` utility specifically for this purpose. – Compo Feb 03 '23 at 16:16
  • Sadly that still gives me the error "'NEEDS' is not recognized as an internal or external command, operable program or batch file." – Danielle Eaton Feb 03 '23 at 16:17
  • As Compo mentions, you can use the `choice` command like the following. `%__APPDIR__%choice.exe /M "May we run Windows Updates on your laptop?"` The next line would be `IF %ERRORLEVEL% EQU 2 goto Not_Approved` followed by `IF %ERRORLEVEL% EQU 1 goto Approved`. – Qwerty Feb 03 '23 at 17:59