0

The idea is that a new device enters my network and a script running in the background can run a command to back it up, SyncToy for example (cd Program Files/SyncToy SyncToyCmd.exe -r).

I've run into scripts from similiar questions (How to check if ping responded or not in a batch file)

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
if not !state!==!oldstate! (
    echo.Link is !state!
    set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal

However I can't figure out how to run the command when the state changes to "up" as an exception in the loop, I also only want to run it once, adding a condition of once every 12 hours maybe.

lit
  • 14,456
  • 10
  • 65
  • 119
Abrelaboca
  • 27
  • 1
  • 4

1 Answers1

0

Is this as simple as placing the command inside the block that tests for receipt? I do not understand how SyncToy would know the IP address.

for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%a"=="xReceived" if "x%%b"=="x1," (
        set state=up
        PUSHD "%ProgramFiles%/SyncToy"
        SyncToyCmd.exe -r
        POPD
    )
)
lit
  • 14,456
  • 10
  • 65
  • 119
  • That works. The idea is not that the IP adress is a variable since the device connecting has a static one, therefore SyncToy doesn't need to know any IP adress since the folder pairs would already be configured and the command runs them all. However the command runs every time the link is "up" and therefore runs it forever, any fix for this? – Abrelaboca Jun 19 '17 at 10:46
  • What causes this script to run? – lit Jun 19 '17 at 10:50
  • Never mind just added a ping after the SyncToy command for 12 hours, thank you! – Abrelaboca Jun 19 '17 at 11:03
  • So, you are going to have `ping` run for 12 hours? Can you accept this response as the answer? – lit Jun 19 '17 at 11:05
  • When pinging a client and checking if "x%%a"=="xReceived" if "x%%b"=="x1," looking at the output of the ping command, the state changes to "up", after which it runs the rest of that block. – Abrelaboca Jun 19 '17 at 11:06
  • Yes, thank you very much. The ping command is apparently less consuming then SLEEP or TIMEOUT. – Abrelaboca Jun 19 '17 at 11:07