Xcopy several times

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Xcopy several times

#1 Post by mor.bas » 07 May 2013 07:10

I want to copy some files in my batch file
how to do it more clearly then just

XCOPY "%~dp0External\Electronics" "%THIRD_FOLDER%\Electronics" /S /V /F /E /I /Y

And doing it X8 for 8 directories.

Thanks in advance....

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Xcopy several times

#2 Post by abc0502 » 07 May 2013 08:29

First SET All Your Directories ( from ) and ( to )in variables & SET the total number of the directories like this:

Code: Select all

SET "Max=3"              // Max Directory Number

SET "DIR1=C:\Test 1"     // source Dir number 1
SET "DIR2=D:\Test 2"     // source Dir number 2
SET "DIR3=E:\Test 3"     // source Dir number 3

SET "To1=C:\New Test 1"      // Destination Dir number 1
SET "To2=D:\New Test 2"      // Destination Dir number 2
SET "To3=E:\New Test 3"      // Destination Dir number 3


Now using a for command to count from 1 till the max total number it will replace the numbers 1,2,3 etc in the last DIR variable name and will pass it's value to one Xcopy command.

Code: Select all

For /L %%A In ( 1 1 %Max% ) Do (
    echo !DIR%%A! : !To%%A!
)
Pause
Now this will display the Directories in your screen, replace echo command with:

Code: Select all

XCOPY "!DIR%%A!" "!To%%A!" /S /V /F /E /I /Y 

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Xcopy several times

#3 Post by Endoro » 07 May 2013 10:25

Nice :D

Code: Select all

@echo off &setlocal enabledelayedexpansion
for %%i in (
   "C:\Test 1"="C:\New Test 1"
   "D:\Test 2"="D:\New Test 2"
   "E:\Test 3"="E:\New Test 3"
) do (
if defined source (echo XCOPY !source! %%i /S /V /F /E /I /Y
set "source=") else set "source=%%i")

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Xcopy several times

#4 Post by Ocalabob » 07 May 2013 20:04

Greetings Endoro,
Sweet work!
I incorporated your script into my Robocopy batch file. The big plus
for me is that two months from now I'll be able to easily edit the batch
file. Thank you.

For consideration:

Code: Select all

::Source DOSTIPS.COM
::FOR IN DO Author Endoro
::Edited for use with Robocopy. Tested
::WIN 7. Local Date format = Ddd MM/DD/YYYY

@echo off &setlocal enabledelayedexpansion

Set fdate=%date:~12,2%_%date:~4,2%_%date:~7,2%

Set _Switches=/COPYALL /B /SEC /MIR
:: /COPYALL :: COPY ALL file information.
:: /B       :: copy files in Backup mode.
:: /SEC     :: copy files with SECurity.
:: /MIR     :: MIRror the Source directory tree.

Set _Options=/R:1 /W:1 /NP /NFL /NDL /LOG+:C:\%fdate%_Bak_log.txt^>nul
:: /R:n   :: Number of Retries.
:: /W:n   :: Wait time in seconds between retries.
:: /NP    :: Turns off copy progress indicator (%) percentage copied.
:: /NFL   :: No File Logging.
:: /NDL   :: No Dir Logging.
:: /LOG+: :: Add to a log file named yy_mm_dd_Bak_log.txt. Create the
          :: file if it does not exist.

Echo Robocopy in Progress...

for %%i in (
   "C:\Mine"="E:\bak_files\Mine"
   "C:\My_Programs"="E:\bak_files\My_Programs"
   "C:\Utilities"="E:\bak_files\Utilities"
   "C:\Pictures"="E:\bak_files\Pictures"
   "C:\Bin"="E:\bak_files\Bin"
) do (
if defined source (Robocopy !source! %%i %_Switches% %_options%
set "source=") else set "source=%%i")

Echo.
Echo Robocopy Complete. Spacebar to Close this Window.
Pause>nul

Post Reply