Page 1 of 1

Search file and copy in other disk, but directory structure

Posted: 06 Mar 2011 15:45
by darioit
Hello Everybody,

I have new question for you expert.

My goal is search (for) a file in a disk (C) from list.txt and copy to other disk (D) in the same place (directory)

Example:
Original file:
C:\dir1\letter1.txt
C:\letter2.txt
C:\dir3\dir2\letter3.txt

file list.txt contain
letter1.txt
letter2.txt
letter3.txt

Result for new file:
D:\dir1\letter1.txt
D:\letter2.txt
D:\dir3\dir2\letter3.txt

Regards

Dario

Re: Search file and copy in other disk, but directory struct

Posted: 07 Mar 2011 06:50
by aGerman
Searching over the entire hard drive wastes a lot of time. I would like to know how long your list is. If it is not too long the DIR command could handle all on the fly. Otherwise we have to call the FINDSTR command for each file on your hard drive. That needs approx. 200ms additional time per file :|
Please give a short information.

Re: Search file and copy in other disk, but directory struct

Posted: 07 Mar 2011 07:15
by darioit
The number of item to search are 181 items on total of 300 items. (but starting all from specific folder like C:\PDF\)
Regards
Dario

Re: Search file and copy in other disk, but directory struct

Posted: 07 Mar 2011 09:18
by !k

Code: Select all

@echo off
set "src=c:\pdf\"
set "list=c:\list.txt"
set "dest=d:\"

for /f "delims=" %%f in (
'dir /a-d/b/s "%src%" ^|findstr /eig:"%list%"'
) do echo f|xcopy "%%f" "%dest%%%~pnxf"

Re: Search file and copy in other disk, but directory struct

Posted: 07 Mar 2011 12:31
by aGerman
Well, !k, I had the same idea to use the /g option. But then I noticed that it would not work right. If there is a file abc.txt in the list but you have a file yxzabc.txt into the search environment then it will be copied as well.

Code: Select all

@echo off &setlocal
for /f "tokens=* delims=" %%a in ('dir /a-d /b /s "C:\PDF\"') do (
  findstr /ixc:"%%~nxa" "list.txt" >nul &&(
    xcopy "%%~a" "D:%%~pa" /iqy
  )
)

@Dario
Take care that there are no spaces behind the file names in your list.

Regards
aGerman

Re: Search file and copy in other disk, but directory struct

Posted: 08 Mar 2011 07:33
by darioit
!k wrote:

Code: Select all

@echo off
set "src=c:\pdf\"
set "list=c:\list.txt"
set "dest=d:\"

for /f "delims=" %%f in (
'dir /a-d/b/s "%src%" ^|findstr /eig:"%list%"'
) do echo f|xcopy "%%f" "%dest%%%~pnxf"


Many Thanks, this one works really file also with space in name file 424/425 file copied (now I check the missing one)

Re: Search file and copy in other disk, but directory struct

Posted: 08 Mar 2011 07:48
by !k
Maybe file not missing but superfluous? Look to aGerman's post
example list.txt

Code: Select all

need.txt
My batch will copy both "need.txt" and "NOT_need.txt" files

And
Take care that there are no spaces behind the file names in your list.

Re: Search file and copy in other disk, but directory struct

Posted: 08 Mar 2011 07:55
by darioit
aGerman wrote:Well, !k, I had the same idea to use the /g option. But then I noticed that it would not work right. If there is a file abc.txt in the list but you have a file yxzabc.txt into the search environment then it will be copied as well.

Code: Select all

@echo off &setlocal
for /f "tokens=* delims=" %%a in ('dir /a-d /b /s "C:\PDF\"') do (
  findstr /ixc:"%%~nxa" "list.txt" >nul &&(
    xcopy "%%~a" "D:%%~pa" /iqy
  )
)

@Dario
Take care that there are no spaces behind the file names in your list.

Regards
aGerman



Same result, this one works file "but slower" also with space in name file 424/425 file copied (now I check the missing one)

Regards
Dario

Re: Search file and copy in other disk, but directory struct

Posted: 09 Mar 2011 01:44
by darioit
I try to improve a little this script, setting on top a search like "DIR /S *.PDF > list.txt but a list is comprensive of path, and the copy script doesn't work.

I can fix this problem?

Regards
Dario

Re: Search file and copy in other disk, but directory struct

Posted: 10 Mar 2011 15:54
by aGerman
Hmm, don't get it. Does it mean that you want to redirect the file names without path?

Code: Select all

>list.txt type nul
for /f "tokens=* delims=" %%a in ('dir /a-d /b /s *.pdf') do >>list.txt echo %%~nxa

But then the XCOPY code will find exactly the same files again :?

Regards
aGerman