read a file and search the content in another folder, copy the same to another folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: read a file and search the content in another folder, copy the same to another folder

#16 Post by aGerman » 30 Jan 2018 14:30

Try it that way

Code: Select all

@echo off &setlocal

set "src=D:\somewhere\AllReports"
set "dest=D:\somewhere\Today_report"
set "list=D:\somewhere\daily_report.txt"

for /f "delims=." %%i in ('wmic os get LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
set "today=_%LocalDateTime:~,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%T*"

set "n=0"
:loop
set /a "n+=1"
set "flag="
>"error.log" (for /f "usebackq delims=" %%i in ("%list%") do if exist "%src%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") else (set "flag=1" &echo "%%~ni%today%%%~xi" not found))
if defined flag if %n% lss 25 (timeout /t 300 /nobreak &goto loop)
Steffen


lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: read a file and search the content in another folder, copy the same to another folder

#18 Post by lalat06bag » 30 Jan 2018 16:14

Hi Steffen,

Just a quick question. It looks, every time, the file is not found and while reiterating in the loop, this is searching for all the files in the source and copying. In that way, if 1 file is found in the 2st iteration, it would copy all the files again and paste. so basically, we are overwriting the files which are copied in the 1st iteration.

To avoid overwriting, can we 1st search in the destination folder, if the file not found, we will search in the source and copy. So every time, we will search in the destination folder and which ever is not found, we will search that and copy.

Please suggest?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: read a file and search the content in another folder, copy the same to another folder

#19 Post by Squashman » 30 Jan 2018 16:56

Just a slight code modification. Steffen might think of something better.

Code: Select all

(copy "%src%\%%~ni%today%%%~xi" "%dest%\")
to

Code: Select all

(echo N|copy /-Y "%src%\%%~ni%today%%%~xi" "%dest%\")

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: read a file and search the content in another folder, copy the same to another folder

#20 Post by lalat06bag » 31 Jan 2018 08:51

Thank you Squashman!
But I think, this one still overwrites the files which are copied already.
(echo N|copy /-Y "%src%\%%~ni%today%%%~xi" "%dest%\")

If instead of looking for the file in source, if it can check the files are present in destination folder, if not , then it would search the files in the source and copy. In that way, we can stop overwriting. Please see.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: read a file and search the content in another folder, copy the same to another folder

#21 Post by Squashman » 31 Jan 2018 10:03

lalat06bag wrote:
31 Jan 2018 08:51
Thank you Squashman!
But I think, this one still overwrites the files which are copied already.
(echo N|copy /-Y "%src%\%%~ni%today%%%~xi" "%dest%\")

If instead of looking for the file in source, if it can check the files are present in destination folder, if not , then it would search the files in the source and copy. In that way, we can stop overwriting. Please see.
No it does not. The /-Y switch prompts you to overwrite. The ECHO N answers the prompt as NO.

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: read a file and search the content in another folder, copy the same to another folder

#22 Post by lalat06bag » 31 Jan 2018 10:29

super cool. Now I got this. Thank you Squashman! Can you please have a look on error log, it is writing. This is the last iteration log, which is being written to the error log. Is there anyway, we can write the file which is not found?

Post Reply