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

WindowsBatch FileCommand PromptXcopy.Doc

Windows Problem Overview


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 directory\sub directory" dir /b /s *.doc > "C:\Main directory\sub directory\filelist.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.

https://stackoverflow.com/questions/585091/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.

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

Just use the XCOPY command with recursive option

xcopy c:\*.doc k:\mybackup /sy

/s will make it "recursive"

Solution 3 - Windows

Things like these are why I switched to Powershell. Try it out, it's fun:

Get-ChildItem -Recurse -Include *.doc | % {
    Copy-Item $_.FullName -destination x:\destination
}

Solution 4 - Windows

Brandon, short and sweet. Also flexible.

set dSource=C:\Main directory\sub directory
set dTarget=D:\Documents
set fType=*.doc
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
    copy /V "%%f" "%dTarget%\" 2>nul
)

Hope this helps.

I would add some checks after the copy (using '||') but i'm not sure how "copy /v" reacts when it encounters an error.

you may want to try this:

copy /V "%%f" "%dTarget%\" 2>nul|| echo En error occured copying "%%F".&& exit /b 1

As the copy line. let me know if you get something out of it (in no position to test a copy failure atm..)

Solution 5 - Windows

you can also use vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
strDestination = "c:\tmp\"
Set objFolder = objFS.GetFolder(strFolder)

Go(objFolder)

Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders		
      	Go eFolder
    Next
    For Each strFile In objDIR.Files
    	strFileName = strFile.Name
    	strExtension = objFS.GetExtensionName(strFile)
    	If strExtension = "doc" Then
    		objFS.CopyFile strFile , strDestination & strFileName
    	End If 
    Next    
  End If  
End Sub	

save as mycopy.vbs and on command line

c:\test> cscript /nologo mycopy.vbs

Solution 6 - Windows

This can be done using this command:

for /r "C:\source" %f in (*.doc) do @copy "%f" "C:\destination"

you could also run these to make it simple

set source="C:\src"
set destination="C:\dest"
set extension=doc

for /r %source% %f in (*.%extension%) do @copy "%f" %destination%

In case you are willing to copy with file types parent folder name try using this command

xcopy "C:\src\*.doc" "C:\dest" /sy

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrandonView Question on Stackoverflow
Solution 1 - WindowsSimeon PilgrimView Answer on Stackoverflow
Solution 2 - WindowsuserJTView Answer on Stackoverflow
Solution 3 - Windowsuser15071View Answer on Stackoverflow
Solution 4 - WindowsJayView Answer on Stackoverflow
Solution 5 - Windowsghostdog74View Answer on Stackoverflow
Solution 6 - WindowsAffes SalemView Answer on Stackoverflow