im trying to sync my 7 Servers (1 to 6) with robocopy but i'm already failing at creating the for-loop. My second Problem is that I don't know where to place the robocopy part so it is only executed if the the server is up. it seems that the script is always run through so both if statements are executed.
@echo off
title Sync
cls
::
:: Logging
::
::Log-Variablen werden gesetzt
set LOGDIR=.\Logs
set LOGFILE=%LOGDIR%\SR_sync.log
::Check ob LOGDIR existiert.
if not exist "%LOGDIR%" mkdir %LOGDIR%
::Backup alter Logs
if exist %LOGFILE%.3 del %LOGFILE%.3
if exist %LOGFILE%.2 move %LOGFILE%.2 %LOGFILE%.3
if exist %LOGFILE%.1 move %LOGFILE%.1 %LOGFILE%.2
if exist %LOGFILE% move %LOGFILE% %LOGFILE%.1
::
:: Synchronisierung
::
::Quellserver wird gesetzt
set sourceserver=SERVER
set targets=targets.txt
::Zielserver werden als Array gesetzt
::Log wird erstellt
echo Sync von %date% %time% >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%
echo. >> %LOGFILE%
echo SOURCE: %sourceserver% >> %LOGFILE%
echo TARGETS: %targets% >> %LOGFILE%
echo. >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%
for /f %%i in (%targets%) do (
echo %time% - Sync mit %%i gestartet >> %LOGFILE%
ping %%i -n 1 -w 1000 - l 2000 -4 | findstr /r /c:"[0-9] *ms"
set hostup=%errorlevel%
if %hostup% == 0 ( echo %time% - %%i erreichbar. >> %LOGFILE% )
if not %hostup% == 0 ( echo %time% - %%i nicht erreichbar. >> %LOGFILE% )
)
This Fails with a unknown "("
rem @echo off
cd %windir%
title Sync
cls
::
:: Logging
::
::Log-Variablen werden gesetzt
set LOGDIR=.\Logs
set LOGFILE=%LOGDIR%\SR_sync.log
::Check ob LOGDIR existiert.
if not exist "%LOGDIR%" mkdir %LOGDIR%
::Backup alter Logs
if exist %LOGFILE%.3 del %LOGFILE%.3
if exist %LOGFILE%.2 move %LOGFILE%.2 %LOGFILE%.3
if exist %LOGFILE%.1 move %LOGFILE%.1 %LOGFILE%.2
if exist %LOGFILE% move %LOGFILE% %LOGFILE%.1
::
:: Synchronisierung
::
::Quellserver wird gesetzt
set sourceserver=SERVER
set targets=targets.txt
::Zielserver werden als Array gesetzt
::Log wird erstellt
echo Sync von %date% %time% >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%
echo. >> %LOGFILE%
echo SOURCE: %sourceserver% >> %LOGFILE%
echo TARGETS: %targets% >> %LOGFILE%
echo. >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%
for /f %%i in (%targets%) do (
echo %time% - Sync mit %%i gestartet >> %LOGFILE%
ping %%i -n 1 -w 1000 - l 2000 -4 | findstr /r /c:"[0-9] *ms"
if %errorlevel% == 0 (
echo %time% - %%i erreichbar. >> %LOGFILE% && exit)
echo %time% - Sync abgebrochen. %%i nicht erreichbar. >> %LOGFILE%
)
This works.
any suggetions? Many thanks in advance.