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
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..)
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.
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
Just use the XCOPY command with recursive option
xcopy c:\*.doc k:\mybackup /sy
/s will make it "recursive"
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
}