read a file and search the content in another folder, copy the same to another folder
Moderator: DosItHelp
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
read a file and search the content in another folder, copy the same to another folder
Hi,
In a notepad, there are several .pdf filenames. Batch would read the file , line by line, take each file name, search for the same file name in a different folder , if found , it would copy the same file and put in a different folder.
Say daily_report.txt has a.pdf, b.pdf, c.pdf.
Now batch would take a.pdf, look for the same file in AllReports folder, if found, it would copy this to Today_report folder.
If not found, writes error in the error.log, move to the next file.
In a notepad, there are several .pdf filenames. Batch would read the file , line by line, take each file name, search for the same file name in a different folder , if found , it would copy the same file and put in a different folder.
Say daily_report.txt has a.pdf, b.pdf, c.pdf.
Now batch would take a.pdf, look for the same file in AllReports folder, if found, it would copy this to Today_report folder.
If not found, writes error in the error.log, move to the next file.
Re: read a file and search the content in another folder, copy the same to another folder
Maybe something like that:
Steffen
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 "usebackq delims=" %%i in ("%list%") do if exist "%src%\%%~i" copy "%src%\%%~i" "%dest%\%%~i"
-
- 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
Thank you. This is working perfectly fine. how to write the the file name to a log file, which is not found in the AllReports folder?
say a.pdf , b.pdf does not exist in AllReports folder, then error.log will be updated as filename not found.
say a.pdf , b.pdf does not exist in AllReports folder, then error.log will be updated as filename not found.
Re: read a file and search the content in another folder, copy the same to another folder
Just update the last line as follows
Steffen
Code: Select all
for /f "usebackq delims=" %%i in ("%list%") do if exist "%src%\%%~i" (copy "%src%\%%~i" "%dest%\%%~i") else >>"error.log" echo "%%~i" not found
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
-
- 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
I have another complexity just came to the response of the above.
In the AllReports folder, report came as below format. Basically, it would contain reports in all dates.
WABC_From_after100MM-en-us_2018-01-20T023511433Z-pdf_desc.xml
WABC_From_after100MM-en-us_2018-01-20T023511759Z.xlsx
WABC_From_after100MM-en-us_2018-01-20T023511759Z-xlsx_desc.xml
WCIR_REX_BD_shops-en-us_2018-01-23T024756751Z.pdf
WCIR_REX_FX_shops-en-us_2018-01-23T024756751Z-pdf_desc.xml
WCIR_REX_FX_shops-en-us_2018-01-23T024757245Z.xlsx
WZIB_From_after100MM-en-us_2018-01-24T023556587Z.pdf
WZIB_From_after100MM-en-us_2018-01-24T023556587Z-pdf_desc.xml
WZIB_From_after100MM-en-us_2018-01-24T023556821Z.xlsx
WZIB_From_after100MM-en-us_2018-01-24T023556821Z-xlsx_desc.xml
daily_report.txt would be having the file names as
WABC_From_after100MM-en-us.xml
WABC_From_after100MM-en-us.xlsx
WCIR_REX_BD_shops-en-us.pdf
WCIR_REX_FX_shops-en-us.xml
WCIR_REX_FX_shops-en-us.xlsx
WZIB_From_after100MM-en-us.pdf
WZIB_From_after100MM-en-us.xml
So while picking up the file names as WABC_From_after100MM-en-us.xml from daily_report.txt, it would append today's date as WABC_From_after100MM-en-us_2018-01-24.xml and search in AllReports folder and pick all the matching files in todays date.
So Today_report folder would be having below content.
WZIB_From_after100MM-en-us_2018-01-24T023556587Z.pdf
WZIB_From_after100MM-en-us_2018-01-24T023556587Z-pdf_desc.xml
WZIB_From_after100MM-en-us_2018-01-24T023556821Z.xlsx
WZIB_From_after100MM-en-us_2018-01-24T023556821Z-xlsx_desc.xml
In the AllReports folder, report came as below format. Basically, it would contain reports in all dates.
WABC_From_after100MM-en-us_2018-01-20T023511433Z-pdf_desc.xml
WABC_From_after100MM-en-us_2018-01-20T023511759Z.xlsx
WABC_From_after100MM-en-us_2018-01-20T023511759Z-xlsx_desc.xml
WCIR_REX_BD_shops-en-us_2018-01-23T024756751Z.pdf
WCIR_REX_FX_shops-en-us_2018-01-23T024756751Z-pdf_desc.xml
WCIR_REX_FX_shops-en-us_2018-01-23T024757245Z.xlsx
WZIB_From_after100MM-en-us_2018-01-24T023556587Z.pdf
WZIB_From_after100MM-en-us_2018-01-24T023556587Z-pdf_desc.xml
WZIB_From_after100MM-en-us_2018-01-24T023556821Z.xlsx
WZIB_From_after100MM-en-us_2018-01-24T023556821Z-xlsx_desc.xml
daily_report.txt would be having the file names as
WABC_From_after100MM-en-us.xml
WABC_From_after100MM-en-us.xlsx
WCIR_REX_BD_shops-en-us.pdf
WCIR_REX_FX_shops-en-us.xml
WCIR_REX_FX_shops-en-us.xlsx
WZIB_From_after100MM-en-us.pdf
WZIB_From_after100MM-en-us.xml
So while picking up the file names as WABC_From_after100MM-en-us.xml from daily_report.txt, it would append today's date as WABC_From_after100MM-en-us_2018-01-24.xml and search in AllReports folder and pick all the matching files in todays date.
So Today_report folder would be having below content.
WZIB_From_after100MM-en-us_2018-01-24T023556587Z.pdf
WZIB_From_after100MM-en-us_2018-01-24T023556587Z-pdf_desc.xml
WZIB_From_after100MM-en-us_2018-01-24T023556821Z.xlsx
WZIB_From_after100MM-en-us_2018-01-24T023556821Z-xlsx_desc.xml
Last edited by lalat06bag on 24 Jan 2018 11:44, edited 1 time in total.
Re: read a file and search the content in another folder, copy the same to another folder
Why do files with suffix 2018-01-23... belong to todays files?
Steffen
Steffen
-
- 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
sorry, my mistake. Just corrected that. It would be having the files of today's date only.
Re: read a file and search the content in another folder, copy the same to another folder
Try
Steffen
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*"
for /f "usebackq delims=" %%i in ("%list%") do if exist "%src%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") else >>"error.log" echo "%%~ni%today%%%~xi" not found
-
- 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
Thank you so much. It worked perfectly fine.
-
- 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
Hi,
Please look into this in response to the above.
@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*"
for /f "usebackq delims=" %%i in ("%list%") do if exist "%src%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") else >>"error.log" echo "%%~ni%today%%%~xi" not found
The above piece worked for me, but we later realized, there may be delay in generating the reports in the source folder. So in every 5 mins, we want to search the destination folder, if the report mentioned in the flat file appended by today's date is not found, it will start the search in the source and copy. When all the file names mentioned in the flat file are available in the destination folder, it will be out of the wait and will not search. It will do this for 2 hours, if not found at the end of 2 hours, it would write this to the error.log.
In the AllReports folder, reports are in below format.
WABC_From_after100MM-en-us_2018-01-20T023511433Z-pdf_desc.xml
WABC_From_after100MM-en-us_2018-01-20T023511759Z.xlsx
WABC_From_after100MM-en-us_2018-01-20T023511759Z-xlsx_desc.xml
WCIR_REX_BD_shops-en-us_2018-01-23T024756751Z.pdf
WCIR_REX_FX_shops-en-us_2018-01-23T024756751Z-pdf_desc.xml
WCIR_REX_FX_shops-en-us_2018-01-23T024757245Z.xlsx
WZIB_From_after100MM-en-us_2018-01-30T023556587Z.pdf
WZIB_From_after100MM-en-us_2018-01-30T023556587Z-pdf_desc.xml
WZIB_From_after100MM-en-us_2018-01-30T023556821Z.xlsx
WZIB_From_after100MM-en-us_2018-01-30T023556821Z-xlsx_desc.xml
flat file "daily_report.txt" is having the file names as
WABC_From_after100MM-en-us.xml
WABC_From_after100MM-en-us.xlsx
WCIR_REX_BD_shops-en-us.pdf
WCIR_REX_FX_shops-en-us.xml
WCIR_REX_FX_shops-en-us.xlsx
WZIB_From_after100MM-en-us.pdf
WZIB_From_after100MM-en-us.xml
Right now, the above code is picking the filenames from the flat file "daily_report.txt", searching the file names_today's date in the source folder and copying those to Today_report folder which is the destination folder.
Please see, if we can tweak this.
We have the file names in the flat file. We will search for those file names appended by today's date in the desitnation folder 1st, if not found, we will search this in the source and when found, we will copy this to the destination folder. Wait for 5 mins, again do a search from the destination folder, if not found, do the same thing till all the files are found in the destination folder. If after 2 hours of search, all the files are not found, write the file name to the error.log.
Please help in this.
Please look into this in response to the above.
@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*"
for /f "usebackq delims=" %%i in ("%list%") do if exist "%src%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") else >>"error.log" echo "%%~ni%today%%%~xi" not found
The above piece worked for me, but we later realized, there may be delay in generating the reports in the source folder. So in every 5 mins, we want to search the destination folder, if the report mentioned in the flat file appended by today's date is not found, it will start the search in the source and copy. When all the file names mentioned in the flat file are available in the destination folder, it will be out of the wait and will not search. It will do this for 2 hours, if not found at the end of 2 hours, it would write this to the error.log.
In the AllReports folder, reports are in below format.
WABC_From_after100MM-en-us_2018-01-20T023511433Z-pdf_desc.xml
WABC_From_after100MM-en-us_2018-01-20T023511759Z.xlsx
WABC_From_after100MM-en-us_2018-01-20T023511759Z-xlsx_desc.xml
WCIR_REX_BD_shops-en-us_2018-01-23T024756751Z.pdf
WCIR_REX_FX_shops-en-us_2018-01-23T024756751Z-pdf_desc.xml
WCIR_REX_FX_shops-en-us_2018-01-23T024757245Z.xlsx
WZIB_From_after100MM-en-us_2018-01-30T023556587Z.pdf
WZIB_From_after100MM-en-us_2018-01-30T023556587Z-pdf_desc.xml
WZIB_From_after100MM-en-us_2018-01-30T023556821Z.xlsx
WZIB_From_after100MM-en-us_2018-01-30T023556821Z-xlsx_desc.xml
flat file "daily_report.txt" is having the file names as
WABC_From_after100MM-en-us.xml
WABC_From_after100MM-en-us.xlsx
WCIR_REX_BD_shops-en-us.pdf
WCIR_REX_FX_shops-en-us.xml
WCIR_REX_FX_shops-en-us.xlsx
WZIB_From_after100MM-en-us.pdf
WZIB_From_after100MM-en-us.xml
Right now, the above code is picking the filenames from the flat file "daily_report.txt", searching the file names_today's date in the source folder and copying those to Today_report folder which is the destination folder.
Please see, if we can tweak this.
We have the file names in the flat file. We will search for those file names appended by today's date in the desitnation folder 1st, if not found, we will search this in the source and when found, we will copy this to the destination folder. Wait for 5 mins, again do a search from the destination folder, if not found, do the same thing till all the files are found in the destination folder. If after 2 hours of search, all the files are not found, write the file name to the error.log.
Please help in this.
Last edited by lalat06bag on 30 Jan 2018 11:48, edited 1 time in total.
Re: read a file and search the content in another folder, copy the same to another folder
Sounds like you need to use the TIMEOUT command for your 5 minute delay and create a variable to keep track of the amount of time it has waited when it has not found the file. So every time the file is not found you add 300 to the variable. When that variable equals 7200, you will write to the log file. If it not 7200 seconds GOTO a label to loop back to execute the code again. Give it a try!
-
- 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
Thank you. need help in modifying the above code if possible. not finding the way out.
Re: read a file and search the content in another folder, copy the same to another folder
So what you are saying is give me the fish and I don't want to learn how to fish.lalat06bag wrote: ↑30 Jan 2018 12:01Thank you. need help in modifying the above code if possible. not finding the way out.
-
- 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
Hi,
I came up with this.
@echo off &setlocal
set "src=C:\Users\u595142\Desktop\AllReports"
set "dest=C:\Users\u595142\Desktop\Today_report"
set "list=C:\Users\u595142\Desktop\catalog\daily_report.txt"
:start
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*"
for /f "usebackq delims=" %%i in ("%list%") do if NOT exist "%dest%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\")
timeout -t 5
pause
goto start
Now this does well, but goes to infinite loop as tough if NOT exist "%dest%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") satisfies, it does nothing and timesout and again starting from start. So in infinite loop after all the files are copied.
So I tried to add a Boolean to come out of the loop, but that seems not working. Can you please see and suggest?
@echo off &setlocal
set "src=C:\Users\u595142\Desktop\AllReports"
set "dest=C:\Users\u595142\Desktop\Today_report"
set "list=C:\Users\u595142\Desktop\catalog\daily_report.txt"
set "condition=false"
:start
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*"
for /f "usebackq delims=" %%i in ("%list%") do if NOT exist "%dest%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") else set "condition=true"
timeout -t 5
if "%condition%" == "true" (Exit)
goto start
I came up with this.
@echo off &setlocal
set "src=C:\Users\u595142\Desktop\AllReports"
set "dest=C:\Users\u595142\Desktop\Today_report"
set "list=C:\Users\u595142\Desktop\catalog\daily_report.txt"
:start
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*"
for /f "usebackq delims=" %%i in ("%list%") do if NOT exist "%dest%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\")
timeout -t 5
pause
goto start
Now this does well, but goes to infinite loop as tough if NOT exist "%dest%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") satisfies, it does nothing and timesout and again starting from start. So in infinite loop after all the files are copied.
So I tried to add a Boolean to come out of the loop, but that seems not working. Can you please see and suggest?
@echo off &setlocal
set "src=C:\Users\u595142\Desktop\AllReports"
set "dest=C:\Users\u595142\Desktop\Today_report"
set "list=C:\Users\u595142\Desktop\catalog\daily_report.txt"
set "condition=false"
:start
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*"
for /f "usebackq delims=" %%i in ("%list%") do if NOT exist "%dest%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" "%dest%\") else set "condition=true"
timeout -t 5
if "%condition%" == "true" (Exit)
goto start