6. The 'COPY' command and Glob patterns

Before giving examples of the 'COPY' command, some words on TAB completion. Type:

D:\test>CD my Doc [ENTER]
This command -as we have seen- results in the prompt
D:\test\my Doc>
To avoid much typing, use TAB completion. Type:
D:\test\my Doc>CD .. [ENTER]
The prompt will be D:\test> again. Type now:
D:\test>CD my
and press TAB. The result is:
D:\test>CD my Doc
Press Enter and you'll see the prompt
D:\test\my Doc>
If your command does not work, try to surround names with space(s) by double quotation marks (double apostrophes).

So if this command
D:\test>CD my Doc [ENTER]
does not work, try
D:\test>CD "my Doc" [ENTER]
To be sure, I'll use in this tutorial the double quotation marks in case of names with space(s).

6.1 Copy files in the current directory to a subdirectory

To copy 'file1.txt' in our example directory to the subdirectory 'my Doc', we could type:
D:\test>COPY file1.txt "my Doc" [ENTER]
To copy the files 'file1.txt', 'file2.dat', 'file2.txt' and 'file3.txt' to 'my Doc', use Glob patterns:
D:\test>COPY file?.* "my Doc" [ENTER]
where the question mark ? refers to the number 1, 2 and 3 and the characters '.*' matches all extensions.

6.2 Copy files in the current directory into a subdirectory in binary mode

The default copy behavior treats files as ASCII-files, as lines of text (with end-of-line characters, end-of-files etc.). The command
D:\test>COPY *.jpg "my Doc" [ENTER]
will copy the image files with the extension .jpg into the subdirectory 'my Doc'. However, loading those images into an image viewer will probably give error. To avoid this, you should use the binary mode: in this case the files are copied byte for byte. Simply add the switch '/B' to the 'COPY' command:
D:\test>COPY /B *.jpg "my Doc" [ENTER]

6.3 Combine ASCII-files in the current directory to a subdirectory

Study the following command:
D:\test>COPY /A file?.* "my Doc"\combinedText.txt [ENTER]
The text files file1.txt, file2.txt, file2.dat and file3.txt are concatenated. The switch '/A' refers to the ASCII-mode, with '/B' you switch to the binary mode.

6.4 Combine binary files in the current directory to a subdirectory

To combine ('concatenate') two MP3 files (binary files!) from the current directory and copy the result to "my Doc", type:
D:\test>COPY /B *.mp3 "my Doc"\CombinedBin.mp3 [ENTER]
The CombinedBin.mp3 contains now both mp3 files of the current directory.

6.5 Are the files copied correctly?

Add the switch '/V' to the 'COPY' command to verify that the new files were written correctly.

6.6 Copy a selection of files with the FOR loop

To copy a set of files that cannot be matched with Glob patterns easily, use the 'FOR' command.

The 'FOR' syntax is quite easy:
FOR %variable IN (set) DO command [command parameters]
Study the next loop:
D:\test>FOR %i IN (file1.txt fiel2.dat) DO COPY /A /V %i "my Doc" [ENTER]
(set) is replaced by a list of two files that are separated by a space: (file1.txt file2.dat). The variable %i takes these list values: first %i has the value 'file1.txt' and then 'file2.dat'.

If 'ROBOCOPY' is available to you (check by typing 'ROBOCOPY /?' at the prompt and press Enter), then you could avoid the 'FOR' loop:
D:\test>ROBOCOPY D:\test "D:\test\my Doc" file1.txt file2.dat [ENTER]
To copy all text files from the current directory and all subdirectories, use the switch '/R' and '*.txt'
D:\test>FOR /R %i IN (*.txt) DO COPY /A /V %i D:\tmp [ENTER]
(you have to make the target directory 'tmp' first!)

With ROBOCOPY:
D:\test>ROBOCOPY /S D:\test D:\tmp *.txt [ENTER]
With the switch '/S' the '.txt' files from subdirectories will be copied also.

If D:\tmp does not exist, 'ROBOCOPY' will create the directory!

'ROBOCOPY' has many possibilities. To give a last example:
D:\test>ROBOCOPY /S /E D:\test D:\tmp [ENTER]
copies all files and subdirectories ('/S'), empty ones inclusive ('/E')!