Copy only subfolders to a new location


  1. Posts : 4
    Windows 7 32bit
       #1

    Copy only subfolders to a new location


    Hello,
    it's time to cleanup my pc and now I got stuck on the following:
    I have the following structure:

    D: Drive

    ->Folder1
    ->Folder2
    ->Subfolder1
    ->Folder3
    ->Folder4
    ->Subfolder2
    (...)


    Now I'd like to copy only all Subfolders (including the content) of Drive D.
    I tried to find a solution with robocopy and windowssearch but didn't have any success.

    Thanks for any help in advance

    Skoller
    Last edited by skoller; 25 Sep 2015 at 10:04. Reason: posting wasn't finished
      My Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    Give this a try, sir.

    th-382167.bat
    Code:
    @echo off
    ::
    set "source=D:\"
    set "match=subfolder*"    &rem Non case sensitive match
    set "destination=C:\Users\%USERNAME%\Desktop\DestinationFolder"
    ::
    pushd %SOURCE% || exit /b 1
    for /f "delims=" %%I in (' dir /a:d-s /b . ') do (
    	echo.%%~nxI| find "%MATCH:"=%" /i >NUL && (
    		xcopy "%%~fI" "%DESTINATION:"=%\%%~nxI" /e /i /h /y >NUL
    	)
    )
    robocopy "%SOURCE:\=\\%" "%DESTINATION:"=%" >NUL
    attrib "%DESTINATION:"=%" -h -s
    popd
    It copies the folders that match "subfolder*" under D:\ and also copies all files under D:\.
    Last edited by Pyprohly; 30 Sep 2015 at 10:32. Reason: Script fixes
      My Computer


  3. Posts : 4
    Windows 7 32bit
    Thread Starter
       #3

    Hello,
    thank you for your help. I tried the code and adapted it in several ways but unfortunately I can't get it to work.
    Right now, it copies only the files in "D:\".
    It looks like that "match" doesn't find any proper folder.
    If it helps, I don't need a perfect match for "subfolder". It would be even better if there is a way to copy every subfolder - if there is one.
    Thanks a lot for any hint in advance.
    Skoller
      My Computer


  4. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #4

    I'm sorry, sir, but you're going to have to express your problem more clearly. I'm losing track of what you're trying to do?
      My Computer


  5. Posts : 4
    Windows 7 32bit
    Thread Starter
       #5

    Hello,
    thanks for your reply. I think an example makes it clear:

    My source looks like this::

    Folder1
    --->Data.txt
    FolderX
    --->Data.txt
    Folder2
    --->Subfolder1
    --->SubData.txt
    FolderY
    --->SubfolderY
    --->SubData.txt
    Now I'd like to have a script which copies only the Subfolders including the data, so the result looks like this:


    Target:

    Subfolder1
    ---> SubData.txt
    SubfolderY
    ---> SubData.txt
    Hope this example makes my problem easier to understand.
    Thanks a lot for any help and best regards
    Stefan
      My Computer


  6. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #6

    Ah, I know exactly what you wish to accomplish now.

    Too easy. Here you are, sir.


    th-382167_2.bat
    Code:
    @echo off
    ::
    set "source=D:\"
    set "destination=C:\Users\%USERNAME%\Desktop\DestinationFolder"
    ::
    pushd %SOURCE% || exit /b 1
    for /f "delims=" %%I in (' dir /a:d-s /b . ') do (
        xcopy "%%~fI" "%DESTINATION:"=%" /e /i /h /y
    )
    xcopy . "%DESTINATION:"=%" /i /h /y
    attrib "%DESTINATION:"=%" -h -s
    popd
      My Computer


  7. Posts : 4
    Windows 7 32bit
    Thread Starter
       #7

    Works great! Thanks a lot for your help.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 11:30.
Find Us