Hello, I try to solve in many way this problem without success, maybe you have the solution
my goal is merge file a.txt and b.txt into new one, read first file and if first item is present in second file update it in c.txt file, if not just copy row from a.txt to c.txt
first file
a.txt
AAA;2020-09-01;Y
BBB;2020-09-01;Y
CCC;2020-09-01;Y
second file
b.txt
AAA;2020-09-01;Y;21/08/2020
BBB;2020-09-01;Y
results:
c.txt
AAA;2020-09-01;Y;21/08/2020
BBB;2020-09-01;Y
CCC;2020-09-01;Y
Thank you in advance
Merge two file in a new one
Moderator: DosItHelp
Re: Merge two file in a new one
Code: Select all
>"c.txt" (
for /f "usebackq delims=" %%i in ("a.txt") do (
set "line="
for /f "delims=" %%j in ('findstr /b "%%i" "b.txt"') do set "line=%%j"
if defined line (
setlocal EnableDelayedExpansion
echo(!line!
endlocal
) else echo(%%i
)
)
Steffen
Re: Merge two file in a new one
Many thank works fine, I had another problem, I use this command "findstr /livg:" for merge two file in a new one in both direction, but command FAIL if a second file is empty, how can I solve this problem?
a.txt
1st_file_1st_row
2nd_file_2st_row
1st_file_3st_row
1st_file_4st_row
1st_file_4st_row
1st_file_5st_row
b.txt
1st_file_1st_row
1st_file_2st_row
1st_file_3st_row
1st_file_4st_row
2st_file_4st_row
1st_file_5st_row
ab.txt
1st_file_2st_row
2st_file_4st_row
ba.txt
2nd_file_2st_row
if b.txt is empty I got a ab.txt empty, but I want all line from a.txt (same problem in reverse condition)
thank you in advance
a.txt
1st_file_1st_row
2nd_file_2st_row
1st_file_3st_row
1st_file_4st_row
1st_file_4st_row
1st_file_5st_row
b.txt
1st_file_1st_row
1st_file_2st_row
1st_file_3st_row
1st_file_4st_row
2st_file_4st_row
1st_file_5st_row
Code: Select all
findstr /livg:a.txt b.txt > ab.txt
1st_file_2st_row
2st_file_4st_row
Code: Select all
findstr /livg:b.txt a.txt > ba.txt
2nd_file_2st_row
if b.txt is empty I got a ab.txt empty, but I want all line from a.txt (same problem in reverse condition)
thank you in advance
Re: Merge two file in a new one
I think you can just determine the file size (modifier ~z of a FOR variable). If it is zero then copy the other file.
Steffen
Steffen
Re: Merge two file in a new one
I thought about it, if empty just copy
Thanks
Dario
Thanks
Dario