jeremyotten Posted November 3, 2008 Posted November 3, 2008 (edited) This is what I want.Read root directory structure and set SOURCEDIR1 - etc per directoryread a file named DESTDIR.TXT and set DESTDIR1 - etc variables after that robocopy should process everything this wayrobocopy 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:\TESTSET DESTROOT=D:\TEST2@echo offsetLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir %SOURCEROOT% /A:D /B') do (set /a n+=1set SOURCEDIR!n!=%%a)for /f "tokens=*" %%b in (DESTDIR.TXT) do (set /a m+=1set DESTDIR!m!=%%brobocopy "%%a" "%%b" /MIR)SETpause Edited November 3, 2008 by jeremyotten
jaclaz Posted November 3, 2008 Posted November 3, 2008 (edited) I am not sure to understand the problem you are having. 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 offsetLocal EnableDelayedExpansionSET SOURCEROOT=D:\TESTSET DESTROOT=D:\TEST2set /A n=0set /A m=0for /f "tokens=* delims= " %%A in ('dir %SOURCEROOT% /A:D /B') do (set /a n+=1set SOURCEDIR!n!=%%Aset SOURCEs=!SOURCEs! !m!)IF NOT EXIST DESTDIR.TXT ECHO Missing file& GOTO :EOFfor /f "tokens=*" %%A in (DESTDIR.TXT) do (set /a m+=1set DESTDIR!m!=%%Aset DESTs=!DESTs! !m!)IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :EOFFOR %%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)jaclazP.S.: personally I would create the DESTDIR.TXT file with PAIRs of values, and parse it through a delimiter, something likeD:\TEST\Dir1,D:\TEST2\Dir1copiedorDir1,Dir1Copied Edited November 3, 2008 by jaclaz
jeremyotten Posted November 3, 2008 Author Posted November 3, 2008 I am not sure to understand the problem you are having. 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 offsetLocal EnableDelayedExpansionSET SOURCEROOT=D:\TESTSET DESTROOT=D:\TEST2set /A n=0set /A m=0for /f "tokens=* delims= " %%A in ('dir %SOURCEROOT% /A:D /B') do (set /a n+=1set SOURCEDIR!n!=%%Aset SOURCEs=!SOURCEs! !m!)IF NOT EXIST DESTDIR.TXT ECHO Missing file& GOTO :EOFfor /f "tokens=*" %%A in (DESTDIR.TXT) do (set /a m+=1set DESTDIR!m!=%%Aset DESTs=!DESTs! !m!)IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :EOFFOR %%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)jaclazP.S.: personally I would create the DESTDIR.TXT file with PAIRs of values, and parse it through a delimiter, something likeD:\TEST\Dir1,D:\TEST2\Dir1copiedorDir1,Dir1CopiedTried it. It didn''t even launch.....
jaclaz Posted November 3, 2008 Posted November 3, 2008 Tried it. It didn''t even launch.....What's this, a joke? What do you mean by "it didn't even launch"?jaclaz
Yzöwl Posted November 3, 2008 Posted November 3, 2008 SET SOURCEROOT=D:\TESTSET DESTROOT=D:\TEST2@echo offsetLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir %SOURCEROOT% /A:D /B') do (set /a n+=1set SOURCEDIR!n!=%%a)for /f "tokens=*" %%b in (DESTDIR.TXT) do (set /a m+=1set DESTDIR!m!=%%brobocopy "%%a" "%%b" /MIR)SETpauseFrom your code I'm confused as to what you're intending to do, can you provide us with something more meaningful please
jeremyotten Posted November 4, 2008 Author Posted November 4, 2008 This is what I want:step1 process the dir command and every line should become a variablestep2 read the destinationdir text file and every line should become a variablestep2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIRthats all.source and destination dirs should always be the same amount...
jaclaz Posted November 4, 2008 Posted November 4, 2008 This is what I want:step1 process the dir command and every line should become a variablestep2 read the destinationdir text file and every line should become a variablestep2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIRthats 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? jaclaz
jeremyotten Posted November 4, 2008 Author Posted November 4, 2008 This is what I want:step1 process the dir command and every line should become a variablestep2 read the destinationdir text file and every line should become a variablestep2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIRthats 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? jaclaztested it like this@echo offsetLocal EnableDelayedExpansionSET SOURCEROOT=\\JEREMY\SOURCE$SET DESTROOT=\\JEREMY\DESTINATION$set /A n=0set /A m=0for /f "tokens=* delims= " %%A in ('dir %SOURCEROOT% /A:D /B') do (set /a n+=1set SOURCEDIR!n!=%%Aset SOURCEs=!SOURCEs! !m!)IF NOT EXIST DESTDIR.TXT ECHO Missing DESTDIR.TXT file& GOTO :ENDfor /f "tokens=*" %%A in (DESTDIR.TXT) do (set /a m+=1set DESTDIR!m!=%%Aset DESTs=!DESTs! !m!)IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :EOFFOR %%A in (%DESTs%) DO (robocopy "%SOURCEROOT%\!SOURCEDIR%%A!" "%DESTROOT%\!DESTDIR%%A!" /MIR):ENDpauseand it works! ;-) 1 request. What to do to output it to a logfile?
jeremyotten Posted November 4, 2008 Author Posted November 4, 2008 I created it myself with loging and with a static sourcedir.txt as well as destdir.txt ;-)@echo offCALL :ALL 1>ROBOCOPY_%DATE:~9,4%%DATE:~6,2%%DATE:~3,2%_%time:~1,1%u%time:~3,2%m.LOG 2>&1EXIT:ALLsetLocal EnableDelayedExpansionSET SOURCEROOT=\\JEREMY\SOURCE$SET DESTROOT=\\JEREMY\DESTINATION$set /A n=0set /A m=0for /f "tokens=* delims= " %%A in (SOURCEDIR.TXT) do (set /a n+=1set SOURCEDIR!n!=%%Aset SOURCEs=!SOURCEs! !m!)IF NOT EXIST DESTDIR.TXT ECHO Missing DESTDIR.TXT file& GOTO :ENDfor /f "tokens=*" %%A in (DESTDIR.TXT) do (set /a m+=1set DESTDIR!m!=%%Aset DESTs=!DESTs! !m!)IF NOT %n%.==%m%. ECHO Unbalanced data &GOTO :ENDFOR %%A in (%DESTs%) DO (robocopy "%SOURCEROOT%\!SOURCEDIR%%A!" "%DESTROOT%\!DESTDIR%%A!" /MIR):ENDpause
jaclaz Posted November 4, 2008 Posted November 4, 2008 (edited) and it works! ;-)Sure it does.And as said this line:set SOURCEs=!SOURCEs! !m!Can be removed.by the way it is wrong as it should read:set SOURCEs=!SOURCEs! !n!to work, IF the variable SOURCEs is to be used.jaclaz Edited November 4, 2008 by jaclaz
oioldman Posted November 4, 2008 Posted November 4, 2008 go to microsoft and look forrobocopyguiput in what you want it to do.place tick in save scriptbingo all done and no code knowledge needed
Yzöwl Posted November 5, 2008 Posted November 5, 2008 This is what I want:step1 process the dir command and every line should become a variablestep2 read the destinationdir text file and every line should become a variablestep2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIRthats 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&SetlocalSet "_=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" /mirAs 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.
IcemanND Posted November 5, 2008 Posted November 5, 2008 Robocopy itself can make a log file of what it copies. or doesn't the same thing you see on the screen gets dumped to a file depending upon the log switches you set.
jeremyotten Posted November 7, 2008 Author Posted November 7, 2008 This is what I want:step1 process the dir command and every line should become a variablestep2 read the destinationdir text file and every line should become a variablestep2 feed the variables to robocopy like "robocopy sourcedirvariable1 destinationdirvariable2 /MIRthats 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&SetlocalSet "_=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" /mirAs 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!!
jaclaz Posted November 8, 2008 Posted November 8, 2008 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 Here is his batch "translated in an easier to read form, FYI:@Echo offSetlocalSet "Counter=0"For /f "delims=" %%A In (SOURCEDIR.TXT) Do (Set /a "Counter+=1"Call :SubRoutine1 %%Counter%% "%%A")Set "Counter="Goto :Eof:SubRoutine1REM Parameter 1 is "Counter"REM Parameter 2 is an entry of SOURCEDIR.TXTSet "AnotherCounter=0"For /f "delims=" %%B In (DESTDIR.TXT) Do (Set/a "AnotherCounter+=1"Call :SubRoutine2 %%AnotherCounter%% "%%B" %1 %2)Set "$="Goto :Eof:SubRoutine2REM Parameter 1 is "AnotherCounter"REM Paramater 2 is an entry of DESTDIR.TXTREM Parameter 3 is "Counter"REM Parameter 4 is an entry of SOURCEDIR.TXTIf %3 Equ %1 Robocopy "\\JEREMY\SOURCE$\%~4" "\\JEREMY\DESTINATION$\%~2" /mirjaclaz
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now