Search file and copy in other disk, but directory structure

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Search file and copy in other disk, but directory structure

#1 Post by darioit » 06 Mar 2011 15:45

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 07 Mar 2011 06:50

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.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

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

#3 Post by darioit » 07 Mar 2011 07:15

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#4 Post by !k » 07 Mar 2011 09:18

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"

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#5 Post by aGerman » 07 Mar 2011 12:31

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

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

#6 Post by darioit » 08 Mar 2011 07:33

!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)

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#7 Post by !k » 08 Mar 2011 07:48

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.
Last edited by !k on 08 Mar 2011 07:55, edited 1 time in total.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

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

#8 Post by darioit » 08 Mar 2011 07:55

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

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

#9 Post by darioit » 09 Mar 2011 01:44

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#10 Post by aGerman » 10 Mar 2011 15:54

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

Post Reply