r/BatchScripts • • Dec 18 '24

My script to PING a remotely restarted system and alert you when it's back online

4 Upvotes

The idea here was to not have a non-stop infinite PING request bogging down network traffic and have it notify you when a system is online again after a remote restart.

@echo off

REM set minimum and maximum bounds here to match DHCP range, or optionally cover it all by doing 1-254

setlocal enabledelayedexpansion
REM default values set here:
set tgt=LocalHost
set wentOffline=0
set intervalSecs=10
set maximumChecks=30
:ShowMenu
cls
set selection=
echo Remote Restart Monitor Utility v1.2.2
echo =====================================
echo/
echo Target: %tgt%
echo Interval: %intervalSecs%
echo Maximum Tries: %maximumChecks%
echo/
echo/
echo MENU
echo ====
echo About = About this bat file
echo Interval = Set interval for checking (in seconds), default is 10
echo Max = Set maximum number of iterations, default is 30
echo Start = Start monitoring
echo Target = Set monitoring target
echo X = Exit
echo/
set /p selection= Enter option:
set selection=%selection:~0,1%
echo/
if /I "%selection%"=="A" (
cls

echo About Remote Restart Monitor Utility

echo Original Release 2018-FEB-02

echo/

echo Author Gary Ritter Jr

echo [email protected]

echo/

echo Intended to alert the user when a remotely restarted system comes back online without spamming constant ping requests.

echo Will attempt a ping every interval seconds and automatically stop when the system responds to a ping after having been unavailable.

echo Start it when you're ready to restart, if the system is already responding when it starts, it will wait to see it go offline and come back again.

pause
)
if /I "%selection%"=="M" (
set /p maximumChecks= "Maximum Iterations:"
)
if /I "%selection%"=="I" (
set /p intervalSecs= "Interval delay (in seconds):"
)
if /I "%selection%"=="S" (
set /p ready= "Really Start (Y/N)?"

if /I "!ready:\~0,1!"=="Y" goto confirmedSend
)
if /I "%selection%"=="T" (
set /p tgt= "New Target:"
)
if /I "%selection%"=="X" (
goto AllDone
)
goto ShowMenu
:confirmedSend
echo/

echo/

echo %time% It has begun...

echo/

for /L %%n in (1,1,%maximumChecks%) DO (

ping -n 1 %tgt% | find "TTL=" >nul

if errorlevel 1 (

if !wentOffline!==0 (
echo Target %tgt% is offline
)

set wentOffline=1

) else (

if !wentOffline!==1 (
echo Target %tgt% is back online
exit
)

)

timeout /t %intervalSecs% >nul

)

if !wentOffline!==0 (

echo Target did not go offline. Timed out.

) else (

echo Target is still offline at %time%. Timed out.

set wentOffline=0

)

goto done
:done
echo/

pause

goto ShowMenu
:AllDone
REMecho Press any key to exit...

REM pause >nul
endlocal

r/BatchScripts • • Dec 18 '24

A batch file to binary compare files in two directories and do something based on options

3 Upvotes

Hi all. I'll describe what I'm hoping to accomplish and with any luck someone can assist in writing something. I've only got fairly basic BAT file skills. This is for verifying integrity/accuracy of files being backed up to a NAS before clearing them off the laptop. The part I don't really know how to tackle is the comparison. If you can give me guidance on that part at least, I can probably figure out wrapping it in an appropriate function.

I want to specify a SOURCE directory "D:\local-copy\" and DESTINATION directory "R:\NAS-copy\". For any files (including recursively into sub-folders) that exist in both directories, I want to compare the contents of the files to ensure they match. Log or echo matching filenames found and whether the contents were a match or not.

If a file is in one directory but no corresponding file named in the other, perform no action on it.

I want to have two different option flags I can set to perform either or both of two different actions:

  1. /DelSrc If two files were compared and found to be a MATCH, delete the one in the SOURCE directory and log/echo that action. By default, log only unless the option is set.

  2. /DelDest If two files were compared and did NOT match, delete the one in the DESTINATION directory and log/echo that action. (It's an invalid backup). By default, log only unless the option is set.


r/BatchScripts • • Aug 14 '24

How?

3 Upvotes

How do I start a new page?


r/BatchScripts • • Feb 07 '23

This way works but that way doesn't

3 Upvotes

Hello,

I'm trying to move all the files of a certian type to another computer on the network, It works one way but complains saying "the filename directory name or volume syntax is incorrect"

This script works

move \\192.168.0.4\Mega.dir\*.lst C:\Transfer\ID\Return

This script doesn't work

move C:\Transfer\ID\*.lst \\192.168.0.4\Mega.dir\Transfer

What could be causing this issue?


r/BatchScripts • • Aug 12 '22

Windows Update via .bat

3 Upvotes

Hey all. I have a script here that I cobbled together to download and apply all Windows updates but as you can see, I have to run the code twice to get it to work, otherwise I get errors. Is there a better command for this?

@echo off
echo Installing Windows Updates (1/2)
powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force}"
echo Y | powershell -command "Install-Module PSWindowsUpdate -All"
echo Y | powershell -command "Install-WindowsUpdate"
echo Y | powershell -command "Install-WindowsUpdate -AcceptAll -AutoReboot"
echo Installing Windows Updates (2/2)
powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force}"
echo Y | powershell -command "Install-Module PSWindowsUpdate -All"
echo Y | powershell -command "Install-WindowsUpdate"
echo Y | powershell -command "Install-WindowsUpdate -AcceptAll -AutoReboot"
pause

r/BatchScripts • • Jun 10 '21

Error saying "| was unexpected at this time"

3 Upvotes

Good Morning,

I am trying to write a windows bat script that captures the output (pid only) of a process and passes it in a variable. So that i can pass the variable in to another command. Below is what i have written but i get the above error in the title.

@echo off
FOR /F %%i in (\tasklist /v /fi "imagename eq avgbrowser.exe" /fi "cputime gt 01:25:00" /FO LIST | find "PID:"`) DO (`
SET var=%%i
)
ECHO %var%

Can someone please help me figure out what i did wrong?


r/BatchScripts • • Jan 02 '21

How to run some commands based on condition.

3 Upvotes

Hi all , How can I run some lines based on some criteria. Example : I am looking for not to run some lines if no user is logged on.

Can someone help and I am poor in batch scripting.


r/BatchScripts • • Aug 24 '24

Is this a script? (probably doesn't work due to dynamic urls) & yes I know how to make a .bat file in notepad

2 Upvotes

cd %USERPROFILE%\Downloads

bitsadmin.exe /transfer "Downloading 7zip" https://www.7-zip.org/a/7z2408-x64.msi %USERPROFILE%\Downloads\7zip_installer.msi

msiexec /a "%USERPROFILE%\Downloads\7zip_installer.msi" /qb TARGETDIR=%USERPROFILE%\Downloads\7zip

bitsadmin.exe /transfer "Downloading inno unp" "https://downloads.sourceforge.net/project/innounp/innounp/innounp%200.50/innounp050.rar?ts=gAAAAABmyisfkjD-4XRvcqtLFUIdY5n0Nq7u3x0SBxcpGMX-zKrBizy4_Me5X1EMSXTj-QE6gu7454rEFCD3zjbFbgdDz3pEMw%3D%3D&r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Finnounp%2Ffiles%2Flatest%2Fdownload" %USERPROFILE%\Downloads\innounp.rar

%USERPROFILE%\Downloads\7zip\Files\7-Zip\7z.exe x %USERPROFILE%\Downloads\innounp.rar -oinnounp

@echo off set /p "installer=Enter Path to installer exe"

%USERPROFILE%\Downloads\innounp\innounp.exe -x -dNonInstall %installer%

explorer %USERPROFILE%\Downloads\NonInstall

exit


r/BatchScripts • • Jun 28 '24

How to Start dfrgui.exe, auto-optimise/defrag for each volume type?

2 Upvotes

I want a one click script to automate to run via task scheduler at a 90day custom interval.

Task scheduler lets me to it with defrag.exe in a dos window, but I want to automate the GUI so I can interrupt the task if I want to.

Also, the arguments work for defrag but not dfrgui.exe. The latter only starts the tool and does nothing.

Can this be done?


r/BatchScripts • • May 18 '24

Is there a ^ equivalents in CMD?

2 Upvotes

In a long batch script line, I would add a ^ character in numerous places to break up the line in to multiple lines for easier reading. It would execute as if it was on one line.

When I rename the .bat to .cmd it fails to run since ^ is treated differently.

Is there a way to break up a line of code in .cmd files?


r/BatchScripts • • Jun 21 '23

Any cool productivity or file organization Bats?

2 Upvotes

Looking for suggestions and ideas. Ive been able to compile a bat, that sends specific file types to specified folders all at once. But what else can be made? I only asked after chat gpt was able to write it for me.


r/BatchScripts • • Mar 01 '23

Can someone give me code for a batch file that runs sfc first then asks if I would like to check health kind of like:

2 Upvotes

(runs sfc /scannow when the batch file is opened and goes to the first question) Would you like to Check Health?

Yes (Runs Dism /Online /Cleanup-Image /CheckHealth) No (Says ‘Have a good day’ and closes after any key is pressed)

(Checks health and goes to the next question)

Would you like to Scan Health?

Yes (Runs Dism /Online /Cleanup-Image /ScanHealth) No (Says ‘Have a good day’ and closes after any key is pressed)

(Scans health and goes to the next question)

Would you like to Restore Health?

Yes (Runs Dism /Online /Cleanup-Image /RestoreHealth) No (Says ‘Have a good day’ and closes after any key is pressed)

(Restores health, Says ‘Have a good day’, and closes after any key is pressed)


r/BatchScripts • • Jan 28 '22

Get CD/DVD Drive Information with error checking?

2 Upvotes

I have the following script that simply gets a few details about the current optical drive. However, how would I add error checking for when the script is executed on a computer without an optical drive (such as a laptop) so that it could show a definable error such as: No optical drive detected?

for /f "skip=2 tokens=2,3 delims=," %%i in (
  '%SystemRoot%\System32\wbem\wmic.exe logicaldisk where "drivetype=5" get Caption^,Description /format:csv'
) Do (
    set "optical_drive_letter=%%i"
    set "optical_drive_description=%%j"
)

for /f "tokens=2,3 delims=," %%a in (
  '%SystemRoot%\System32\wbem\wmic.exe cdrom where "mediatype!=11" get name^,mediatype /format:csv'
) Do (
  set "optical_drive_type=%%a"
  set "optical_drive_name=%%b"
)

echo %optical_drive_letter%\ %optical_drive_description%/%optical_drive_type% (%optical_drive_name%)

r/BatchScripts • • Dec 21 '21

Playing Some Radios Stations with a Dynamic Menu

Thumbnail self.batch
2 Upvotes

r/BatchScripts • • Jun 01 '21

Batch file that checks for all files, then lists them without extension

2 Upvotes

Any way to do that? Thanks!


r/BatchScripts • • Apr 24 '21

.bat discord file code

2 Upvotes

when i send a .bat file to my friend it shows the code on discord anyway to hid it?


r/BatchScripts • • Apr 19 '21

Downloading a file straight to current users download path w/o wget or any non default programs

2 Upvotes

Setting up a simple script to make something clear a folder but im trying to make it double click and go with the script, just wanting a default win10 installattion without any new programs to just be able to double click and run, installing 1 new file (Zip file)


r/BatchScripts • • Aug 30 '20

Adobe Flash Bulk silent removal

2 Upvotes

Hi With Flash going away, I’m looking for a script that will silently find any version of Flash and uninstall it silently. This will be run over several different networks thru the Datto RMM Thanks in advance


r/BatchScripts • • Apr 28 '15

[Request] Script to click a button on a webpage periodically

2 Upvotes

I need a sort of script or something to trigger an event (check now) on a specific URL (ifttt) periodically. I have little to no knowledge in programming but I have tried refresh clients to load the url of the button, and running the url from cmd line. The event is only ever triggered when it is clicked, and the web page will update.

Any thoughts?


r/BatchScripts • • Mar 04 '15

[WIP] OS agnostic dep management. [Mac/Debian for now]

2 Upvotes

This is a script I've been working on for a little bit. The idea is that a project would provide a file in it's root called depends.sh which would include everything that project relys on. This originated from my dotfiles repository have having many deps, like vim, zsh, janus the list goes. Wanted to share and see what you guys think?

https://github.com/andrewhood125/establish


r/BatchScripts • • Jul 16 '14

[Request]Batch Script to Rename a JPG Based on Folder Name, and Then Copy to a New Folder.

2 Upvotes

At work we have an entire drive full of PowerPoint templates. I want to create an image gallery to basically serve as a directory, and I am hoping that a batch script will help. I have Googled, and found similar ideas, but nothing quite what I am looking for, and nothing I have been able to make work.

Here are the deets: I have a large folder called Templates. There are dozens, if not hundreds, of subfolders named ppt12543, or similar. Inside each subfolder is a file called master.jpg, among other files.

I want to rename master.jpg to the name of the subfolder it is in (eg: ppt12543.jpg), and move it to a new folder in the Templates folder called Directory. The renaming can happen at any point in the process, it doesn't necessarily need to be first, but it just needs to be the name of the folder it was moved from.

I am working with 2 versions of the folder, one to preserve as is, and the other to delete everything but the images once I have taken them, and also in case anything goes wrong. I am working with Windows 7.

If anyone has any ideas I would be extremely grateful. Thanks in advance for your help.


r/BatchScripts • • Oct 31 '13

[Help BATCH] Checking who is logged to computer and returning their email address

2 Upvotes

I often need to check who is logged into certain machine and to get their email address for our ticketing system.

I use

set Host=%1
if "%HOST%"=="" (
set /P HOST=Enter host: %=%
)
powershell -command "WMIC /Node:%HOST% ComputerSystem Get UserName"
if "%1"=="" (
pause
)

to get the user logged to computer and this returns the user name in format "domain\prewin"

I know that I can convert prewin to email address with following:

dsquery user -samid prewin |dsget user -email

but this won't accept the output of the earlier script due the format.

Would you fellow redditors have any decent ideas of how to write a script which would check who is logged to computer in domain and then output both domain\prewin and the email address for user?


r/BatchScripts • • Oct 25 '13

[REQUEST] Batch Script to find a file and then replace it with a new version

2 Upvotes

I need to find a specific file on a system and then replace it with a new version from a network location.

For arguments sake . The file i need to find is call mountcs.txt it can exists in multiple locations on a machine. I need to search the system , find all instances then replace those instances with the new file. I am not that great at scripting and hitting a few snags. I was hoping someone could help me write a script to accomplish this.

thanks in advance to anyone who can assist.


r/BatchScripts • • Nov 07 '24

Using a txt file to store a file to file variable.

1 Upvotes

I was just curious on how I would be able to use txt file contents for a server name.

I create the txt file in one batch file:

hostname > name.txt

Then I want to use it in place of a PC/server name for net use command in a different batch file like this:

net use A: \\contentsOf"name.txt"

I just don't know how to do it. Please someone tell me it's possible🙏. & If possible how?


r/BatchScripts • • Oct 16 '24

New to batch scripts

1 Upvotes

Afternoon guys/gals can someone assist in trying to autmate moving folders from one destination to another. The only variable would be if folders not there make it. If it is there add to it. I’m at a loss I’ve set the source and destination folders used robocopy command but can’t get the jist of it 🫣