Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Making Robocopy Script need HELP!

Featured Replies

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


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

  • Author
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.....

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
  • Author

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...

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

  • Author
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?

  • Author

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

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

go to microsoft and look for

robocopygui

put in what you want it to do.

place tick in save script

bingo all done and no code knowledge needed :)

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.

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.

  • Author
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!!

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

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.