4

Wondering if there is a script I could run in a .bat file that would allow me to copy only certain file types in a directory, and then paste them in another directory

Example of contents of directory: F:\testbatch\test1

test_01.tcs
test_02.tcs
test_03.tcs
garbagefile_01.txt
garbagefile_02.txt
nothing.rtf
test.tpl

Lets say I want to copy all .tpl & .tcs files in from F:\testbatch\test1 and paste them to:

F:\testbatch\test2

It should be noted that the destination directory F:\testbatch\test2 will already have some of the same files as the source folder, and I need these files to be overwritten.

So my question here is twofold:

1 - What command that I can put in a batch script will only copy certain types of files?

2 - How can I make sure when these files are copied & pasted that they will overwrite the existing files of the same name(s)?

EDIT I have tried:

xcopy "C:\Users\me\Desktop 3\123" *.bin "C:\Users\Jeff\Desktop 3\456" /y
xcopy "C:\Users\me\Desktop 3\123" *.tpl "C:\Users\Jeff\Desktop 3\456" /y

but nothing gets copied at all.

Running Windows 7 64 bit

1
  • Have you tried the obvious command... copy? (i.e.: Copy F:\testbatch\test1\*.tpl F:\testbatch\test2). Copy also has an option for forcing overwrites without asking (see copy /?).. Commented Apr 18, 2017 at 21:17

4 Answers 4

6

What command that I can put in a batch script will only copy certain types of files?

Any command that accepts wildcards.

For example xcopy or robocopy.


How can I make sure when these files are copied they will overwrite existing files?

Use the xcopy /y (Suppress prompt to confirm overwriting a file) option.


I want to copy all .tpl and .tcs files from F:\testbatch\test1 to F:\testbatch\test2

Use the following commands

xcopy F:\testbatch\test1\*.tpl F:\testbatch\test2 /y
xcopy F:\testbatch\test1\*.tcs F:\testbatch\test2 /y

Why isn't this working?

I have tried:

xcopy "C:\Users\me\Desktop 3\123" *.bin "C:\Users\Fiver\Desktop 3\456" /y
xcopy "C:\Users\me\Desktop 3\123" *.tpl "C:\Users\Fiver\Desktop 3\456" /y

Your quotes are in the wrong place and you are missing a backslash.

The correct commands are:

xcopy "C:\Users\me\Desktop 3\123\*.bin" "C:\Users\Fiver\Desktop 3\456" /y
xcopy "C:\Users\me\Desktop 3\123\*.tpl" "C:\Users\Fiver\Desktop 3\456" /y

Further Reading

0
1

There is no command that directly lets you pick multiple extensions types to copy files only with that extensions. The way I do it is create a batch file with multiple xcopy calls.

In your case, you want to create a batch file with the lines.

xcopy F:\testbatch\test1\*.tpl F:\testbatch\test2 /y
xcopy F:\testbatch\test1\*.tcs F:\testbatch\test2 /y

So the short answer is batch file is your friend.

1

My solution for my purposes was to image the source directory to another temporary one, but in the copy prompt only process *.pdf files

On windows I used XCOPY -

XCOPY "C:\utils\*.pdf" "D:\Backup\utilspdfs" /s /i

I then processed that folder, then copy pasted back into original folder.

You can do similarly with bash on gnulinux

cp -r source_directory/*.{png,jpg,jpeg} target_directory 2>/dev/null || :
-2

You may want to look into Robocopy. It has a multitude of different switches that can be used to get the results you want.

3
  • 2
    Rather than saying "Use this" - a good answer recommending a tool tells you how to use the tool.
    – Journeyman Geek
    Commented Apr 18, 2017 at 22:50
  • OK, noted. I had seen other comments that encourage questioners to do a modicum of work/research on their own before asking for help. Note that I had answered before the edit to the original question. Sometimes, a gentle push in the right direction is all that's needed. Commented Apr 18, 2017 at 23:15
  • There's a specific meta post on this on meta which reflects the community consensus. The "do research" bit is more for questions than answers.
    – Journeyman Geek
    Commented Apr 18, 2017 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .