#1 20 Aug 2019 17:56

Yazidioubi
Member
Registered: 20 Aug 2019
Posts: 4

merge multiple text files with a new header

Assuming we have this text files

*0000000000003000345800091600000000000002082019              0
*000000000000322322930002160000000DOU JIJEL                  1
*000000000000306156240007000000000TIC TAHER                  1
The header contains always what follows :

From position 1 to position 21 we have always this:

*00000000000030003458 which is an «unchangeable» value. It contains 21 characters.

From position 22 to 34, we got 13 characters which represent the sum of the amounts contained in every line the text file from position 22 to 34.

To clarify ; if you look at the header you’ll see from position 22 to 34 :

0009160000000 which is 91 600 000,00 It’s an amount of money, which is the sum of the amounts in the first and second line.

First line : 0002160000000 which is 21 600 000,00 Second line : 0007000000000 which is 70 000 000,00

21 600 000,00+70 000 000,00=91 600 000,00
« If we have in the first line 3162160000000 it means the amount in 31 621 600 000,00 If we have in the first line 0000000541000 it means the amount is 5 410,00 »

From position 35 to 41, we have seven characters, which represent the number of amounts contained in the text file. We have From position 35 to 41 0000002, and we have two lines except the header, so the sum is 2. If for example we have 714 lines, the position 35 to 41 in the header will be 0000714, and so on.

So, if I have two text files, and I want to merge them together in one file, in a way that we’ll have: Only one header and All the lines in the text files. The lines of course will be unchanged. But the header will be changed as I explained above, in addition to that and from position 42 to position 62, will always be of the values or the characters contained in the header of the text files I want to merge, which are always the same. That means that the header will be changed only from position 22 to position 41.

I've managed to remove the headers, but the new header I write it manually

@echo off   
setlocal enabledelayedexpansion   
if exist output.txt del output.txt   
set "var="   
for /r %%i in (*.txt) do (   
  if "%%~nxi" NEQ "output.txt" (   
  set "var="   
  for /f "usebackq skip=1 delims=" %%b in ("%%~i") do (   
    set var=%%b   
    if "!var!" NEQ "" Echo !var!   
))) >> output.txt
this code will remove the header of the text files

So I expect the new header to be calculated automatically

Offline

#2 21 Aug 2019 06:19

Aacini
Member
Registered: 05 Dec 2012
Posts: 148

Re: merge multiple text files with a new header

You asked the exact same question at this Stack Overflow post and I posted a complete answer there. Isn't such an answer working correctly? If so, post full details below such an answer.

It is a bad netiquette practice to ignore the answer that somebody prepared to you for free and simply post the same question again in another forum; this is called cross-posting...  sad

Antonio

Offline

#3 21 Aug 2019 06:39

Yazidioubi
Member
Registered: 20 Aug 2019
Posts: 4

Re: merge multiple text files with a new header

Antonio
It's not not what you think
Thank you very much for your help
I really appreciate it
The problem is that( in the bat fife you provided) the sum of the amounts contained in the lines of diffrent text files is being calculated from the sum of the headers of each fle.
I want the headers to be removed; and  then bat file calculate the total amount of the sums contained in the lines  and put it in the new header.
Thanks again antonio for your reply.
( in case the total amount of the lines of a text file in its header is wrong; your bat file will create a wrong header)
I really appreciated you help. smile

Offline

#4 21 Aug 2019 18:13

Aacini
Member
Registered: 05 Dec 2012
Posts: 148

Re: merge multiple text files with a new header

Neither in your original S.O. post, nor in this new one, you indicated: "I want... the bat file calculate the total amount of the sums contained in the lines", nor the possibility that "the total amount of the lines of a text file in its header is wrong".

Your question description is very, very clear:

Yazidioubi wrote:

The header contains... From position 22 to 34, we got 13 characters which represent the sum of the amounts contained in every line the text file from position 22 to 34.

To clarify ; if you look at the header you’ll see from position 22 to 34 :

0009160000000 which is 91 600 000,00 It’s an amount of money, which is the sum of the amounts in the first and second line.

First line : 0002160000000 which is 21 600 000,00 Second line : 0007000000000 which is 70 000 000,00

21 600 000,00+70 000 000,00=91 600 000,00

If someone posted a solution to your question you should acknowledge the answerer that you read such an answer and appreciate it; this is the most basic netiquette rule in this type of forums, where people write and post solutions for free... Anyway, IMHO it is much simpler to just fix this detail in an already working code instead of ignore it and post a complete new question in a different forum (unless you don't care the solution posted in the former forum)...

Antonio

Offline

#5 21 Aug 2019 18:16

Yazidioubi
Member
Registered: 20 Aug 2019
Posts: 4

Re: merge multiple text files with a new header

Antonio
My bad
You're right

Offline

Board footer

Powered by FluxBB