Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

windows - batch file Copy files with certain extensions from multiple directories into one directory

I'm a newbie, so bear with me...

I am trying to copy all .doc files that I have scattered throughout several subdirectories of one main directory into another directory using a batch file. I have managed to get a filelist.txt of all the files (there are hundreds) out of these directories that I want to copy using:

"C:Main directorysub directory" dir /b /s *.doc > "C:Main directorysub directoryfilelist.txt"

What script would I use to xcopy those into one directory? Can I use some code that actually grabs those file names from filelist.txt and xcopies them?

For reference, I looked at the question below because it looked like it was doing what I want to do, but it didn't work for me.

Using xcopy to copy files from several directories to one directory

Also, I would really like to understand this concept, so please break down the code for me to tell me what each item does, or at least include a link that will explain it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In a batch file solution

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

The code works as such;

for each file for in directory c:source and subdirectories /R that match pattern (*.xml) put the file name in variable %%f, then for each file do copy file copy %%f to destination x:\destination\

Just tested it here on my Windows XP computer and it worked like a treat for me. But I typed it into command prompt so I used the single %f variable name version, as described in the linked question above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...