I have the following code. It works great except for a couple issues. First, my "if not blank" check doesn't work at all. Second, during each iteration, the last time it passes the servername as both the servername and folder, which I'm really not understanding...
code
SET servers=server01,server02
SET drive=c
SET root=program files (x86)\test\
SET backup=%root%\backup
SET folders=folder01\,folder02\file01.txt
FOR %%b in (%servers%) do (
FOR %%a in (%folders% %%b) DO (
if not %%a == "" call :backup %%b %%a
)
)
:backup
SET currentfile="\\%1\%drive%$\%2"
setlocal
echo Backing Up %currentfile%
REM mkdir "\\%1\%drive%$\%backup%"
REM xcopy /I
exit /b
endlocal
goto :end
output
1] Backing Up "\\server01\c$\folder01\"
2] Backing Up "\\server01\c$\folder02\file01.txt"
3] Backing Up "\\server01\c$\server01"
4] Backing Up "\\server02\c$\folder01\"
5] Backing Up "\\server02\c$\folder02\file01.txt"
6] Backing Up "\\server02\c$\server02"
7] Backing Up "\\\c$\"
As you can see, lines 3, 6 and 7 are not desired.
Lines 3 and 6 are caused because of the %%b in the inner for loop. Line 7 is caused because the script does not terminate after the loops, so it runs the :backup code once more at the end.
The following seems to give the desired result:
@echo off
SET servers=server01,server02
SET drive=c
SET root=program files (x86)\test\
SET backup=%root%\backup
SET folders=folder01\,folder02\file01.txt
FOR %%b in (%servers%) do (
FOR %%a in (%folders%) DO (
rem echo a is %%a
if not %%a == "" call :backup %%b %%a
)
)
exit /b
:backup
SET currentfile="\\%1\%drive%$\%2"
setlocal
echo Backing Up %currentfile%
REM mkdir "\\%1\%drive%$\%backup%"
REM xcopy /I
endlocal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With