How to recursively copy files using Windows command line

Solution 1:

Copying all files named web.foo.config to web.config in the same directory

Since you don't want to use a batch file for this operation then from an elevated command prompt, you can use the below to complete this.

This assumes the directory you are in when you run the command from the command prompt is the one which will be traversed through recursively doing the copy command of the found files.

I left the asterisk (*) from the beginning of the web.foo.config file name but you can add that where needed if it's really needed to find files with that naming pattern.

Using Copy Example

FOR /F "TOKENS=*" %F IN ('DIR /B /S web.foo.config') DO COPY /Y "%~F" "%~DPFweb.config"

Using Xcopy Example

FOR /F "TOKENS=*" %F IN ('DIR /B /S web.foo.config') DO ECHO F | XCOPY /Y /F "%~F" "%~DPFweb.config"

Further Resources

  • FOR /F
  • FOR /?

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
    

Solution 2:

Use xcopy with /S switch to copy all files and directories recursively.

At any Windows command prompt:

xcopy *.* \destination /S

Quick and easy.