Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi.
I'd like to batch copy only files, including files in subdirectories, listed in other txt. file.For example, if I want to copy a whole directory with subdirectories, I do something like this:
--
@echo off
for /f %%a ('dir /b/s c:\data') do (
copy %%a c:\bkup
)
--How can I copy only files listed in a txt file?
txt file I mentioned looks like this:
--
some.pdf
some.xls
brahbrah.doc
aaaaaa.txt
adfafd.xls
:
:
--
(yeah, only file names, no directory paths.)Any help will be appreciated.
Thanks

Use a second, nested for loop (the first does the dir) which checks to see if the filename is in your list.
=====================================
Helping others achieve escape felicityM2

Thanks Mechanix2Go
OK, I followed your instruction and wrote something like this:
--
@echo off
for /f "delims=" %%A in (sample.txt) do (
for /f "delims=" %%B in ('dir /s /b C:\document\') do copy %%B D:\bkup\
)
--However, when I run this batch, it seems the batch ignores sample.txt and copies all files (incl. files in subdir) into D:\bkup\ instead.
Then I am stuck again.

not tested
=========================
@echo off & setLocal EnableDELAYedExpansionfor /f "tokens=* delims= " %%a in ('dir/s/b/a-d C:\document') do (
find /i "%%~NXa" < sample.txt > nul
if not errorlevel 1 (
copy "%%a" D:\bkup
)
)
=====================================
Helping others achieve escape felicityM2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |