How to bulk Copy all matching files including sub-folders from the Command Prompt

Using the Windows command prompt and the following commands will allow you to bulk copy all files matching one name to another in all sub directories/folder.

For example if you have a file cover.jpg in each folder and you want it to be copied to folder.jpg in each folder:

e.g. You have:

Z:\music\Bob Dylan\Blood On The Tracks\cover.jpg
Z:\music\Charlie Parker\Nows The Time\cover.jpg
Z:\music\Peter Gabriel\New Blood\cover.jpg

and you want copy 'cover.jpg' to 'folder.jpg' in all folders like this:

Z:\music\Bob Dylan\Blood On The Tracks\folder.jpg
Z:\music\Charlie Parker\Nows The Time\folder.jpg
Z:\music\Peter Gabriel\New Blood\folder.jpg

Use the following command from a Windows Command Prompt

Change to drive containing the files (could be a mapped network drive)

Z:

Enter the following command:

FOR /F "delims=" %i in ('dir /b /s cover.jpg') do copy "%i" "%~pifolder.jpg"

The above command is the equivalent of issuing the following commands in each our example folders:

copy "Z:\music\Bob Dylan\Blood On The Tracks\cover.jpg" "\music\Bob Dylan\Blood On The Tracks\folder.jpg"
copy "Z:\music\Charlie Parker\Nows The Time\cover.jpg" "\music\Charlie Parker\Nows The Time\folder.jpg"
copy "Z:\music\Peter Gabriel\New Blood\cover.jpg" "\music\Peter Gabriel\New Blood\folder.jpg"

Notes

FOR	%variable IN (set) DO command [command-parameters]

%variable Specifies a replaceable parameter.

(set) Specifies a set of one or more files. Wildcards may be used.

command Specifies the command to carry out for each file.

command-parameters Specifies parameters or switches for the specified command.

  • %~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

The modifiers can be combined to get compound results:

  • %~dpi - expands %i to a drive letter and path only
  • %~nxi - expands %i to a file name and extension only
  • %~fsi - expands %i to a full path name with short names only
  • %~dp$PATH:i - searches the directories listed in the PATH environment variable for %i and expands to the drive letter and path of the first one found.
  • %~ftzai - expands %i to a DIR like output line
info
Search
Close