Announcement

Collapse
No announcement yet.

Copy only files, not folder structure from a directory

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Copy only files, not folder structure from a directory

    Hello all!

    A rather odd circumstance requires me to take a large directory and "flatten" it. That is to say, I need to move all files from one directory (and all sub folders in that directory) and place them in one single folder. There will be no duplicate files or filenames (I'll take care of the beforehand with one of the tools I mentioned in this thread).

    I've given a rudimentary try at this with RoboCopy. It seems like that would be the way to go, but I don't think that it ntaively supports what I need it to do. I'd have to create a script that iterates through each folder in the heirarchy and runs a simple "robocopy sourcefolder destfolder /MOV" command. Of course, I could be barking up the wrong tree altogether.

    Anyone care to throw me a bone?

    Thanks,
    Wesley David
    LinkedIn | Careers 2.0
    -------------------------------
    Microsoft Certifications: MCSE 2003 | MCSA:Messaging 2003 | MCITP:EA, SA, EST | MCTS: a'plenty | MCDST
    Vendor Neutral Certifications: CWNA
    Blog: www.TheNubbyAdmin.com || Twitter: @Nonapeptide || GTalk, Reader and Google+: [email protected] || Skype: Wesley.Nonapeptide
    Goofy kitten avatar photo from Troy Snow: flickr.com/photos/troysnow/

  • #2
    Re: Copy only files, not folder structure from a directory

    a one-liner from the Run box:
    Code:
    %comspec% /k title * & @For /F "Delims=" %! in ('Dir "c:\1234\" /b /s /a-d 2^>nul') do @echo.%! &(@xcopy "%!" "c:\SHOEBOX\" /i /y /h /q /c >nul)

    You can execute it also from a batch, in that case
    - you'll have to replace %! with %%!
    - %comspec% /k or %comspec% /c was only needed to get a dos prompt.

    batch:
    Code:
    @echo off
    title WAIT !
    
    Set "sourceDir=c:\1234"
    Set "destinationFolder=c:\SHOEBOX"
    Set "_report=c:\logxcopy.txt"
    
    IF NOT EXIST "%sourceDir%" (echo.Could not find %sourceDir% &GoTo:done)
    
    :: overwrite previous log
    >"%_report%" (
          echo.%date% - %time%
          echo.---------------------------------------------------
          echo.
    )
    
    :: copy files
    For /F "Delims=" %%! in ('Dir "%sourceDir%\" /b /s /a-d 2^>nul') do (
       @echo.%%! &(
       @xcopy "%%!" "%destinationFolder%\" /i /y /h /f /c >>"%_report%",2>&1)
    )
    
    :done
    title,Done.......
    
    echo.&pause>nul
    \Rems


    EDIT
    If you use /a in the dir command without specifying Attributes, dir displays the names of all files, including hidden and system files.
    The switch /a-d that is used in the samples, excludes 'just foldernames' and will now include the path\filenames of all files, including hidden and system files. - if you
    do not want to display hidden nor system files you must use the switch /a-d-h-s

    http://technet2.microsoft.com/window....mspx?mfr=true
    .
    Last edited by Rems; 11th June 2008, 10:28. Reason: There should not be a last backslash in "sourceDir=c:\1234\" for the IF EXIST to work

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Copy only files, not folder structure from a directory

      Excellent post Rems. It saved me a tone of time. Thank you very much.

      Comment


      • #4
        Re: Copy only files, not folder structure from a directory

        You're welcome jb_alphamale, Glad we could help.

        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment

        Working...
        X