Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMD Command to Copy files with certain extension

Tags:

cmd

So I'm using this command to copy only txt files from a certain directory to another directory

for /R c:\source %%f in (*.xml) do copy %%f x:\destination\

But it only copies over the text files without a space in the name, so it copies test.txt but not test 2.txt. How do I make it so it copies txt files with spaces?

like image 580
Richard Avatar asked Jan 21 '10 22:01

Richard


People also ask

How do I copy a file with a specific extension in Windows?

To copy the files with the specific extension, you need to use the Get-ChildItem command. Through the Get-ChildItem you first need to retrieve the files with the specific extension(s) and then you need to pipeline Copy-Item command.

How do I duplicate files using the command line?

Highlight the files you want to copy. Press the keyboard shortcut Command + C . Move to the location you want to move the files and press Command + V to copy the files.

Can xcopy move files?

Xcopy has no built-in functionality to move files and folders from the source to the destination. But, as a workaround, you can create a script that would Xcopy the files first and then delete the files from the source. The code below will copy the files to the destination.


2 Answers

Add quotes just around the variable after the copy command:

for /R c:\source %%f in (*.xml) do copy "%%f" x:\destination\
like image 188
stepanian Avatar answered Oct 19 '22 15:10

stepanian


What's wrong with

copy c:\source\*.xml x:\destination\ >nul

[Edit] Oh I see, you want to copy all the files in all directories recursively, but without copying the directory structure. Nevermind, then.

like image 21
BlueRaja - Danny Pflughoeft Avatar answered Oct 19 '22 14:10

BlueRaja - Danny Pflughoeft