There are lots of possibilities to copy all folders of a drive to another drive, for example for a backup operation, except some specified folders.
Here is one solution working for Windows XP and any newer Windows version:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:FindDrive
cls
echo Searching for external USB hard disk for backup ...
for /F "skip=2 delims=" %%H in ('%SystemRoot%\System32\wbem\wmic.exe LOGICALDISK GET DeviceID^,DriveType /Format:CSV') do for /F "tokens=2,3 delims=," %%I in ("%%H") do if "%%J" == "3" if exist "%%I\DataBackup\" set "BackupDrive=%%I" & goto RunBackup
echo/
echo Could not find USB hard disk with folder DataBackup.
echo/
if exist %SystemRoot%\System32\choice.exe goto UseChoice
setlocal EnableDelayedExpansion
:UseSetPrompt
set "UserChoice=Y"
set /P "UserChoice=Try again (Y/n): "
set "UserChoice=!UserChoice: =!"
if /I "!UserChoice!" == "N" endlocal & exit /B
if /I "!UserChoice!" == "Y" endlocal & goto FindDrive
goto UseSetPrompt
:UseChoice
%SystemRoot%\System32\choice.exe /C YN /N /T 10 /D Y /M "Try again (Y/n):"
if errorlevel 2 exit /B
goto FindDrive
:RunBackup
echo USB hard disk detected as drive %BackupDrive%
for /D %%D in (%SystemDrive%\*) do :BackupFolder "%%D"
rem for /F "eol=| delims=" %%D in ('dir %SystemDrive%\* /AD-L /B 2^>nul') do call :BackupFolder "%SystemDrive%\%%D"
exit /B
:BackupFolder
if /I "%~1" == "%SystemRoot%" goto :EOF
if /I "%~1" == "%ProgramFiles%" goto :EOF
if /I "%~1" == "%ProgramFiles(x86)%" goto :EOF
if /I "%~1" == "%SystemDrive%\$RECYCLE.BIN" goto :EOF
if /I "%~1" == "%SystemDrive%\System Volume Information" goto :EOF
echo/
echo Creating backup of folder "%~1" ...
echo/
%SystemRoot%\System32\xcopy.exe "%~1\*" "%BackupDrive%\DataBackup\%~nx1\" /C /E /H /I /K /M /Q /R /Y
goto :EOF
The upper part is just for finding out if a USB hard disk with folder DataBackup
is currently plugged in and what is its drive letter.
The user of the batch file is notified about missing USB hard disk with the folder for the data backup and can cancel the backup operation with pressing key N (case-insensitive). Or the user presses key Y and one more approach is made to find the backup hard disk. After 10 seconds the batch file automatically answers the prompt with Y
and makes one more approach to find the backup drive on choice.exe
exists which is the case since Windows Vista and Windows Server 2003, but by default not on Windows XP.
The part according to question starts with the command line:
for /D %%D in (%SystemDrive%\*) do :BackupFolder "%%D"
The FOR loop calls for each non-hidden directory in root of system drive the subroutine BackupFolder
with name of the directory as parameter.
The next line commented out with command REM would process also directories in root of system drive with hidden attribute set.
Once the loop finished processing all (non-hidden) folders on system drive, the batch file execution is exiting with command exit /B
avoiding a fall through to the subroutine BackupFolder
below main code of the batch file.
The subroutine BackupFolder
contains at top several case-insensitive executed comparisons of passed folder name with path with predefined strings to skip Windows standard folders. The Windows directory, both program files directories and the hidden directories of recycle bin and system restore are skipped by exiting the subroutine with goto :EOF
on a positive string comparison.
Otherwise all files and subfolders of passed folder with archive attribute set are copied to the data backup folder on USB hard disk and the archive attribute on files in source folder are removed to prevent copying the same unmodified files again and again in future.
So the XCOPY command copies just new or modified files and folders since last execution of this batch file to the backup hard disk which reduces the total time required for the backup.
While there are many other possibilities to exclude files and folders from a backup operation, I prefer this method as it is very easy to duplicate one of the if /I "%~1" == "..." goto :EOF
command lines and modify the string on right side to ignore one more file or folder. And more folders from other drives can be easily copied by calling BackupFolder
in command block below label RunBackup
for those additional folders.
Please note that this batch file never deletes files and folders on backup drive not existing anymore in source folder. So a file/folder cleanup on backup drive must be made from time to time manually. This is for safety to prevent an automatic deletion of backup file/folder if a file/folder in a source folder was by mistake deleted and this was not immediately recognized and file/folder was restored using backup hard disk.
There can be used also %SystemRoot%\System32\robocopy.exe
with multiple usage of option /XD
to exclude the directories on Windows Vista and Windows Server 2003 and newer client/server versions of Windows with robocopy.exe
installed by default. robocopy.exe
does not exist by default on Windows XP, but can be copied like choice.exe
from system directory of Windows Server 2003 using same kernel version as Windows XP to the system directory of Windows XP.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
cls /?
dir /?
echo /?
exit /?
for /?
goto /?
if /?
rem /?
robocopy /?
type /?
wmic /?
wmic logicaldisk /?
wmic logicaldisk get /?
xcopy /?
Read also the Microsoft article about Using command redirection operators and my answers on Single line with multiple commands using Windows batch file and How can I make an "are you sure" prompt in a Windows batch file?