Solved Copy only subfolders to a new location

skoller

New member
Local time
8:30 PM
Messages
4
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:

My Computer My Computer

At a glance

Windows 7 32bit
Computer type
PC/Desktop
OS
Windows 7 32bit
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:

My Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
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 My Computer

At a glance

Windows 7 32bit
Computer type
PC/Desktop
OS
Windows 7 32bit
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 My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
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 My Computer

At a glance

Windows 7 32bit
Computer type
PC/Desktop
OS
Windows 7 32bit
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 My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Works great! Thanks a lot for your help.
 

My Computer My Computer

At a glance

Windows 7 32bit
Computer type
PC/Desktop
OS
Windows 7 32bit
Back
Top