Link to home
Start Free TrialLog in
Avatar of elwayisgod
elwayisgodFlag for United States of America

asked on

XCOPY or COPY help

I need a script that will copy a complete directory from location A to location B.  The first time it runs it copies the whole thing.  After that it only copies new files or files that have changed.  Basically trying to build a script to backup 'My Documents' folder from hard drive to a USB drive and have it run once a week via scheduled tasks.

Avatar of Bill Prew
Bill Prew

You can't do this with either COPY or XCOPY, but ROBOCOPY can be a good tool for this.  Take a look at:

https://www.experts-exchange.com/questions/21539691/Copy-just-changed-files.html

http://ss64.com/nt/robocopy.html

~bp
use robocopy

and try this batch script

@echo off
cd\
Set RobocopyCMDSource=path of source
Set RobocopyCMDDestination=path of destination
C:\timeout 600
C:\Timethis c:\robocopy %RobocopyCMDSource% %RobocopyCMDDestination%
echo copying finished
pause
exit
copy all robocopy files to c:\ for this script or modify it
Avatar of elwayisgod

ASKER

OK so is Robocopy built into XP?  Or is it a separate program I download?  Can't figure out if I need to install something and then if I do where to find the install file....
C:\scripts>dir
 Volume in drive C has no label.
 Volume Serial Number is F02A-4B94

 Directory of C:\scripts

07/11/2010  09:42 AM    <DIR>          .
07/11/2010  09:42 AM    <DIR>          ..
07/11/2010  09:42 AM               269 backup_script.bat
               1 File(s)            269 bytes
               2 Dir(s)  95,945,363,456 bytes free

C:\scripts>call backup_script.bat > sam.log
'C:\timeout' is not recognized as an internal or external command,
operable program or batch file.
'C:\Timethis' is not recognized as an internal or external command,
operable program or batch file.
ASKER CERTIFIED SOLUTION
Avatar of Moindardt
Moindardt
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I already have a script which will do this for you, because I created it to do exactly this purpose for myself. The script will even find what drive the batch file is run from if double clicked, or you can specify it manually.

I have spent a lot of time today updating the notes so that you can easily follow and custom tailor the batch or skip any part you like.

My batch file has complete logging of the process including the files backed up each time and places this information in the destination drive though this is completely editable in variables at the top



  PS: The version of RoBoCopy they linked you to is old, you should Download the RoboCopy GUI available here to get a newer version released just prior to Windows Vista:

 http://download.microsoft.com/download/f/d/0/fd05def7-68a1-4f71-8546-25c359cc0842/UtilitySpotlight2006_11.exe

  I wouldn't bother running the GUI however it's rather useless IMO..

Please see the attached script!I think you will love this script!

::------------------------------------------------------------------------------------------------::
:: Script: RoboCopy_Backup-Any-Drive.bat                                               			  ::
:: Version: 4.0                                                                                   ::
:: Copyright: Ben Personick                                                                       ::
:: Date: 2010-07-12                                                                               ::
::                                                                                                ::
::                                                                                                ::
:: Desc: Copies Local System settings, and documents to a backup location on the HD it's run from ::
::                                                                                                ::
::------------------------------------------------------------------------------------------------::
:Begin
  ECHO Off
  SET ScriptName=RoboCopy-Backup
  ECHO ---- Begining %ScriptName%... ----

::------------------------------------------------------------------------------------------------::
:: This section sets up default variable settings and sets the logging directory and file name.   ::
::------------------------------------------------------------------------------------------------::
:Start-Prep
	ECHO off
	CALL :Start-Set-Date
	SET Preamble=%ScriptName% --
	
	::--------------------------------------------------------------------------------------------::
	:: Sets Relative Paths used to Copy later.  												  ::
	::--------------------------------------------------------------------------------------------::	
	SET SrcDrv=C:
	:: NOTE: On Windows XP SET UserDir=Documents and Settings
	SET UserDir=Users
	:: NOTE: On Windows XP SET PSTFilePath=%UserDir%\%UserName%\Local Settings\Application Data\Microsoft\Outlook
	:: NOTE: To Skip SET PSTFilePath=NOMATCH
	SET PSTFilePath=%UserDir%\%UserName%\AppData\Local\Microsoft\Outlook
	::The Following Variable Sets the Folder path to store the user files in
	SET DstShare=BKUP_WORK_REAL
	::--------------------------------------------------------------------------------------------::
	:: Set the following Variables to backup a Sharepoint to a Local Directory for Copy to USB	  ::
	::--------------------------------------------------------------------------------------------::
	:: NOTE: To skip this SET SharePointDir=URLTOSHAREPOINTSITE
	SET SharePointDir=URLTOSHAREPOINTSITE
	SET SPUserName=YOURDOMAIN\YOURUSERNAME
	SET SPPassword=1234
	SET SPLocal=Sharepoint
	
	::--------------------------------------------------------------------------------------------::
	:: Set the following Variable to hold a list of Directories under %SrcDrv%		 			  ::
	::--------------------------------------------------------------------------------------------::
	:: NOTE: Encapsulate Directories with double Quotes and separate them with Commas
	:: CAREFUL: Ensure not to leave directories below set as NOMATCH, it will terminate the copy job prematurely!
	:: NOTE: To skip SET SrcDirectories=NOMATCH
	SET SrcDirectories="Admin", "Dell", "Tools", "Temp", "%SPLocal%", "%UserDir%\%UserName%\Contacts", "%UserDir%\%UserName%\Desktop", "%UserDir%\%UserName%\Downloads", "%UserDir%\%UserName%\Favorites", "%UserDir%\%UserName%\Documents", "%UserDir%\%UserName%\Music", "%UserDir%\%UserName%\Pictures", "%UserDir%\%UserName%\Videos", "%UserDir%\%UserName%\Saved Games"

	::--------------------------------------------------------------------------------------------::
	:: If DrvDst is Set to NOMATCH it will find the drive the batch file is double-clicked from.  ::
	::--------------------------------------------------------------------------------------------::
	SET DrvDst=NOMATCH 	
	
	::--------------------------------------------------------------------------------------------::
	:: This section finds the Drive The Batch File is run from for use later in the script        ::
	::--------------------------------------------------------------------------------------------::
	IF %DrvDst% NEQ NOMATCH GOTO End-Find-Drive-Letter
	:Start-Find-Drive-Letter
		Call :Start-Find-DL-Run-From
	:End-Find-Drive-Letter
	ECHO %Preamble% Drive Run From = %DrvDst% --
	IF DstDrv == NOMATCH ECHO THE BATCH WAS NOT RUN FROM A HARD DRIVE!!
	REM IF DstDrv == NOMATCH pause
	IF DstDrv == NOMATCH GOTO :EOF
	
	::--------------------------------------------------------------------------------------------::
	:: This Section Sets up logging variables.													  ::
	::--------------------------------------------------------------------------------------------::
	SET IDir=%DrvDst%\Admin\%ScriptName%\Logs
	SET ILog=%ScriptName%_%TDate%_%TTime%.Log
	SET IDL=%IDir%\%ILog%
	
	::--------------------------------------------------------------------------------------------::
	:: This Section Creates Directories for Logging and other files								  ::
	::--------------------------------------------------------------------------------------------::
	:Start-Create-Directories
		For /F %%D IN ("%IDir%") DO CALL :Start-DIR-Loop %%D
		GOTO End-DIR-Loop
		:Start-DIR-Loop
			SET TDir=%1
			ECHO %Preamble% Checking For %TDir%
			IF EXIST %TDir% CALL :Start-Echo "%TDir% -- Found!"
			IF EXIST %TDir% GOTO :EOF
			ECHO %Preamble% Creating %TDir%
			MKDir %TDir%
			CALL :Start-Echo "Created %TDir%"
			GOTO :EOF
		:End-DIR-Loop
		CALL :Start-Echo "Dir-Loop Completed"
	:End-Create-Directories
	
	::--------------------------------------------------------------------------------------------::
	:: This Section Sets Default Values for match variables, don't change except for testing.	  ::
	::--------------------------------------------------------------------------------------------::
	SET OSVer=NOMATCH
	Set vTemp=NOMATCH
	SET OSSP=NOMATCH
	SET OSbit=NOMATCH
	SET OSSPExit=NOEXIT
	SET RoBoVista=NO
	SET SkipLoop=NO

:End-Prep

:Start-Begin-Log
	CALL :Start-Set-Date
	CALL :Start-Echo "BEGIN Script on %Date%"
	CALL :Start-Echo " "
	CALL :Start-Echo "------------------------------------------------------"
	CALL :Start-Echo "BEGIN %ScriptName% Script on %TDate% AT %TTime%"
	CALL :Start-Echo "------------------------------------------------------"
	CALL :Start-Echo " "
:End-Begin-Log



::------------------------------------------------------------------------------------------------::
:: This section finds the version of the operating system for use later in the script             ::
::------------------------------------------------------------------------------------------------::
:Start-FIND-OS

	:Start-FIND-Ver
		
		FOR %%V IN (3.1, 3.5, 3.51, 4.0, 5.0, 5.1, 5.2, 6.0, 6.1) DO call :Start-Ver-Loop %%V
		GOTO End-Ver-Loop
		:Start-Ver-Loop
			If %OSVer% NEQ NOMATCH GOTO :EOF
			SET TVer=%1
			CALL :Start-Echo "TVer = %TVer%"
			VER | FIND "%TVer%"
			IF ERRORLEVEL 1 GOTO :EOF
			IF %TVer% == 3.1 SET OSVer=NT%TVer%
			IF %TVer% == 3.5 SET OSVer=NT%TVer%
			IF %TVer% == 3.51 SET OSVer=NT%TVer%
			IF %TVer% == 4.0 SET OSVer=NT4
			IF %TVer% == 5.0 SET OSVer=2K
			IF %TVer% == 5.1 SET OSVer=XP
			IF %TVer% == 5.2 SET OSVer=2K3
			IF %TVer% == 6.0 SET OSVer=Vista
			IF %TVer% == 6.1 SET OSVer=7
			GOTO :EOF
			
		:End-Ver-Loop
		CALL :Start-Echo "End-Ver-Loop"
		IF %OSVer% == Vista (SET RoBoVista=GO)  ELSE IF %OSVer% == 7 (SET RoBoVista=GO) ELSE (SET RoBoVista=NO)
		IF %OSVer% == Vista (GOTO 2K8?-Start)  ELSE IF %OSVer% == 7 (GOTO 2K8?-Start) ELSE (GOTO 2K8?-end)
		:2k8?-start
			CALL :Start-Echo "2k8?-start"
			START /WAIT REGEDIT.EXE /E "%Temp%.\%%~n0.dat" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" 
			  FOR /F "tokens=2 delims==" %%A IN ('TYPE "%Temp%.\%%~n0.dat" ^| FIND /I "ProductName"') DO SET VTemp=%%~A 

			ECHO %VTemp% | find "2008"
			if errorlevel = 1 (GOTO 2k8?-end)  else (set OSVer=2K8)
			
		:2k8?-end

	:End-Find-Ver
	 
	
	:Start-FIND-SP
		CALL :Start-Echo "%OSSP%"
		IF 	%OSSP% NEQ NOMATCH GOTO End-FIND-SP
		START /WAIT REGEDIT.EXE /E "%Temp%.\%%~n0.dat" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" 
		FOR /F "tokens=2 delims==" %%A IN ('TYPE "%Temp%.\%%~n0.dat" ^| FIND /I "CSDVersion"') DO SET OSSP=%%~A 
		IF ERRORLEVEL 1 SET OSSP= 
		DEL "%Temp%.\%%~n0.dat" 
		CALL :Start-Echo "%OSSP%"

		FOR %%S IN (1, 2, 3, 4, 5, 6) DO call :Start-SP-Loop %%S
		CALL :Start-Echo "End 2nd for"
		GOTO End-SP-Loop
		:Start-SP-Loop
			If %OSSPExit% == EXIT GOTO :EOF
			SET TSP=%1
			CALL :Start-Echo "TSP = %TSP%"
			ECHO %OSSP% | FIND "%TSP%"
			IF ERRORLEVEL 1 GOTO :EOF
			SET OSSP=SP%TSP%
			SET OSSPExit=EXIT
			GOTO :EOF
			
		:End-SP-Loop
		CALL :Start-Echo "End-SP-Loop"
		
	:End-FIND-SP
	CALL :Start-Echo "End Find SP"

::------------------------------------------------------------------------------------------------::
:: This section finds Whether the Operating system is 32bit or 64bit for use later in the script. ::
::------------------------------------------------------------------------------------------------::
	:Start-Find-OS-Architechture

		:Start-Find-bit-Level

			FOR %%V IN (86, 64) DO call :Start-bit-Loop %%V
			GOTO End-bit-Loop
			:Start-bit-Loop
				If %OSbit% NEQ NOMATCH GOTO :EOF
				SET Tbit=%1
				CALL :Start-Echo "Tbit = %Tbit%"
				ECHO %PROCESSOR_ARCHITECTURE% | FIND "%Tbit%"
				IF ERRORLEVEL 1 GOTO :EOF
				SET OSbit=%Tbit%
				If %Tbit% == 86 SET OSbit=32
				GOTO :EOF
				
			:End-bit-Loop
			CALL :Start-Echo "End-bit-Loop"
			
		:End-Find-bit-Level

	:End-Find-OS-Architechture
	
::------------------------------------------------------------------------------------------------::
:: This section is intended to determin if the OS is Desktop or Server for differing installs.    ::
::------------------------------------------------------------------------------------------------::
	:Start-Set-Desktop-OS-Runs
		SET DTOSRun=NO
		CALL :Start-Echo "Checking if Desktop OS"
		CALL :Start-Echo "DTOSRun = %DTOSRun%, OSVer = %OSVer%"
		CALL :Start-Echo "Set DTOSRun to GO Only if OSVer is a desktop OS"
		IF /i %OSVer% == XP SET DTOSRun=GO
		IF /i %OSVer% == Vista SET DTOSRun=GO
		IF /i %OSVer% == 7 SET DTOSRun=GO
		
	:End-Set-Desktop-OS-Runs
	
	CALL :Start-Echo "OS Version = %OSVer%"
	CALL :Start-Echo "OS SP Version = %OSSP%"
	CALL :Start-Echo "OS Architechture = %OSbit% bit"
	CALL :Start-Echo "OS Desktop? (NO or GO) = %DTOSRun%"
	
:End-FIND-OS




:Start-Main

:Start-Copy-Sharepoint
	IF /I "%SharePointDir%"=="URLTOSHAREPOINTSITE" GOTO End-Copy-Sharepoint
	CALL :Start-Set-Date
	CALL :Start-Echo "Start-Copy-Sharepoint ON %TDate% AT %TTime%"
	Net Use "%SharePointDir%" /USER:%SPUserName% %SPPassword%
	IF %RoBoVista%==GO (Robocopy "%SharePointDir%" "%SrcDrv%\%SPLocal%" * /ZB /E /DCOPY:T /MIR /LOG+:%IDL% /TEE /NP /R:2 /W:2)
	IF %RoBoVista%==NO (Robocopy "%SharePointDir%" "%SrcDrv%\%SPLocal%" * /ZB /E /MIR /LOG+:%IDL% /TEE /NP /R:2 /W:2)
	Net Use "%SharePointDir%" /DELETE
:End-Copy-Sharepoint


:Start-DoCopy

	CALL :Start-Set-Date
	CALL :Start-Echo "Start-DoCopy ON %TDate% AT %TTime%"
	REM Below is the For Loop, %%A is the term pulled from the set to pass to the command loop
	REM  The terms are located within the open and close parenthesis, and quotes allow spaces to be ignored
	REM  If Quotes were not used, the spaces would be seen as a delimiter just as commas are.
	FOR %%A IN (%SrcDirectories%) DO call :Start-DoCopy-Loop %%A
	GOTO End-DoCopy-Loop
	:Start-DoCopy-Loop
		IF /I %1=="NOMATCH" SET SkipLoop=YES
		IF /I %SkipLoop%==YES GOTO :EOF
		CALL :Start-Set-Date
		REM This is the command loop, everythign inside this loop will be preocessed as a separate batch file
		REM we use %~1 for the variable the Tilde says to strip quotes off the passed text XD
		CALL :Start-Echo "ON %TDate% AT %TTime% Start-%~1"
		IF %RoBoVista%==GO (Robocopy "%SrcDrv%\%~1" "%DrvDst%\%DstShare%\%~1" * /ZB /E /DCOPY:T /LOG+:%IDL% /MIR /TEE /NP /R:2 /W:2 /XJD)
		IF %RoBoVista%==NO (Robocopy "%SrcDrv%\%~1" "%DrvDst%\%DstShare%\%~1" * /ZB /E /LOG+:%IDL% /MIR /TEE /NP /R:2 /W:2)
		GOTO :EOF
	:End-DoCopy-Loop

:End-DoCopy
CALL :Start-Set-Date
CALL :Start-Echo "End-DoCopy ON %TDate% AT %TTime%"

:Start-Copy-PST
	IF /I "%PSTFilePath%"=="NOMATCH" GOTO End-Copy-PST
	CALL :Start-Set-Date
	CALL :Start-Echo "Start-Copy-PST ON %TDate% AT %TTime%"
	IF %RoBoVista%==GO (Robocopy "%SrcDrv%\%PSTFilePath%" "%DrvDst%\%PSTFilePath%" *.pst /ZB /E /DCOPY:T /LOG+:%IDL% /MIR /TEE /NP /R:2 /W:2)
	IF %RoBoVista%==NO (Robocopy "%SrcDrv%\%PSTFilePath%" "%DrvDst%\%PSTFilePath%" *.pst /ZB /E /LOG+:%IDL% /MIR /TEE /NP /R:2 /W:2)

:End-Copy-PST

:End-Main
GOTO :End


GOTO End-Subs
GOTO :End
:Start-Subs
	GOTO :End-Set-Date
	:Start-Set-Date
		SET TDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
		SET TTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
		GOTO :EOF
	:End-Set-Date
	GOTO End-Echo
	:Start-Echo
		Set TEcho=%~1
		ECHO %Preamble% %TEcho% --
		ECHO %Preamble% %TEcho% -- >> %IDL%
		GOTO :EOF
	:End-Echo
	GOTO End-Find-DL-Run-From
	:Start-Find-DL-Run-From
		FOR %%V IN (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z) DO call :Start-DL-Loop %%V
		GOTO End-DL-Loop
		:Start-DL-Loop
			If %DrvDst% NEQ NOMATCH GOTO :EOF
			SET TDrv=%1:
			REM ECHO %Preamble% TDrv = %TDrv% --
			Dir /D | FIND " Directory of " | Find "%TDrv%\"
			IF ERRORLEVEL 1 GOTO :EOF
			SET DrvDst=%TDrv%
			GOTO :EOF
		:End-DL-Loop
		REM Echo %Preamble% End-DL-Loop --
		GOTO :EOF
	:End-Find-DL-Run-From

:End-Subs


:End
	CALL :Start-Set-Date
	CALL :Start-Echo "!!--!! %ScriptName% Exiting ON %TDate% AT %TTime% !!--!!
	GOTO :EOF

Open in new window

nice and simple script to copy a to b, excluding older files in directory a..

robocopy c:\source\a d:\destination\b /e /xo /r:3 /w:5 /log+c:\backuplog.txt

best of luck
so mine was not accepted????
EY - Does yours:  

1 - Copy the entire directory first time
2 - Only new or modified files thereafter?

yep it works like that .... it will synchronize the data between two folders
@ EY:  your script has the usage of 'timethis' in it.  you woudl need to let the author know where to get this command utility from

@EL:  Yes, his script does both, but he used a non-standard Command utility which you need to get from the Windows 2000 resource kit, so if you ran it it would fail.

  Also he did not escape the directories inside quotes so you may have had directories with spaces in them and again it would die.

Finally:  Every script posted here would work to do the basic requirements, however some had additional functionality such as mine has logging and allows you to specify multiple directories.  Therefore it was really a matter of you picking the script you liked the most, IMO.  or the one you found the most helpful.
thankyou Qcubed ..
thanks for the information
@ EL
its ok
I definitely don't want to get into a pissing match over this.  What I really want is a very simple command that will suffice my first two requirements.  I'm willing to use Robocopy but find that download confusing.  I'm not really sure how/where to download the Robocopy version I need.  Where/How to install it.  And then finally how to use it.  Can someone explain in laymans terms how to do that?
Now I'm getting confused, you already accepted a solution (although the post you accepted really isn't a solution on it's own) that was ROBOCOPY based, but now it sounds like you aren't real comfortable with ROBOCOPY?

~bp
I'm confused too actually.  
All of our scripts require A version of RoBoCopy be installed.

  • However you should not have handed out points and then asked for clarification because your question remains essentially 'unanswered'..
RoBoCopy comes are a normal part of Windows Vista, 2k8, 7, and 2k8R2

  To Download the newest version of Robocopy available as a stand-alone download you should DL the Robocopy GUI, you may even want to use it if it's a large issue for you to use our scripts. (SEE BELOW FOR LINKS)

Proper RoboCopy Syntax is:

Robocopy "Source" (NO TRAILING \ ) "Destination" (NO TRAILING \ ) FileMask (I use * for backups you may like *.* but it will skip files that do not have an extension at the end of them) Options
IE. to copy all files in C:\test SRC directory\ TO C:\Test DST Dir\ you would write:

Robocopy "C:\Test SRC Directory" "C:\Test DST Directory" * /ZB
Options you should use always: /ZB (Use Restartable mode for each file, unless the file is locked, then use backup mode)

To Copy Sub directories as well as the original directory use the /E Option (/E copies empty directories as well as ones with files /S only copies directories that have files within them)

Robocopy "C:\Test SRC Directory" "C:\Test DST Directory" * /ZB /E


You can Download the Robocopy GUI here:

http://download.microsoft.com/download/f/d/0/fd05def7-68a1-4f71-8546-25c359cc0842/UtilitySpotlight2006_11.exe

here is info on using the GUI here:

http://www.mydigitallife.info/2007/05/08/microsoft-robocopy-gui-free-download/

Does this clear up all your questions?