views:

2483

answers:

4

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.

http://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.

Thanks all for your help! I have the greatest respect for savvy people like stackoverflow users!

+2  A: 

For the in a batch file solution

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

Well the other 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 box and it worked a treat for me. But I typed it into a command prompt so I used the single %f variable name version, as described in the linked question.

Simeon Pilgrim
A: 

Its things like these why I switched to Powershell. Try it out, its fun:

Get-ChildItem -Recurse -Include *.doc | % {
    Copy-Item $_.FullName -destination x:\destination
}
You can actually just do `gci -rec -inc *.doc | cp -dest X:\Destination` since `Copy-Item` accepts pipeline input. Also you can probably get rid of `Get-ChildItem` completely and just do `Copy-Item -Recurse -Include *.doc -Destination C:\Destination` since `Copy-Item` has `-Recurse` and `-Include` as well.
Joey
A: 

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
ghostdog74
A: 

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..)

Jay