Search code examples
batch-filexcopy

How to copy specific subfolders if existing to new folders in a backup folder using a batch file?


I am a total beginner and I need some help to backup some folders with files from a server.

I need to copy some folders to my local computer.

Folders to copy are like this

\\DistantServer\path\RandomFolder\Config\old
\\DistantServer\path\Another Random Folder\Config\old
...

Those old folders should be copied to:

D:\Backup\RandomFolder\old
D:\Backup\Another Random Folder\old

I tried some codes, but I'm only having errors.

Could anyone help me?


Solution

  • This batch file should work for the task:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "SourceFolder=\\DistantServer\path"
    set "TargetFolder=D:\Backup"
    
    for /D %%I in ("%SourceFolder%\*") do (
        if exist "%%I\Config\Old\" (
            rd /Q /S "%TargetFolder%\%%~nxI\old" 2>nul
            %SystemRoot%\System32\xcopy.exe "%%I\Config\Old" "%TargetFolder%\%%~nxI\old\" /C /E /H /I /K /Q /R /Y >nul
        )
    )
    
    endlocal
    

    The command FOR searches in source folder as defined with environment variable SourceFolder for non hidden subfolders. Each subfolder found is assigned with full path not ending with a backslash and never enclosed in double quotes to loop variable I referenced with %%I.

    The command IF checks for existence of the subfolder Config\old in current subfolder of source folder. The found subfolder is ignored if not containing the subfolder Config\old.

    On existence of Config\old in current subfolder of source folder the command RD first removes quietly with all subfolders the appropriate target folder independent on its existence to make sure that the copy done next results in 1:1 folder copy. The RD command can be removed in case of target folder most likely never exists before the copying is done or if it is wanted that existing contents of target folder is merged with current contents of source folder.

    %%~nxI references the current subfolder in source folder without path, i.e. without folder path as defined with environment variable SourceFolder.

    The command XCOPY copies the entire folder Config\old in current subfolder of source folder to a subfolder old in target folder as defined by environment variable TargetFolder. The entire directory structure to appropriate target folder is automatically created by XCOPY if not already existing because of target specification ends with a backslash being a clear indication for XCOPY that the target string specifies a directory.

    Note: The entire code above without command RD could be optimized also to a single command line.

    @for /D %%I in ("\\DistantServer\path\*") do @if exist "%%I\Config\Old\" %SystemRoot%\System32\xcopy.exe "%%I\Config\Old" "D:\Backup\%%~nxI\old\" /C /E /H /I /K /Q /R /Y >nul
    

    Single command line with command RD:

    @for /D %%I in ("\\DistantServer\path\*") do @if exist "%%I\Config\Old\" rd /Q /S "D:\Backup\%%~nxI\old" 2>nul & %SystemRoot%\System32\xcopy.exe "%%I\Config\Old" "D:\Backup\%%~nxI\old\" /C /E /H /I /K /Q /R /Y >nul
    

    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.

    • echo /?
    • endlocal /?
    • for /?
    • if /?
    • rd /?
    • set /?
    • setlocal /?
    • xcopy /?

    Read also the Microsoft article about Using Command Redirection Operators for an explanation of >nul and 2>nul. And read answer on Single line with multiple commands using Windows batch file for an explanation of operator & as used in the last batch code (single command line with RD).