VFile creator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

VFile creator

#1 Post by drgt » 18 Jan 2022 05:24

I need to create a text file of outgoing SMS using patern:

Code: Select all

BEGIN:VMESSAGE
TYP:SMS;OUT
BOX:INBOX
READ:0
DATE:To be imported as is
NUMBER:To be imported as is
BODY;CHARSET=1253:To be imported without the quotes EDIT: No need. Removed the quotes.
END:VMESSAGE
That will import from other text (CSV) file:

Code: Select all

NUM;TIME;BODY
6912345678;20211102T113146U;"This is Body: Test.
For line feed there is a little square, I do not know the character name. Pasting it here creates line feed."

Feasible or there are better alternatives?
Last edited by drgt on 19 Jan 2022 04:15, edited 1 time in total.

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

Re: VFile creator

#2 Post by Squashman » 18 Jan 2022 08:05

The input and output look pretty straightforward. What is stopping your from reading the input with a FOR /F command and echoing the output as needed. The square box is an end of file character also known as a SUB. HEX 1A. We have had several discussion on the forum about how to output that character.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: VFile creator

#3 Post by drgt » 18 Jan 2022 16:32

I used

Code: Select all

@echo off
>"t1.log" (for /f "usebackq tokens=1-3 delims=;" %%i in ("test.txt") do (
echo %%i
echo %%j
echo %%k))
however I am getting
echo is off
echo is off
when the SUB is encountered.

I need the exact character to be returned

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

Re: VFile creator

#4 Post by Squashman » 18 Jan 2022 17:40

Could you zip up the text file and upload it here.

Or run this code so we can see the hex of those two lines. This does a binary compare of the input file with a dummy file.
Input file should be named test.txt

Code: Select all

@echo off
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set /p "BS=%%a"<nul >dummy.txt
for /l %%n in (1 1 15) do type dummy.txt >>dummy.txt
fc /b test.txt dummy.txt
del dummy.txt
Copy the all the output from the compare into the forum.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

VFile creator

#5 Post by drgt » 19 Jan 2022 01:07

Sure, here it is...

The problem lies in that the "Body" variable may contain multiple lines (SUB in this case, Carriage Returns in other cases)
It seems the variable reads only the part up to the first sub and passes the rest into the next loop, leaving the rest of the fields empty for that record.
Attachments
test.rar
(570 Bytes) Downloaded 540 times

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

Re: VFile creator

#6 Post by Squashman » 19 Jan 2022 12:19

I don't see any SUB (hex 1A) or Carriage Returns (hex 0D) in your file.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: VFile creator

#7 Post by drgt » 19 Jan 2022 12:55

I am enclosing files and pictures in the attached archive.
Attachments
VFile.rar
(697.58 KiB) Downloaded 542 times

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

Re: VFile creator

#8 Post by aGerman » 19 Jan 2022 13:53

OK, so there isn't any SUB. However I clearly see what's going on. Data sets are separated by CR LF, while new lines in the message are represented by LF only.
Right now I don't have any good idea of how to solve this issue using pure Batch. Most of the IO operations don't make a big difference between CR LF and LF.

Steffen
Screenshot 2022-01-19 205219.png
Screenshot 2022-01-19 205219.png (89.77 KiB) Viewed 16059 times

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

VFile creator

#9 Post by drgt » 19 Jan 2022 14:06

Thank you very much.
I am sure you will find a way!
Is there anything I could be modifying in the test.txt file so that does not happen?
I do not mean just omit the LF's as the result (all in one continuous paragraph) won't be appealing.

Lastly, do you know of any software that will be able to do this?

Thanks again
George

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: VFile creator

#10 Post by AR Coding » 19 Jan 2022 21:03

Would this topic be helpful?
viewtopic.php?t=3639

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: VFile creator

#11 Post by drgt » 20 Jan 2022 00:06

Thanks AR Coding.
I will leave that to experts in here as it is over my head.

I cannot find a utility that will do this either.
I thought maybe in word, but only mailmerge exists there. :(

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

Re: VFile creator

#12 Post by aGerman » 20 Jan 2022 11:55

This is a pretty simple task in almost every language. Just not in Batch.
A few lines of VBScript (even if off-topic in this forum):

*.vbs

Code: Select all

Option Explicit
Const inFile = "Test.txt"
Const outFile = "T1.txt"

Dim oFSO, oInFile, oOutFile, arrDataSets, dataSet, arrData
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oInFile = oFSO.OpenTextFile(inFile)
arrDataSets = Split(oInFile.ReadAll, vbCrLf)
oInFile.Close

Set oOutFile = oFSO.OpenTextFile(outFile, 2, True)
For Each dataSet In arrDataSets
  arrData = Split(dataSet, ";", 3)
  oOutFile.WriteLine "BEGIN:VMESSAGE"
  oOutFile.WriteLine "TYP:SMS;OUT"
  oOutFile.WriteLine "BOX:INBOX"
  oOutFile.WriteLine "READ:0"
  oOutFile.WriteLine "DATE:" & arrData(1)
  oOutFile.WriteLine "NUMBER:" & arrData(0)
  oOutFile.WriteLine "BODY;CHARSET=1253:" & arrData(2)
  oOutFile.WriteLine "END:VMESSAGE"
Next
oOutFile.Close
Steffen

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: VFile creator

#13 Post by drgt » 20 Jan 2022 13:01

WOW!!!
THANK YOU VERYYYY MUCH!!!

Although I know nothing about VBS, I took the liberty of modifying it for 6 fields

Code: Select all

Option Explicit
Const inFile = "Test.txt"
Const outFile = "T1.txt"

Dim oFSO, oInFile, oOutFile, arrDataSets, dataSet, arrData
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oInFile = oFSO.OpenTextFile(inFile)
arrDataSets = Split(oInFile.ReadAll, vbCrLf)
oInFile.Close

Set oOutFile = oFSO.OpenTextFile(outFile, 2, True)
For Each dataSet In arrDataSets
  arrData = Split(dataSet, ";", 6)
  oOutFile.WriteLine "BEGIN:VMESSAGE"
  oOutFile.WriteLine "TYP:SMS;" & arrData(0)
  oOutFile.WriteLine "BOX:" & arrData(1)
  oOutFile.WriteLine "READ:" & arrData(2)
  oOutFile.WriteLine "DATE:" & arrData(3)
  oOutFile.WriteLine "NUMBER:" & arrData(4)
  oOutFile.WriteLine "BODY;CHARSET=1253:" & arrData(5)
  oOutFile.WriteLine "END:VMESSAGE"
Next
oOutFile.Close
on this input file

Code: Select all

IN;INBOX;0;20210101T000000U;12345;MESSAGE1
OUT;OUTBOX;1;20220101T000100U;67890;MESSAGE2
;;;;;MESSAGE3
I hope this post comes up for those looking for a CSV to VCF converter as well

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: VFile creator

#14 Post by Aacini » 20 Jan 2022 22:12

The following Batch file identifies if the lines of a file ends in CR+LF or just LF. It uses the /O ("offset") switch of FINDSTR command that gives the starting position of the lines in the file:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Get line lenghts of all lines assuming they ends in CR+LF
rem so lines that ends in just LF will be *longer* by 1 char

set /A "lastOffset=0, i=0"
for /F "delims=:" %%a in ('findstr /O "^" test.txt') do (
   set /A "len[!i!]=%%a-lastOffset-2, lastOffset=%%a, i+=1"
)
for %%a in (test.txt) do set /A "len[%i%]=%%~Za-lastOffset-1"


rem Process and identify each line

set "i=0"
for /F "delims=" %%a in (test.txt) do (
   set "line=%%a"
   set /A i+=1
   set /A "len=len[!i!]"
   for %%i in (!len!) do (
      if "!line:~%%i!" equ "" (
         echo CR+LF: !line!
      ) else (
         echo    LF: !line!
      )
   )
)
The Batch file below uses such a method to solve this problem:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set /A "lastOffset=0, i=0"
for /F "delims=:" %%a in ('findstr /O "^" test.txt') do (
   set /A "len[!i!]=%%a-lastOffset-2, lastOffset=%%a, i+=1"
)
for %%a in (test.txt) do set /A "len[%i%]=%%~Za-lastOffset-1"

set "i=0"
set "inBody="
>"T1.txt" (for /F "delims=" %%a in (test.txt) do (

   rem Output the appropriate data
   if not defined inBody (
      for /F "tokens=1-3 delims=;" %%i in ("%%a") do (
         echo BEGIN:VMESSAGE
         echo TYP:SMS;IN
         echo BOX:INBOX
         echo READ:0
         echo DATE:%%j
         echo NUMBER:%%i
         echo BODY;CHARSET=1253:%%k
      )
   ) else (
      echo %%a
   )

   rem Identify if this data belongs to Body
   set "line=%%a"
   set /A i+=1
   set /A "len=len[!i!]"
   for %%i in (!len!) do (
      if "!line:~%%i!" equ "" (
         set "inBody="
         echo END:VMESSAGE
      ) else (
         set "inBody=1"
      )
   )
))
Output:

Code: Select all

BEGIN:VMESSAGE
TYP:SMS;IN
BOX:INBOX
READ:0
DATE:20211102T113146U
NUMBER:6012589631
BODY;CHARSET=1253:Πείτε μου αν λάβατε όλο το μήνυμα. Τελευταία λεξη: τσεκάρισμα.
ΟΝΟΜΑ
END:VMESSAGE
BEGIN:VMESSAGE
TYP:SMS;IN
BOX:INBOX
READ:0
DATE:20211102T104259U
NUMBER:6256945389
BODY;CHARSET=1253:KAΛHMEPA. XΘEΣ HPΘE TO ΠAIΔI KAI MAΣ EΔEIXNE TO ΠΟΔΙ TOY. EIXAN BΓEI TA ΠΑΠΟΥΤΣΙΑ AΠO MΠPOΣTA AΛΛA KPATIOTAN AKOMH AΠO ΠIΣΩ.
EΠIΠΛEON:
1.NOMIZΩ ΠΩΣ MΠAINOYN APKETA EYKOΛA XΩPIΣ NA AΣKOYN IΔIAITEPH ΠIEΣH ΣTA MΠPOΣTINA ΔΑΚΤΥΛΑ.
2.NOMIZΩ TO MΠPOΣTINO ΣYPMATINO TOΞO KAΘETAI ΠOΛY ΨHΛA, KONTA ΣTA ΠΡΑΓΜΑΤΑ.
IΣΩΣ OΛO AYTO NA EINAI AΠOTEΛEΣMA TOY OTI ΠPOΣTEΘHKAN ATOMA TOY ΓΥΡOY KAI THΣ ΘEPAΠEIAΣ ΠOY BAZOYN - BΓAZOYN KAI IΣΩΣ NA EXOYN AΠOPYΘMIΣTEI.
NA ΔOYME AN ΞANABΓOYN AΠO MONA TOYΣ, H NA EPΘOYME ΓIA TΣEKAPIΣMA?
END:VMESSAGE
BEGIN:VMESSAGE
TYP:SMS;IN
BOX:INBOX
READ:0
DATE:20220118T090000U
NUMBER:1234567890
BODY;CHARSET=1253:TEST
END:VMESSAGE
Antonio

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

Re: VFile creator

#15 Post by aGerman » 21 Jan 2022 03:29

Antonio,
I guess this is going to turn the LFs in the BODY into CR LFs, right?

Steffen

Post Reply