0

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.

  • Why are you using `MOVE` instead of `RENAME | REN`? Also I'd suggest you use doublequotes as best practice; `CD /D "%SystemRoot%"`, `Set "LOGDIR=Logs"`, `Set "LOGFILE=%LOGDIR%\SR_sync.log"` & `If Not Exist "%LOGDIR%\" MD "%LOGDIR%"` – Compo Jan 12 '18 at 14:14
  • You need to invoke delayedexpansion [hundreds of SO articles about that - use the search feature] in order to display or use the run-time value of any string variable that's changed within a parenthesised series of instructions (aka "code block"). – Magoo Jan 12 '18 at 14:21
  • you need [delayed expansion](https://stackoverflow.com/a/30284028/2152082) with `!hostup!` and `!errorlevel!`. Also `!time!` would be a benefit... – Stephan Jan 12 '18 at 14:51
  • Sebastian , once you've fixed the delayed expansion issue, if you're still having problems with the `I don't know where to place the robocopy part so it is only executed if the the server is up`, [edit your question](https://stackoverflow.com/posts/48227492/edit) so that it can be considered for reopening. – Compo Jan 12 '18 at 16:30

0 Answers0