I am creating a batch script to clear cache, check pc health and more. With questions and automation. My script works, but I would like to see a countdown in the questions. Without repeating the code if possible.
For example: “Do you want to delete prefetch files? (07) (Y/N/C for cancel): “
This is part of the script I have now:
<nul set /p=Do you want to delete prefetch files? (Y/N/C to Cancel):
choice /c YNC /n /t 7 /d Y
if errorlevel 3 (
echo.
echo %ESC%[32mAction canceled. Exiting...%ESC%[0m
echo Action canceled. >> "%LOGFILE%"
echo.
pause
goto :end
)
if errorlevel 2 goto skipPrefetch
echo %ESC%[32mDeleting Prefetch files...%ESC%[0m
echo Deleting Prefetch files... >> "%LOGFILE%"
for /r "%SystemRoot%Prefetch" %%f in (*) do (
echo Deleting "%%f" >> "%LOGFILE%"
del /f /q "%%f" > nul 2>&1
)
:skipPrefetch
<nul set /p=Do you want to SFC and DISM the PC? (Y/N/C to Cancel):
choice /c YNC /n /t 7 /d Y
if errorlevel 3 (
echo.
echo %ESC%[32mAction canceled. Exiting...%ESC%[0m
echo Action canceled. >> "%LOGFILE%"
echo.
pause
goto :end
)
if errorlevel 2 goto skipSfcDism
Etc….
This is the end part of my script:
<nul set /p=Do you want to restart your computer now? (Y/N):
choice /c YN /t 10 /n /d N
echo(
echo.
if errorlevel 2 (
echo %ESC%[32m Please restart your system manually to complete cleanup%ESC%[0m
echo Please restart your system manually to complete cleanup. >> "%LOGFILE%"
echo %ESC%[36m The script closes automatically in 14 sec.%ESC%[0m
Timeout /t 14
goto :eof
)
set /a counter=10
:countdown
<nul set /p=[2K[0G %ESC%[31mRestarting system in !counter! seconds... Press C to cancel.%ESC%[0m
choice /c CD /n /t 1 /d D >nul
if errorlevel 2 (
set /a counter-=1
if !counter! leq 0 goto restart
goto countdown
) else (
echo.
echo %ESC%[33m Restart canceled.%ESC%[0m
echo Restart canceled. >> "%LOGFILE%"
echo %ESC%[36m Press any key to close or wait 14 seconds.%ESC%[0m
echo.
timeout /t 14
goto :eof
)
:restart
echo Restarting system... >> "%LOGFILE%"
shutdown /r /t 0
I have experimented with this. But the input doesn’t work in my script:
set /a seconds=7
set "base=Do you want to delete prefetch files?"
for /L %%i in (%seconds%,-1,1) do (
<nul set /p="[0G!base! (%%i) (Y/N/C to Cancel): "
timeout /nobreak /t 1 >nul
)
echo.
<nul set /p="!base! (Y/N/C to Cancel): "
choice /c YNC /n >nul
if errorlevel 3 goto cancelAction
if errorlevel 2 goto skipPrefetch
goto doDeletePrefetch
I know the script is a bit much haha. I can simplify it, but I like it this way. It takes me back to my DOS menu-making and scripting days. (I’m just not that good at it)
Thank you!!