0

I have two drives, I: is my main drive, and J: is the backup drive for I:.

My batch below starts by deleting everything inside drive J:, except the following folders: "Folder One", "Folder Two", "System Volume Information", "$Recycle.bin". Then my batch copies drive I: (except "Folder One", "Folder Two", "System Volume Information", "$Recycle.bin") and pastes everything inside J:. It works like a charm (credits and thanks to @user1016274):

@Echo Off
md J:\empty
%SystemRoot%\System32\robocopy.exe /MIR J:\empty J:\ /XD "Folder One" "Folder Two" "System Volume Information" $Recycle.bin
%SystemRoot%\System32\robocopy.exe I:\ J:\ *.* /MIR /XD "Folder One" "Folder Two" "System Volume Information" $Recycle.bin /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32

Please, I need to improve my batch with the last two steps:

  1. Instead of "Folder One", how can I use a path with a sub-folder like "Folder One/Folder One Sub Folder"?

  2. How can I replace my

%SystemRoot%\System32\robocopy.exe I:\ J:\ *.* /MIR /XD "Folder One" "Folder Two" "System Volume Information" $Recycle.bin /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32

with a command line that:

a) It will copy everything inside I:, but it will paste at J: only files that are in I: but are not in J:.

b) It will copy files from I:, it will paste on J: even if the files already exist (files with the same name at I: and at J:), but it will replace files at J: only if something (size, date, whatever) changed.

The general idea is to copy everything inside I: and paste inside J:, but only if the files are not in J:, and only if the files exist at J: but are different from files in I:.

Hope you can help me! These are my last two steps I need to finish my batches. Thank you a lot in advance!

Compo
  • 36,585
  • 5
  • 27
  • 39
Lepefe
  • 15
  • 3

1 Answers1

1

There should be first opened a command prompt, run robocopy /? and read the output usage help carefully from top to bottom.

There are available also following documentations for the Windows command ROBOCOPY which is an executable in the Windows System32 directory available by default since Windows Vista and Windows Server 2003.

The executable is not available by default on Windows XP and former versions of Windows. %SystemRoot%\System32\Robocopy.exe can be copied from Windows Server 2003 (real machine or the ISO image of Windows Server 2003) to the directory %SystemRoot%\System32 of a machine running Windows XP SP3 to have it also available on Windows XP machine.

There is required in the batch file only a single command line:

@%SystemRoot%\System32\Robocopy.exe I:\ J:\ /DCOPY:T /MIR /MT:32 /NDL /NFL /NJH /NJS /R:0 /XJ /XD "I:\Folder One\Folder One Sub Folder" "I:\Folder Two" "I:\System Volume Information" I:\$Recycle.bin

The wildcard pattern *.* being interpreted by the Window file IO functions like * meaning any file/directory is the default and must not be explicitly specified therefore on the command line.

The option /COPY:DAT is omitted as that is the default.

For directories are copied by default also the attributes (archive, hidden, read-only, system), but not the last modification time stamp. The option /DCOPY:T results in setting the last modification time stamp of source directory also on destination directory.

It depends on file system of source and destination drive if /COPY: and /DCOPY: with additional attributes like security, owner or auditing information makes sense or not. If one of the drives is not NTFS formatted like on using FAT32 or exFAT for the backup drive, the NTFS specific attributes cannot be copied at all. If both drives are NTFS formatted, it could make sense to copy also the special NTFS attributes to have a 100% backup, but that can be also a little bit problematic on backup drive is used later on a different machine.

The mirror option /MIR causes the deletion of all files and folders in destination directory J:\ (root directory of drive J:) which do not exist in source directory I:\ (root directory of drive I:) with the exception of those directories which are excluded in source directory as specified after directory exclude option /XD.

There are ignored for that reason on both drives (in their root directories):

  1. Folder One\Folder One Sub Folder
  2. Folder Two
  3. System Volume Information
  4. $Recycle.bin

A separate directory deletion process before staring the copying/backup process is not needed at all for that reason.

The option /MIR enables by default also the options /E and /S to copy also all subdirectories including empty subdirectories and all files in the subdirectories.

It depends on the following criteria if the option /MT:32 for using up to 32 copying threads makes sense or not:

  • the number of cores the processor has at all;
  • the number of other processes running during the copy operation;
  • the type of storage media(s) of source and destination drives.
  • Is the drive J: on a different storage media than dhe drive I: or on same physical device?

For example, if the drives I: and J: are partitions on same HDD with one or more rigid rapidly rotating platters, it is counterproductive to use more than one copying thread because of all disk accesses (read on source and write on destination) must be serialized on accessing the hard disk on which the actuator arm with the magnetic heads must be moved around over the platters.

It is also better using less copying threads on machine is used further by one or more users with several running processes while the copying task is done by ROBOCOPY.

The options /NDL /NFL /NJH /NJS turn off console output during and after copying process which speeds up the copying process.

The option /R:0 sets the number of retries to 0. That makes sense on both drives are local drives. There should be used a greater value with an appropriate option /W: also specified on the command line if one of the two drives is accessed using a network as there could be also a temporary network access problem during copying the data on which retries with several seconds delay between each retry makes sense.

The option /XJ excludes following junctions (links) which could even link to directories on a different drive than the source drive I:. There should not be copied directories and files from other drives than the source drive I:. See the SS64 documentation for MKLink explaining the types of links.


There is no batch file needed at all for this task if the batch file does not contain other commands. There should be used a shortcut file for this task.

A shortcut file can be created by

  1. browsing to the directory %SystemRoot%\System32 being usually C:\Windows\System32 in Windows File Explorer,
  2. clicking with secondary (usually right) pointing device (mouse) button on the file Robocopy.exe,
  3. clicking with primary (left) pointing device (mouse) button in opened context menu on the menu item Show more options at the bottom on being shown at all in the context menu as on Windows 11,
  4. clicking on menu item Send to for opening this submenu,
  5. clicking on Desktop (create shortcut).

There is now on user's desktop the shortcut file Robocopy.exe - Shortcut with not displayed file extension .lnk. That name is not good and it is therefore best to rename the shortcut first by clicking with secondary pointing device button on the shortcut and clicking next on context menu item Show more options if not already visible the context menu item Rename which must be clicked on for renaming the shortcut file, for example, to Backup I to J. The not visible file extension .lnk is not changed by this rename operation.

The renamed shortcut file must be clicked on once again with secondary pointing device button for opening the context menu in which must be clicked on once again Show more options if necessary at all for getting displayed at bottom the menu item Properties which must be clicked on for opening the Shortcut Properties window.

The property Target contains already ROBOCOPY with its fully qualified file name. This property must be extended by appending the rest of the command line. The character @ is just for cmd.exe processing in the batch file. The property Target of the shortcut must be without this character at the beginning.

In other words property Target is either

%SystemRoot%\System32\Robocopy.exe I:\ J:\ /DCOPY:T /MIR /MT:32 /NDL /NFL /NJH /NJS /R:0 /XJ /XD "I:\Folder One\Folder One Sub Folder" "I:\Folder Two" "I:\System Volume Information" I:\$Recycle.bin

making it possible to use the shortcut file also on another computer on which Windows is installed on drive D: instead of drive C: or

C:\Windows\System32\Robocopy.exe I:\ J:\ /DCOPY:T /MIR /MT:32 /NDL /NFL /NJH /NJS /R:0 /XJ /XD "I:\Folder One\Folder One Sub Folder" "I:\Folder Two" "I:\System Volume Information" I:\$Recycle.bin

which is executed some nanoseconds faster because of no environment variable reference %SystemRoot% which must be first expanded by explorer.exe.

Start in can be kept as is. It defines the current working directory for Robocopy.exe.

The other properties on tab Shortcut and the other tabs can be configured as wanted before clicking finally on button OK to apply the changes and close the window.

The usage of a shortcut is better than the usage of a batch file because of all the options which can be configured in the shortcut properties which explorer.exe on double clicking the shortcut file passes to the Windows kernel library function CreateProcess with an appropriate filled out STARTUPINFO structure.

A double clicked batch file results in explorer.exe calling also CreateProcess to run %ComSpec% /c "C:\Path\To\Batch File.cmd" with settings stored in registry under key HKEY_CURRENT_USER\Console.

The difference between shortcut and batch file is therefore that with the shortcut file just one process is started instead of two and the user has with a shortcut file the possibility to define a nice icon for this shortcut like from file %SystemRoot%\System32\shell32.dll, define a good comment shown as tooltip on hovering the mouse pointer over the shortcut, define how the console window is opened (normal or minimized), define size of console window, text color and background color in console window, etc.

The shortcut file can be also cut from desktop and pasted into the Windows Start menu or a subfolder of the Windows Start menu (directory %APPDATA%\Microsoft\Windows\Start Menu for current user only or directory %ALLUSERSPROFILE%\Microsoft\Windows\Start Menu for all users) or any other folder or attached to the Windows taskbar.

There can be also assigned a shortcut key to run ROBOCOPY by pressing this shortcut key independent on which application has currently the input focus.

It is also possible to configure in the shortcut properties that the executable is run as administrator if that is always necessary.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • My final comment is to you @Mofi, once again to thank you for such beautiful code, and amazing answer with explanation. You help people, but you also have the patience to understand that not every user has same level of knowledge, and not every user has time to read tons of stuff (sometimes we just need the code). THKS! PS: Another advantage of the shortcut is that it can be executed automatically with administrative privileges. – Lepefe Jun 18 '23 at 11:10