none
Find folders listed in txt file in subdirectories and copy to target directory? RRS feed

  • Question

  • Hi Guys!

    I have a list of folder names in a text file, which I need to find and copy over to a new directory. This is an example of what the directory I have to search through looks like:

    C:\test\folder1\subfolder1\sub_subfolder1
    C:\test\folder1\subfolder1\sub_subfolder2
    C:\test\folder1\subfolder2\sub_subfolder1
    C:\test\folder1\subfolder2\sub_subfolder2
    C:\test\folder1\subfolder3\sub_subfolder1
    C:\test\folder1\subfolder3\sub_subfolder2

    So the folders that I have to find are the ones listed as sub_subfolder. I only have the names of the folders in the text file and not the full path. I'd tried searching using the Windows search feature but that was taking forever. A friend told me to try the FOR command or FORFILES command and xcopy. But I was not getting anywhere with it. 
    Here are the few scripts I tried:

    for /f "delims=" %i in (FILE.txt) do echo D|xcopy "C:\test\%i" "C:\test\newdest\%i" /i /z /y /s
     
    FOR /F %F IN (FILE.txt) DO xcopy /I /E "C:\test\%F" "C:\test\newdest\%F"
     
    for /f "tokens=*" %i in (FILE.txt) DO xcopy "C:\test\%i" "C:\test\newdest\%i"
     
    @echo off
    set src_folder=C:\test\
    set dst_folder=C:\test\newdest\
    for /f "tokens=*" %i in (FILE.txt) DO (
        xcopy /S/E "%src_folder%%%i" "%dst_folder%"
    )

    So to reiterate what I am trying to do is find the list of folders that are listed in the text file, and copy them over to C:\test\newdest\. Any help would be appreciated!!

    Thanks,
    E

    • Moved by Bill_Stewart Wednesday, May 30, 2018 7:29 PM This is not "scripts on demand"
    Tuesday, April 3, 2018 1:14 PM

All replies

  • This should be able to get you somewhere, PowerShell is much better to use than your previous approaches.

    Get-Content -Path "PATH_TO_TXT_FILE" | % { Copy-Item -Path "C:\test\$_" -Destination "c:\test\newdest" }
    
    If sub folders in each in different folders themselves, then the above script probably won't work and you'll need to add extra logic. Let us know how you get on.


    Learn PowerShell                     Script Requests

    -Remember to mark the correct response as the answer-

    Tuesday, April 3, 2018 2:10 PM