xcopy directories and subdirectories recursively and filter only filenames by extension
Asked Answered
M

3

28

Here is what i have for now:

xcopy "c:\projects\SampleProject" "c:\temp\copytest" /E /H /EXCLUDE:elist.txt

It does all the job i need except filtering filenames by extensions.

For example: copy all *.exe files from c:\temp\copytest and subdirectories.

How to do that?

Meekins answered 1/6, 2010 at 18:39 Comment(0)
C
47

I happened to need this too, and found out that if you want to xcopy files with specific type to a new folder keeping the current folder structure you need only to do this

xcopy [SourcePath]*.mp3 [DestinationPath]  /sy

/s: Copies directories and subdirectories, unless they are empty. If you omit /s, xcopy works within a single directory.

/y : Suppresses prompting to confirm that you want to overwrite an existing destination file

Documentation

Complice answered 22/4, 2013 at 22:26 Comment(1)
When copying projects, /D statement is also useful. It orders CMD to only copy files that are newer than their destination opposite.Mender
I
3

Something like:

@echo off
setlocal
set DIR=
set OUTPUTDIR=C:\Documents and Settings\<username>\Desktop\sandbox1\output
for /R %DIR% %%a in (*.mp3) do xcopy "%%a" "%OUTPUTDIR%"

See (http://technet.microsoft.com/en-us/library/bb490909.aspx)

Inglebert answered 2/9, 2010 at 2:25 Comment(1)
what if I wanted to copy .txt files in addition to .mp3 files?Syntactics
R
0

I needed the same procedure (copy folders with exclude filter) and used the example of @pollirrata.

One thing I have to mention that in order to copy a folder I needed to specify a backslash for destination directory to suppress prompting a question.

So I ended with the following syntax:

xcopy SourceFolder DestinationFolder\ /EXCLUDE:exclude.txt

Contents of the exclude.txt file:

.mp3

To copy only files with specific extension (e.g. *.exe) I used

xcopy SourceFolder\*.exe DestinationFolder\
Reichstag answered 25/9, 2020 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.