copy folders containing specific file-types

December 26, 2018 at 09:32:21
Specs: Windows 7
I'm looking for a way to copy all folders from an external drive that contain files of a specific type.

For example, there is a directory with unspecific levels of folders and sub folders. they contain all kinds of files. however, as soon as one contains for example a .txt the folder and everything it contains should be copied to a specific new folder.

message edited by davebaer


See More: copy folders containing specific file-types

Report •

#1
December 26, 2018 at 19:24:31
This is untested, but "theoretically" it should work:
:: ----- begin batch snip. substitute drive/paths as appropriate
@echo off & setlocal
:: where you want them to go
set dest=c:\txt
if not exist %dest% md %dest%
:: using as example drive F: - whatever your OS has assigned to the external drive
pushd f:\
for /f "tokens=*" %%a in ('dir /b /s f:\*.txt') do copy "%%a" %dest%
::--- end batch snip

I'm sure there's more to it! The other shoe to drop...

message edited by nbrane


Report •

#2
December 26, 2018 at 20:16:14
nbrane: I'm sure there's more to it!
Namely, OP's looking to copy the entire directory with the file in question, not just the file itself.

How To Ask Questions The Smart Way


Report •

#3
December 27, 2018 at 04:42:32
gonna give it a shot

does the script have to stop after finding 1 such .txt file, or does it have to keep going?
(because there could be more than 1 folder with "veryspecialname.txt")

i5-6600K[delid]@4.7GHz/@1.392v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2666MHz@1.35v | MSI Armor RX 570 4GB@1415Mhz core@1.2v/1920MHz


Report •

Related Solutions

#4
December 27, 2018 at 05:50:35
Tested with folder computing (containing 8 dir's some contain more dirs(containing more dirs, etc.))
751 Files, 32 Folders (the 7th dir of the main folder (computing), contained the magic file export1.txt), so that entire folder was copied to the destination

turn echo on if you want to see what is happening.
replace the values of destination, motherfolder and magicfilename to whatever you need.

@echo off
set destination=C:\Users\%USERNAME%\Desktop\PLACE-HERE\
set "motherfolder=C:\Users\%USERNAME%\Desktop\Scripts\Batch\Terminal\Computing\"
set "magicfilename=export1.txt"
if not exist "%destination%" mkdir "%destination%"
:MAIN
pushd "%motherfolder%"
for /f "tokens=*" %%a in ('dir/b/AD') do call :LETSGODEEPER "%%a"
exit/b
:LETSGODEEPER
cd "%~1"
if exist "%magicfilename%" robocopy /E "%CD%" "%destination%\%~1"
for /f "tokens=*" %%A in ('dir/b/AD') do if not "%%A"=="" call :LETSGODEEPER "%%A"
cd..
exit/b

i5-6600K[delid]@4.7GHz/@1.392v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2666MHz@1.35v | MSI Armor RX 570 4GB@1415Mhz core@1.2v/1920MHz

message edited by hidde663


Report •

#5
December 27, 2018 at 21:52:40
Thanks, Razor. Knew I was missing a cylinder or two.
@OP: just don't know how you want the files to be delivered and wrapped. Options
1) keep original subdirectory structure
2) dump all into one subdirectory
3) sequentially numbered subdirectories

both options 1 and 2 potentially require a bunch of work to keep from using identical names for different items.
For example, there could be two sources: xxx\aa.txt, yyy\aa.txt, which would defeat option two unless the files are renamed to distinguish.
Option 1 might result in a lot of empty directories just to support a single one that has a .txt file 10 levels deep. (messy!).
option 3 may make them harder to locate or identify later.
All this depends on how you intend to use it. If it were me, I would use option 3 and build an index of original source to cover my ass.
Here is an example for option 3:

@echo off & setlocal enabledelayedexpansion
set c=0
set dest=c:\test
set prev=
for /f "tokens=*" %%a in ('dir /b /s f:\*.txt') do call :xx "%%a"
goto :eof

:xx
if ~p1 equ "%prev%" goto :eof
set prev=%~p1
:z
if exist %dest%\%c% (
set /a c+=1
goto :z
)
copy %prev% %dest%\%c%
>>index echo %prev% %c%
set /a c+=1
::--------- end sample

message edited by nbrane


Report •

Ask Question