Doing a windows files count down of installing windows updates - batch-file

This is the code I am working on with right now. The code run the *.msu file from a dir onto the system for update. Looking to add a number count down the files from 9 to 1 so can tell The code is using a bat file. where I am all most done with the install.
There is the code
#echo off
setlocal EnableDelayedExpansion
'This part code found the number of msu files
for /f %%A in ('dir *.msu') do (
set files=!dirs!
set dirs=%%A
)
'This part of code put the file msu file name
for /f %%A in ('dir /b *.msu') do (
echo == Installing Updates == "%files%" "%%A" ...
%%A /quiet /norestart
)
echo.
echo ########################################
echo.
echo == Updates installed ==
echo.
echo == Press any key to restart ==&pause>nul
echo.
shutdown.exe /r /t 0
I would the output to look like this if can
== Installing Updates == "9" "file" ...
== Installing Updates == "8" "File" ...
I just need help a way of every time install update it make the number go down by one. Any help would be good. Thank for all your help.

Krii's answer should work fine. However there are easier ways. Such as the timeout command. The following will wait for ten seconds, and display a timer:
timeout 10
Or if you didn't want any output you could add > nul at the end of the command.
Another possibility is to exploit the choice command:
choice /c a /n /d a /t 10 > nul
That example also won't have any output. Of course the user can press the a key to cancel the countdown and continue the script, but I don't see that being an issue.

There is probably a better way to do this, but is this what you want?
#echo off
for /f %%A in ('dir /b *.msu') do (
echo == Installing Updates == "%%A" ...
%%A /quiet /norestart
)
for /l %%A in (9,-1,1) do (
cls
echo %%A
ping 1.1.1.1 -n 1 -w 1 >nul
)
cls
echo.
echo ########################################
echo.
echo == Updates installed ==
echo.
echo == Press any key to restart ==&pause>nul
echo.
shutdown.exe /r /t 0

Related

Keep 2 specific folders using RMDIR

As part of my job I need to remove files from computers that are being shipped over seas, this task is done several times a week and can be scripted, however I am terrible with script, I am in no means a coder of any sort.
My issue is that I need to clear out the C:\Users folder whilst keeping the Public and Default folders. I know how to remove a directory and all subfolders however I don't know how to keep just those two folders.
The code I have so far is:
#Echo off
color F
:Choice
Echo.
Echo.
set choice=
set /p choice="Type Y to proceed or N to close the tool and then press ENTER:----"
IF '%Choice%' =='Y' GOTO DELETE
IF '%Choice%' =='y' GOTO DELETE
IF '%Choice%' =='N' GOTO END
IF '%Choice%' =='n' GOTO END
:DELETE
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
Echo.
Echo.
#Echo All Files Removed
GOTO END
:END
echo.
echo.
Echo *****...Closing programme... Please wait...*****
Timeout /t 3
exit
Do you know if this is possible?
The following method, ran As Administrator, may work for you:
#Echo Off
Color 0F
Echo=&Echo=
Choice /N /M "Type Y to proceed or N to close the tool"
If ErrorLevel 2 Exit /B
For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
"Special!='True'" Assoc /AssocClass:Win32_UserAccount 2^>Nul'
) Do WMIC UserAccount Where "SID='%%A' And LocalAccount='TRUE'" Delete
Echo=&Echo=&Echo All Files Removed&Echo=&Echo=
Echo *****...Closing programme... Please wait...*****
Timeout 3 /NoBreak>Nul
Exit /B
Special!='True' will ignore special accounts, which include Administrator, Public and Default.

Reg Query wiping computer when using remotely

Recently I got a script to query the reg set a value and then delete folders under the folder I puled from the query. Looks like this:
pause
FOR /F "TOKENS=2*" %%I IN ('REG QUERY "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\14.0\Outlook\Security" /V OutlookSecureTempFolder') Do SET "ValueData=%%J"
pause
echo Delete Outlook Temp. Files???
echo Enter to continue or Ctrl+C to cancel.
pause
del /q /f /s "%valuedata%\*.*"
del /q /f /s "%systemdrive%\Jacob'sTemp"
pause
echo --------------------------------------------------------------------------------
echo Complete! Goodbye!
echo --------------------------------------------------------------------------------
timeout /t 3
Works great when used locally. So I set up a robo copy to copy a folder with this script in it then use psexec to execute it remotely and it looks like this:
set /p cpu=
robocopy "\\nmcfs01\software\scripts\Jacob's Awesome Outlook Scripts" \\%cpu%\c$\Jacob'sTemp
pause
psexec \\%cpu% -u administrator "%systemdrive%\Jacob'sTemp\outlooktempdelete.bat"
pause
Now it works and it will run but here is the kicker when it goes back to the reg query batch to do the reg query it will run but it skips the 1st pause following the query and it always says it can not find the registry key but I can follow the path and it is there. The worst part is once I end the script there it wipes the computer of everything the user has access to. Not folders but all files/subfiles everywhere. Any insight is greatly appricated!
Here's a quick tidy up of your script:
#Echo Off
SetLocal EnableExtensions
(Set OV=14.0)
Choice /C YN /M "Delete Outlook Temp Files?"
If ErrorLevel 2 Exit/B
Set "BK=HKCU\SOFTWARE\Microsoft\Office\"
Set "EK=\Outlook\Security"
Set "VN=OutlookSecureTempFolder"
For /F "Tokens=2*" %%I In ('Reg Query "%BK%%OV%%EK%" /V %VN%') Do Set "VD=%%J"
PushD "%VD%" && (RD/S/Q "%VD%" 2>Nul) && PopD
REM The below commands will empty Jacob'sTemp:
If Exist "%SystemDrive%\Jacob'sTemp" (PushD "%SystemDrive%\Jacob'sTemp" && (
(RD/S/Q "%SystemDrive%\Jacob'sTemp" 2>Nul) && PopD
REM The below commands without the first two characters will remove Jacob'sTemp
::If Exist "%SystemDrive%\Jacob'sTemp" (RD/S/Q "%SystemDrive%\Jacob'sTemp"
Pause
Echo(------------------------------------------------------------------------------
Echo( Complete! Goodbye!
Echo(------------------------------------------------------------------------------
Timeout 5 >Nul
Give that a try exactly as it is just to see if it makes any difference.
I left the lonely but editable 'set' at the top because this is the only bit that need's changing to cater for earlier or later editions of outlook.

Batch file issue not solved

I've tried to create an invisible countdown.
For example, the script starts the countdown, but in the meantime the script runs like an MMORPG. When you are entering an instance dungeon, it starts the countdown and you can walk etc. But when I do it with PING -n 6 127.0.0.1 //Delay the script for 5 seconds. I want the PING to run in the background and meanwhile the script goes on. I've got a response from Stephan but his code didn't work :(
Stephan's Code:
#echo off
setlocal enabledelayedexpansion
REM create a CarriageReturn:
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
for /L %%i in (5,-1,0) do (
<nul set /p =King Albireo is spawning in %%i seconds...!CR!
ping -n 2 127.0.01 >nul
)
goto Main
:Main
#title Example: Countdown
cls
echo It works
pause
Can someone make this script so it is running in the background and meanwhile the script goes on.
I know I used the same words over and over just to make sure everyone is getting what I am trying to say.
BTW thanks Stephan for the code.
You can try with
start "" /b cmd /q /v /e /c"(for /l %%a in (5 -1 1) do (echo respawn in %%a seconds&>nul ping -n 2 ""))&echo RESPAWN"

Batch file over network path

I have the following batch script to make life easier at work.
Here is what it is supposed to work:
1- Drag and drop some file onto the .bat
2- Choose file's destination on the "menu"
3- Script copy's files to destination folder
4- Script executes remote procedure (that's the PSexec line)
5- Script copy's the result of the remote procedure to other folders.
And this works fine... except for a "small" detail with which i need some help.
When i try to copy the network location \10.250.39.116\d%... if i haven't previously logged into that machine it wont work.
I've been looking into the 'net use' command to overcome this, but i'm not sure if it suits my needs.
There are a total of 4 different machines i need to authenticate, dependent on the choice of the menu.
Actual Question:
Can i log in to such machines with the batch, and avoid creating duplicate connections every time i run the script ? If so, how?
Thank you for your time!
:)
I know the paths I have all point to the same place :)
#echo off
setlocal enabledelayedexpansion
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Compilar em Qualidade
ECHO 2 - Compilar na HSDEV
ECHO 3 - Compilar nas DEMOS
ECHO 4 - EXIT
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO :QUAL
IF %M%==2 GOTO :HSDEV
IF %M%==3 GOTO :DEMO
IF %M%==4 GOTO EOF
:QUAL
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:HSDEV
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:DEMO
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:PROCESSA
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~nx"
set "pathVec[!argCount!]=%%~dpx"
)
rem echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do (
echo Vou compilar %%i - "!argVec[%%i]!"
if exist %PathForms6%!argVec[%%i]!.* del /q %PathForms6%!argVec[%%i]!.*
if exist %PathForms10%!argVec[%%i]!.* del /q %PathForms10%!argVec[%%i]!.*
robocopy "!pathVec[%%i]!." %PathForms6% !argVec[%%i]!.fmb > nul
)
c:
cd c:\pstools
psexec %PathCompilador%
for /L %%i in (1,1,%argCount%) do (
if exist "%PathForms10%!argVec[%%i]!.fmx" (
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPriv% /y
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPub% /y)
)
pause
How much testing have you done with net use? Try running it twice at the command line. Notice how the output changes at the second running:
As you can see, where a connection has already been established, net use will output a summary of the connection rather than creating a duplicate connection.
If you prefer, you could use conditional execution or errorlevel checking. Using this method, you can avoid calling net use until xcopy fails, which should only be the first time. Here's a short example, simply to illustrate the mechanics:
#echo off
setlocal
ping -n 1 10.250.39.116 | find /i "TTL=" >NUL || (
echo 10.250.39.116 is offline. Unable to continue. Press any key to exit.
pause >NUL
goto :EOF
)
call :xcopy "%~1" "destination"
echo Press any key to exit.
pause >NUL
net use \\10.250.39.116\d$ /delete >NUL 2>NUL
goto :EOF
:xcopy <source> <dest_dir>
xcopy /L "%~1" "%~2" 2>NUL || (
net use \\10.250.39.116\d$ /user:username password >NUL 2>NUL
xcopy /L "%~1" "%~2"
)
goto :EOF

Check if file size is changing with Windows batch

I have a little problem I'm trying to sort out. I'm recording sound 24/7 and they are split in .wav files that have 2 hour duration. Since .wav files are pretty large I would like to encode them to .mp3. Everything works fine with this script
#echo off
cd path/to/folder
for %%i in (*.wav) do (
lame.exe %%i
del %%i
)
Everything works as it should but there is one problem, I want to check if file is still recording, if that's true then I don't want to encode it. I tried to check file size now and 10 sec later but it gets same value. Here is example
#echo off
cd path/to/folder
for %%i in (*.wav) do (
echo %%~zi
ping -n 10 127.0.0.1 >nul
echo %%~zi
)
output is for example:
444444
and after 10 sec same
444444.
Can someone please tell me how to check file size if it's constantly changing?
Test this on your files.
It will get a filename (sorted alphabetically in NTFS) and test to see if it is locked, and loop if it is - testing every 60 seconds.
When the file is free it will launch lame and then check the next file.
#echo off
cd /d "path/to/folder"
for %%a in (*.wav) do call :next "%%a"
echo done
pause
goto :eof
:next
set "filename=%~1"
:loop
2>nul (call;>>"%filename%") && (
echo "%filename%" is free! & goto :lame
) || (
echo "%filename%" is in use or is read only!
)
timeout /t 60 /nobreak
goto :loop
:lame
lame.exe "%~1"
del "%~1"

Resources