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

后端 未结 5 1378
别那么骄傲
别那么骄傲 2020-11-30 19:33

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

相关标签:
5条回答
  • 2020-11-30 20:17

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

    0 讨论(0)
  • 2020-11-30 20:18

    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.

    0 讨论(0)
  • 2020-11-30 20:24

    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
    
    0 讨论(0)
  • 2020-11-30 20:25

    Just use the XCOPY command with recursive option

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

    /s will make it "recursive"

    0 讨论(0)
  • 2020-11-30 20:29

    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
    }
    
    0 讨论(0)
提交回复
热议问题