Jump to content

Making Robocopy Script need HELP!


Recommended Posts

This is what I want.

Read root directory structure and set SOURCEDIR1 - etc per directory

read a file named DESTDIR.TXT and set DESTDIR1 - etc variables

after that robocopy should process everything this way

robocopy sourcedir destinationdir /MIR

till every variable is processed. This is what I have till now...

but im stuck --> can''t seem to get the last robocopy part automated (also left in this script)

SET SOURCEROOT=D:\TEST
SET DESTROOT=D:\TEST2

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir %SOURCEROOT% /A:D /B') do (
set /a n+=1
set SOURCEDIR!n!=%%a
)

for /f "tokens=*" %%b in (DESTDIR.TXT) do (
set /a m+=1
set DESTDIR!m!=%%b
robocopy "%%a" "%%b" /MIR
)


SET
pause

Edited by jeremyotten
Link to comment
Share on other sites


I am not sure to understand the problem you are having. :unsure:

in first loop you assign to n SOURCEDIR variables some values.

in second loop you assign to m DESTDIR variables the values in a .txt.

You whould check how many n's and m's you assigned to make sure that they are balanced and then loop through them.

Something like this:

@echo off
setLocal EnableDelayedExpansion
SET SOURCEROOT=D:\TEST
SET DESTROOT=D:\TEST2

set /A n=0
set /A m=0

for /f "tokens=* delims= " %%A in ('dir %SOURCEROOT% /A:D /B') do (
set /a n+=1
set SOURCEDIR!n!=%%A
set SOURCEs=!SOURCEs! !m!
)

IF NOT EXIST DESTDIR.TXT ECHO Missing file& GOTO :EOF

for /f "tokens=*" %%A in (DESTDIR.TXT) do (
set /a m+=1
set DESTDIR!m!=%%A
set DESTs=!DESTs! !m!
)


IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :EOF

FOR %%A in (%DESTs%) DO (
ECHO robocopy "%SOURCEROOT%\!SOURCEDIR%%A!" "%DESTROOT%\!DESTDIR%%A!" /MIR
)

You can make the comparison both between the indexes m and n or through the DESTs/SOURCEs (actually you do not need the SOURCEs as the final loop is done with DESTs)

jaclaz

P.S.: personally I would create the DESTDIR.TXT file with PAIRs of values, and parse it through a delimiter, something like

D:\TEST\Dir1,D:\TEST2\Dir1copied

or

Dir1,Dir1Copied

Edited by jaclaz
Link to comment
Share on other sites

I am not sure to understand the problem you are having. :unsure:

in first loop you assign to n SOURCEDIR variables some values.

in second loop you assign to m DESTDIR variables the values in a .txt.

You whould check how many n's and m's you assigned to make sure that they are balanced and then loop through them.

Something like this:

@echo off
setLocal EnableDelayedExpansion
SET SOURCEROOT=D:\TEST
SET DESTROOT=D:\TEST2

set /A n=0
set /A m=0

for /f "tokens=* delims= " %%A in ('dir %SOURCEROOT% /A:D /B') do (
set /a n+=1
set SOURCEDIR!n!=%%A
set SOURCEs=!SOURCEs! !m!
)

IF NOT EXIST DESTDIR.TXT ECHO Missing file& GOTO :EOF

for /f "tokens=*" %%A in (DESTDIR.TXT) do (
set /a m+=1
set DESTDIR!m!=%%A
set DESTs=!DESTs! !m!
)


IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :EOF

FOR %%A in (%DESTs%) DO (
ECHO robocopy "%SOURCEROOT%\!SOURCEDIR%%A!" "%DESTROOT%\!DESTDIR%%A!" /MIR
)

You can make the comparison both between the indexes m and n or through the DESTs/SOURCEs (actually you do not need the SOURCEs as the final loop is done with DESTs)

jaclaz

P.S.: personally I would create the DESTDIR.TXT file with PAIRs of values, and parse it through a delimiter, something like

D:\TEST\Dir1,D:\TEST2\Dir1copied

or

Dir1,Dir1Copied

Tried it. It didn''t even launch.....

Link to comment
Share on other sites

SET SOURCEROOT=D:\TEST
SET DESTROOT=D:\TEST2

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir %SOURCEROOT% /A:D /B') do (
set /a n+=1
set SOURCEDIR!n!=%%a
)

for /f "tokens=*" %%b in (DESTDIR.TXT) do (
set /a m+=1
set DESTDIR!m!=%%b
robocopy "%%a" "%%b" /MIR
)


SET
pause

From your code I'm confused as to what you're intending to do, can you provide us with something more meaningful please
Link to comment
Share on other sites

This is what I want:

step1 process the dir command and every line should become a variable

step2 read the destinationdir text file and every line should become a variable

step2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIR

thats all.

source and destination dirs should always be the same amount...

Link to comment
Share on other sites

This is what I want:

step1 process the dir command and every line should become a variable

step2 read the destinationdir text file and every line should become a variable

step2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIR

thats all.

source and destination dirs should always be the same amount...

and that's exactly what the posted code (based on yours) does.

WAHT is the problem? :unsure:

jaclaz

Link to comment
Share on other sites

This is what I want:

step1 process the dir command and every line should become a variable

step2 read the destinationdir text file and every line should become a variable

step2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIR

thats all.

source and destination dirs should always be the same amount...

and that's exactly what the posted code (based on yours) does.

WAHT is the problem? :unsure:

jaclaz

tested it like this

@echo off
setLocal EnableDelayedExpansion
SET SOURCEROOT=\\JEREMY\SOURCE$
SET DESTROOT=\\JEREMY\DESTINATION$

set /A n=0
set /A m=0

for /f "tokens=* delims= " %%A in ('dir %SOURCEROOT% /A:D /B') do (
set /a n+=1
set SOURCEDIR!n!=%%A
set SOURCEs=!SOURCEs! !m!
)

IF NOT EXIST DESTDIR.TXT ECHO Missing DESTDIR.TXT file& GOTO :END

for /f "tokens=*" %%A in (DESTDIR.TXT) do (
set /a m+=1
set DESTDIR!m!=%%A
set DESTs=!DESTs! !m!
)


IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :EOF

FOR %%A in (%DESTs%) DO (
robocopy "%SOURCEROOT%\!SOURCEDIR%%A!" "%DESTROOT%\!DESTDIR%%A!" /MIR
)

:END
pause

and it works! ;-)

1 request. What to do to output it to a logfile?

Link to comment
Share on other sites

I created it myself with loging and with a static sourcedir.txt as well as destdir.txt ;-)

@echo off
CALL :ALL 1>ROBOCOPY_%DATE:~9,4%%DATE:~6,2%%DATE:~3,2%_%time:~1,1%u%time:~3,2%m.LOG 2>&1
EXIT

:ALL
setLocal EnableDelayedExpansion
SET SOURCEROOT=\\JEREMY\SOURCE$
SET DESTROOT=\\JEREMY\DESTINATION$

set /A n=0
set /A m=0

for /f "tokens=* delims= " %%A in (SOURCEDIR.TXT) do (
set /a n+=1
set SOURCEDIR!n!=%%A
set SOURCEs=!SOURCEs! !m!
)

IF NOT EXIST DESTDIR.TXT ECHO Missing DESTDIR.TXT file& GOTO :END

for /f "tokens=*" %%A in (DESTDIR.TXT) do (
set /a m+=1
set DESTDIR!m!=%%A
set DESTs=!DESTs! !m!
)


IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :END

FOR %%A in (%DESTs%) DO (
robocopy "%SOURCEROOT%\!SOURCEDIR%%A!" "%DESTROOT%\!DESTDIR%%A!" /MIR
)

:END
pause

Link to comment
Share on other sites

and it works! ;-)

Sure it does.

And as said this line:

set SOURCEs=!SOURCEs! !m!

Can be removed.

by the way it is wrong :blushing: as it should read:

set SOURCEs=!SOURCEs! !n!

to work, IF the variable SOURCEs is to be used.

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

This is what I want:

step1 process the dir command and every line should become a variable

step2 read the destinationdir text file and every line should become a variable

step2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIR

thats all.

source and destination dirs should always be the same amount...

I know you've already got a response you're happy with, but I'd just like to make a suggestion.

I think the approach is slightly `heavy`! What I mean is that I think that setting a bunch of variables for each line of both files is intensive and probably not necessary based on your information.

I would suggest something a little more like this

@Echo off&Setlocal
Set "_=0"
For /f "delims=" %%# In (SOURCEDIR.TXT) Do (
Set/a "_+=1"&Call :N_ %%_%% "%%#")
Set "_="&Goto :Eof
:N_
Set "$=0"
For /f "delims=" %%# In (DESTDIR.TXT) Do (
Set/a "$+=1"&Call _ %%$%% "%%#" %1 %2)
Set "$="&Goto :Eof
_
If %3 Equ %1 Robocopy "\\JEREMY\SOURCE$\%~4" "\\JEREMY\DESTINATION$\%~2" /mir

As you'll note I removed all that logging rubbish, If I wished to log anything it would be the actual robocopy command and for that I'd use it's /LOG switch.

As a final note, if you did intend using a log file and you also wished to use the date and time in such a manner, I'd advise you to set both date and time to new variables once then use those variables for your expansion etc. Otherwise if the date/time changed during the formation of that file name you'd have all sorts of chaos.

Link to comment
Share on other sites

This is what I want:

step1 process the dir command and every line should become a variable

step2 read the destinationdir text file and every line should become a variable

step2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIR

thats all.

source and destination dirs should always be the same amount...

I know you've already got a response you're happy with, but I'd just like to make a suggestion.

I think the approach is slightly `heavy`! What I mean is that I think that setting a bunch of variables for each line of both files is intensive and probably not necessary based on your information.

I would suggest something a little more like this

@Echo off&Setlocal
Set "_=0"
For /f "delims=" %%# In (SOURCEDIR.TXT) Do (
Set/a "_+=1"&Call :N_ %%_%% "%%#")
Set "_="&Goto :Eof
:N_
Set "$=0"
For /f "delims=" %%# In (DESTDIR.TXT) Do (
Set/a "$+=1"&Call _ %%$%% "%%#" %1 %2)
Set "$="&Goto :Eof
_
If %3 Equ %1 Robocopy "\\JEREMY\SOURCE$\%~4" "\\JEREMY\DESTINATION$\%~2" /mir

As you'll note I removed all that logging rubbish, If I wished to log anything it would be the actual robocopy command and for that I'd use it's /LOG switch.

As a final note, if you did intend using a log file and you also wished to use the date and time in such a manner, I'd advise you to set both date and time to new variables once then use those variables for your expansion etc. Otherwise if the date/time changed during the formation of that file name you'd have all sorts of chaos.

WOW i don''t understand a thing of what you are doing with that script but it does work indeed!! You are SCRIPTING GOD!!

Link to comment
Share on other sites

WOW i don''t understand a thing of what you are doing with that script but it does work indeed!! You are SCRIPTING GOD!!

Indeed, Yzöwl is The Master :thumbup

Here is his batch "translated in an easier to read form, FYI:

@Echo off
Setlocal
Set "Counter=0"
For /f "delims=" %%A In (SOURCEDIR.TXT) Do (
Set /a "Counter+=1"
Call :SubRoutine1 %%Counter%% "%%A"
)
Set "Counter="
Goto :Eof

:SubRoutine1
REM Parameter 1 is "Counter"
REM Parameter 2 is an entry of SOURCEDIR.TXT
Set "AnotherCounter=0"
For /f "delims=" %%B In (DESTDIR.TXT) Do (
Set/a "AnotherCounter+=1"
Call :SubRoutine2 %%AnotherCounter%% "%%B" %1 %2
)
Set "$="
Goto :Eof

:SubRoutine2
REM Parameter 1 is "AnotherCounter"
REM Paramater 2 is an entry of DESTDIR.TXT
REM Parameter 3 is "Counter"
REM Parameter 4 is an entry of SOURCEDIR.TXT
If %3 Equ %1 Robocopy "\\JEREMY\SOURCE$\%~4" "\\JEREMY\DESTINATION$\%~2" /mir

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...