how to read info (strings) from txt file output variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

how to read info (strings) from txt file output variable?

#1 Post by goodywp » 29 Aug 2017 13:47

I have a question:
1) to read from txt file to extract some info as variable, output as either txt file or variable.bat so it can be used by the other batch files for processing
2) currently I only found from internet, we can get a entire line containing that wanted string or info. After that we need trim off unwanted part in that line and get the only string wanted as variable...
but usually the wanted string is a variable, meaning changed between versions or between components...
So how we can do the job efficiently or automatically
BTW, we can define the txt format favor to the reading string..
here is the example:
1) component: 1
file to sign: ---1111.file
folder: app\abc
signed file: 0101.file
scheme pack: T501-12345-0101
2) component: 2
file to sign: ---2222.file
folder: app\efg
signed file: 0202.file
scheme pack: T501-67891-0102
..........................
Basically what I try to do is to read each component as comp1 and comp2....
to get the value after : -->variable
for example component 1
set comp_var1=1111.file
set comp_var2=abc
set comp_var3=0101.file
set comp_var4=T501-67891-0101
Now how I can efficiently to get these info from txt file...
any thoughts? appreciated for your help...
Thanks

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

Re: how to read info (strings) from txt file output variable?

#2 Post by aGerman » 29 Aug 2017 14:53

I still don't get it. Your example shows 5 lines each for 2 components. So do we have a separate file for each component or is everything saved in only one file? Are lines like
1) component: 1
really part of the file or did you just write them in order to explain where a new component begins?
Are there any blank lines in the file?
Do always 4 values belong to a component?
What about the hyphens prepended to the file name? Are they also in the file or did you write them to preserve indentation and they are just spaces in reality?

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to read info (strings) from txt file output variable?

#3 Post by goodywp » 30 Aug 2017 07:47

Thanks Steffen for asking.

It is everything in one file. It is real file. In this file, there are some components need to be picked up if saying "schemes required for signing". Yes, it is usually 5 parts need to be picked up for that component:
the following is a example, usually
component Name: i.e., KIA
file to be signed: xxx.P3A.pkg
Location: \abc\KIA
signed file name: 1234.P3A
Scheme Pack: T501-08680-0101

Basically all the components listed in this file are same format. Only some are required to sign, like the above one.
My task is to automate this picking up all the underline parts of the component need to sign then output as a variables so I can use another script to do the replace then sign....

It is also flexible for me to design the format whichever is favor to search these strings (underline part) output them as variable, so it is open to the format, as long as easy for extracting, it shall be fine....

I really want to know if I can do accomplish this....

Thanks..

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

Re: how to read info (strings) from txt file output variable?

#4 Post by aGerman » 30 Aug 2017 12:00

I tell you the problems that I have with your explanation. You told that you usually have 5 lines for a component. If you write a code that reads 5 lines each but sometimes there is a component with only 4 lines you would be lost. Also you want to assign 5 variables. Okay so far. But since you have several component "blocks of 5 lines" in the file these variables will be overwritten several times and only the values of the last block keep being saved in the variables.
What do I miss?

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to read info (strings) from txt file output variable?

#5 Post by goodywp » 31 Aug 2017 07:55

You are right! I can avoid to have only 4 lines. every component must have 5 lines.

So my thoughts would be to have a couple of batch files to do following:

1) to pickup those blocks which has "schemes required for signing", can I do it?
2) to read the 5 lines as variable for each block (actually is component) to output as variables like data_1.txt below:
ideally in this data_1.txt only containing 5 values captured from 5 lines

so I can use it later in the following code:

Code: Select all

set [0] = data_1.txt
set [1] = data_2.txt
set [2] = data_3.txt
set [3] = data_4.txt

for /F "tokens=2 delims==" %%n in ('set [') do (

   cd C:\auto_pkg_build\Scripts\scheme_replace

   del pkg_data.txt

   cd C:\auto_pkg_build\Scripts\scheme_replace\pkg_data

   copy %%n C:\auto_pkg_build\Scripts\scheme_replace

   timeout /t 2

   cd C:\auto_pkg_build\Scripts\scheme_replace

   ren %%n pkg_data.txt   

   timeout /t 1

   call C:\auto_pkg_build\Scripts\get_variable.cmd

   timeout /t 1

   call C:\auto_pkg_build\Scripts\pre_sch_repl.cmd

   timeout /t 1

   call C:\auto_pkg_build\Scripts\unsigned_package\rename_jsat.cmd

   timeout /t 1
   
   call C:\auto_pkg_build\Tools\PACKAGER\list_comp_sch.cmd

   timeout /t 1

   call C:\auto_pkg_build\Tools\PACKAGER\md_S3S.cmd
   
   timeout /t 1

   cd C:\auto_pkg_build\Tools\PACKAGER

)
Last edited by aGerman on 31 Aug 2017 09:34, edited 1 time in total.
Reason: code tags added

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

Re: how to read info (strings) from txt file output variable?

#6 Post by aGerman » 31 Aug 2017 10:08

Okay I'll try to write an example.

data.txt

Code: Select all

component Name: KIA
file to be signed: xxx.P3A.pkg
Location: \abc\KIA
signed file name: 1111.P3A
Scheme Pack: T501-08680-0101
component Name: FOO
file to be signed:
Location: \abc\FOO
signed file name: 2222.P3A
Scheme Pack: T501-08680-0102
component Name: BAR
file to be signed: xxx.P3A.pkg
Location: \abc\BAR
signed file name: 3333.P3A
Scheme Pack: T501-08680-0103
Note that component FOO has no value next to file to be signed.

test.bat

Code: Select all

@echo off &setlocal
set "infile=data.txt"
set "blocksize=5"

for /f %%i in ('type "%infile%" ^| find /c /v ""') do set /a "cnt= %%i / blocksize"
set /a "n=0"

setlocal EnableDelayedExpansion
<"!infile!" (
  for /l %%i in (1 1 %cnt%) do (
    for /l %%j in (1 1 %blocksize%) do (
      set "line=" &set /p "line="
      set "comp_var%%j="
      for /f "tokens=1* delims=:" %%k in ("!line!") do for /f "tokens=*" %%m in ("%%l") do set "comp_var%%j=%%m"
    )
    if defined comp_var2 call :process_component
  )
)

pause
exit /b


:process_component
setlocal DisableDelayedExpansion

echo ~~~
echo Do something with
echo %comp_var1%
echo %comp_var2%
echo %comp_var3%
echo %comp_var4%
echo %comp_var5%

endlocal
exit /b
Your task is to use variables %comp_var1%...%comp_var5% in label :process_component because I don't understand what you try to do with them.

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to read info (strings) from txt file output variable?

#7 Post by goodywp » 01 Sep 2017 08:46

Thanks soooooo much Steffen :D !!!! tried it out and got the results as below:
KIA
111.P3A.pkg
\abc\KIA
1111.P3A
T501-08680-0101
BAR
333.P3A.pkg
\abc\BAR
3333.P3A
T501-08680-0103

since component FOO has no value next to file to be signed.
After I added some value it shows as expected:
KIA
111.P3A.pkg
\abc\KIA
1111.P3A
T501-08680-0101
FOO
222.P3A.pkg
\abc\FOO
2222.P3A
T501-08680-0102
BAR
333.P3A.pkg
\abc\BAR
3333.P3A
T501-08680-0103

Now come to your question: in label :process_component because I don't understand what you try to do with them.

I try to save each component 5 lines data into txt files, like data_1, data_2, data_3 in this case:
inside the data_1.txt shall show as:
KIA
111.P3A.pkg
\abc\KIA
1111.P3A
T501-08680-0101

or better formatted as
KIA, 111.P3A.pkg, \abc\KIA, 1111.P3A, T501-08680-0101

so I can use them in my code posted on 31 Aug 2017 08:55

I added output in your code as below:

Code: Select all

@echo off &setlocal
set "infile=data.txt"
set "blocksize=5"

if exist data_no.txt (del data_no.txt)

for /f %%i in ('type "%infile%" ^| find /c /v ""') do set /a "cnt= %%i / blocksize"
set /a "n=0"

setlocal EnableDelayedExpansion
<"!infile!" (
  for /l %%i in (1 1 %cnt%) do (
    for /l %%j in (1 1 %blocksize%) do (
      set "line=" &set /p "line="
      set "comp_var%%j="
      for /f "tokens=1* delims=:" %%k in ("!line!") do for /f "tokens=*" %%m in ("%%l") do set "comp_var%%j=%%m"
    )
    if defined comp_var2 call :process_component
  )
)

pause
exit /b


:process_component
setlocal DisableDelayedExpansion

echo ~~~
echo Do something with
echo %comp_var1% >>data_no.txt
echo %comp_var2% >>data_no.txt
echo %comp_var3% >>data_no.txt
echo %comp_var4% >>data_no.txt
echo %comp_var5% >>data_no.txt

endlocal
exit /b


But they are all in one file data_no.txt... Can I save them separately in data_1.txt, data_2.txt, data_3.txt ?

BTW, I forgot to let you know that each block has one blank line for separating each other...My bad, I did not answered your question at the beginning...

This is already much help to me. The only goal is to automate the replacement process from one customer profile to another....

So the file containing components info has either need to sign or no need to sign components

My task is to pick up those blocks from this big file which has "schemes required for signing".... after this step. I can come to your data.txt which listed all components blocks need to sign....

Thanks a lot!

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

Re: how to read info (strings) from txt file output variable?

#8 Post by aGerman » 01 Sep 2017 11:07

In that case you might not even need the label.

Code: Select all

@echo off &setlocal
set "infile=data.txt"
set /a "blocksize= 5, n= 0"

for /f %%i in ('type "%infile%" ^| find /c /v ""') do set /a "cnt= %%i / blocksize"

setlocal EnableDelayedExpansion
<"!infile!" (
  for /l %%i in (1 1 %cnt%) do (
    for /l %%j in (1 1 %blocksize%) do (
      set "line=" &set /p "line="
      set "comp_var%%j="
      for /f "tokens=1* delims=:" %%k in ("!line!") do for /f "tokens=*" %%m in ("%%l") do set "comp_var%%j=%%m"
    )
    if defined comp_var2 (
      set /a "n += 1"
      >"data_!n!.txt" echo !comp_var1!, !comp_var2!, !comp_var3!, !comp_var4!, !comp_var5!
    )
  )
)


My point was that processing the variables directly in the label could have been better than writing dozens of files. I don't know what you want to do with those files. If you process them with another batch file afterwards they don't make sense. Just use the code of the other batch file in the label.

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to read info (strings) from txt file output variable?

#9 Post by goodywp » 05 Sep 2017 08:08

Perfect!!! it works exactly as expected! Cheers!

I got the following as expected.
data_1
KIA, 111.P3A.pkg, \abc\KIA , 1111.P3A, T501-08680-0101

data_2
FOO, 222.P3A.pkg, \abc\FOO , 2222.P3A, T501-08680-0102

data_3
BAR, 333.P3A.pkg, \abc\BAR , 3333.P3A, T501-08680-0103

Now I also try to do as you suggested instead of saving into separate files rather to use variables directly in the label...
But that will need to modify the main function mode " replace_scheme"...
My primary attempt is to consolidate all variables into one place or file for each component....

BTW, I have one more question for you. Although I put my requirement in the question but did not put into that data.txt, :oops: there is still one issue from the beginning.
The data.txt should looks like this as below:

some instruction line...
some instruction line...
some instruction line...
.........................
KIA: (Schemes are required for signing, Refer to "Schemes to Sign_for_Tetra_829551.doc")
file to be signed: 111.P3A.pkg
Location: \abc\KIA
signed file name: 1111.P3A
Scheme Pack: T501-08680-0101

Instruction after Signing: Copy signed file back to the original folder

FOO: (Schemes are required for signing, Refer to "Schemes to Sign_for_Tetra_829552.doc")
file to be signed: 222.P3A.pkg
Location: \abc\FOO
signed file name: 2222.P3A
Scheme Pack: T501-08680-0102

Instruction after Signing: Copy signed file back to the original folder

BAR: (Schemes are required for signing, Refer to "Schemes to Sign_for_Tetra_829553.doc")
file to be signed: 333.P3A.pkg
Location: \abc\BAR
signed file name: 3333.P3A
Scheme Pack: T501-08680-0103

Instruction after Signing: Copy signed file back to the original folder

DLL: (Scheme is not required for signing)
file to be signed: 444.P3A.pkg
Location: \abc\DLL
signed file name: 4444.P3A
Instruction after Signing: Copy signed file back to the original folder

ABC: (Scheme is not required for signing)
file to be signed: 555.P3A.pkg
Location: \abc\ABC
signed file name: 5555.P3A
Instruction after Signing: Copy signed file back to the original folder;

So we need only pickup the blocks which has (Schemes are required for signing, Refer to "Schemes to Sign_for_Tetra_xxxxxxx.doc")...
the blocks which has (Scheme is not required for signing) does not have scheme pack: info as displayed as above
:oops:

Thanks

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

Re: how to read info (strings) from txt file output variable?

#10 Post by aGerman » 05 Sep 2017 13:29

I don't understand what of the above is really content of your file and what is a comment that you added.

some instruction line...
some instruction line...
some instruction line...
.........................
Does this mean there is an undefined number of lines at the beginning of the file that has to be skipped?

KIA: (Schemes are required for signing, Refer to "Schemes to Sign_for_Tetra_829551.doc")
Is the parenthesized substring something that appears in the real file or is it some kind of comment. If so, how does it look like in reality?

Instruction after Signing: Copy signed file back to the original folder

FOO: (Schemes are required for signing, Refer to "Schemes to Sign_for_Tetra_829552.doc")
f
Is the first line I quoted above a line that appears in the file? And is there really a blank line before the next block begins?

I assume it would be best to ZIP such a file and upload it in order to see the real content ...

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to read info (strings) from txt file output variable?

#11 Post by goodywp » 05 Sep 2017 13:42

Here is the txt as below: also it is attached

----------------- Read me - how to use this package ---------------
-------------------------------------------------------------------
Note:
--------------------------------------------------------------------

--------------------------------------------------------------------
File(s) to be added to the package:
--------------------------------------------------------------------
- PROFILE:
Copy your Public Key file to Profile folder: \TELIUM3\PROFILE

--------------------------------------------------------------------
Following applications/components/data files need to be signed:
--------------------------------------------------------------------


- E2EE: (Schemes are required for signing, Refer to "Schemes to Sign_for_Tetra_829552.doc")
File to be signed : 8295522003_E2EE.P3A.pkg (Prm file to be used: 8295522003_E2EE.P3A.jsat);
Location : \TELIUM3\E2EE_DLL ;
Signed file name : 8295522003_E2EE.P3A;
E2EE scheme pack : T501-08680-0102;
Instruction after Signing: Copy signed file back to the original folder;


- KIA: (Schemes are required for signing, Refer to "Schemes to Sign_for_829500_DES_v1.xx.doc")
File to be signed : 8295000512_KIA.P3A.pkg (Prm file to be used: 8295000512_KIA.P3A.jsat);
Location : \TELIUM3\KIA ;
Signed file name : 8295000512_KIA.P3A;
KIA scheme pack : T501-08680-0101, 0501-08690-0100, T501-08699-0100;
Instruction after Signing: Copy signed file back to the original folder;

- TeliumGeneric application: (Schemes are required for signing, Refer to "Schemes to Sign_for_829529.doc")
File to be signed : 8295291003_CoreApp.P3A.pkg (Prm file to be used: 8295291003_CoreApp.P3A.jsat);
Location : \TELIUM3\TeliumGeneric;
Signed file name : 8295291003_CoreApp.P3A;
Gen scheme pack : T501-08680-0102, T501-08699-0100;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under

- TSA: (Schemes are required for signing, Refer to "Schemes to Sign_for_829501_DES_v1.xx.doc")
File to be signed : 8295010512_TSA.P3A.pkg (Prm file to be used: 8295010512_TSA.P3A.jsat);
Location : \TELIUM3\TSA;
Signed file name : 8295010512_TSA.P3A;
TSA scheme pack : T501-08680-0101, T501-08699-0100;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under



- Print Engine DLL: (Scheme is not required for signing)
File to be signed : 8295440210.BIN (Prm file to be used: 82954400210_T2.TXT);
Location : \TELIUM\Print Engine DLL;
Signed file name : 82954400210 .LGN;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under


- SQLite DLL: (Scheme is not required for signing)
File to be signed : 8295420200.BIN (Prm file to be used: 8295420200-SAT.txt);
Location : \TELIUM\SQLite DLL;
Signed file name : 8295420200.LGN;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under


- SWAP: (Scheme is not required for signing)
------------------------------------------------------------------------------------
* Sign the Receipt Form:
File to be signed : RCPTS_PTIC.XML (Prm file to be used: RCPTS_PTIC-SAT.txt);
Location : \TELIUM\SWAP;
Signed file name : RCPTS_PTIC.PGN;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under


* Sign the Report Form:
File to be signed : REPTS_PTIC.XML (Prm file to be used: REPTS_PTIC-SAT.txt);
Location : \TELIUM\SWAP;
Signed file name : REPTS_PTIC.PGN;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under



* Global SSL Certificates:
File to be signed : VER_CLS3.PEM (Prm file to be used: 829534VER_CLS3-SAT.txt);
Location : \TELIUM\SWAP;
Signed file name : 829534VCLS3.PGN;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under



* Sign the Dialog form:
File to be signed : DIALOG.XML (Prm file to be used: DLG_829529-SAT.txt);
Location : \TELIUM\SWAP;
Signed file name : DLG_829529.PGN;
Instruction after Signing: Copy signed file back to the original folder;
Add the signed file into "VeriCentre" Folder under


------------------------------------------------------------------------------------

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

Re: how to read info (strings) from txt file output variable?

#12 Post by aGerman » 05 Sep 2017 15:12

also it is attached
Where?

You know that these are completely different preconditions that require a complete new script? I really hope that there is nothing more that you withhold to tell. If the last content you posted was not how it really looks like then I'm outta here.

Code: Select all

@echo off &setlocal
set "infile=data.txt"
set /a "blocksize= 5"

setlocal EnableDelayedExpansion
set /a "before= 1"
<"!infile!" (
  for /f "delims=:" %%i in ('findstr /nrc:"- ..*: (Schemes are required for signing, Refer to" "!infile!"') do (
    set /a "end= %%i - before"
    for /l %%j in (1 1 !end!) do set /p "="
    for /l %%j in (1 1 !blocksize!) do (
      set "line=" &set /p "line="
      set "comp_var%%j="
      for /f "tokens=1* delims=:" %%k in ("!line!") do for /f "tokens=*" %%m in ("%%l") do set "comp_var%%j=%%m"
    )
    call :process_component
    set /a "before= %%i + blocksize"
  )
)
pause
exit /b

:process_component
setlocal DisableDelayedExpansion

echo ~~~
echo Do something with
echo %comp_var1%
echo %comp_var2%
echo %comp_var3%
echo %comp_var4%
echo %comp_var5%

endlocal
exit /b

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to read info (strings) from txt file output variable?

#13 Post by goodywp » 06 Sep 2017 08:18

Thanks again Steffen!!!
Actually after I try to send the attachment again, just realized it is not allowed saying " The extension txt is not allowed" That means yesterday I did not send it successfully. Tried different extension and lastly rename it without extension just got the same complain "The extension is not allowed"

Anyway, the last post is just a copy of an exactly same txt file.

Yes the zipped file is working now

I tried to save the output as separate files as your second code did based upon your third code. So I modified to your code as below:

Code: Select all

@echo off &setlocal
set "infile=Read me - how to use thia package.txt"
set /a "blocksize= 5"

setlocal EnableDelayedExpansion
set /a "before= 1"
<"!infile!" (
  for /f "delims=:" %%i in ('findstr /nrc:"- ..*: (Schemes are required for signing, Refer to" "!infile!"') do (
    set /a "end= %%i - before"
    for /l %%j in (1 1 !end!) do set /p "="
    for /l %%j in (1 1 !blocksize!) do (
      set "line=" &set /p "line="
      set "comp_var%%j="
      for /f "tokens=1* delims=:" %%k in ("!line!") do for /f "tokens=*" %%m in ("%%l") do set "comp_var%%j=%%m"
    )
    if defined comp_var2 (
    set /a "before= %%i + blocksize"
   >"data_!n!.txt" echo !comp_var1!, !comp_var2!, !comp_var3!, !comp_var4!, !comp_var5!
   )
  )
)


After tried the above code only got the last component in the output file which is TSA. And also the output file is data_ instead of data_1? what is wrong here?

I just need another option
Attachments
Readme-how-to-use-this-package.zip
(1.08 KiB) Downloaded 359 times
Last edited by goodywp on 06 Sep 2017 08:44, edited 1 time in total.

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

Re: how to read info (strings) from txt file output variable?

#14 Post by aGerman » 06 Sep 2017 08:28

aGerman wrote:ZIP such a file and upload it

Don't you know what a ZIP folder is?
right-click on the file -> Send to -> Compressed (zipped) folder.

Steffen

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

Re: how to read info (strings) from txt file output variable?

#15 Post by aGerman » 06 Sep 2017 09:44

goodywp wrote:Anyway, the last post is just a copy of an exactly same txt file.

No it's not. There are spaces and tabs that don't show up in the post. Fortunately the script does yet work.

Code: Select all

@echo off &setlocal
set "infile=Readme-how-to-use-this-package.txt"
set /a "blocksize= 5, n= 0"

setlocal EnableDelayedExpansion
set /a "before= 1"
<"!infile!" (
  for /f "delims=:" %%i in ('findstr /nrc:"- ..*: (Schemes are required for signing, Refer to" "!infile!"') do (
    set /a "end= %%i - before"
    for /l %%j in (1 1 !end!) do set /p "="
    for /l %%j in (1 1 !blocksize!) do (
      set "line=" &set /p "line="
      set "comp_var%%j="
      for /f "tokens=1* delims=:" %%k in ("!line!") do for /f "tokens=*" %%m in ("%%l") do set "comp_var%%j=%%m"
    )
    if defined comp_var2 (
      set /a "n += 1"
      >"data_!n!.txt" echo !comp_var1!, !comp_var2!, !comp_var3!, !comp_var4!, !comp_var5!
    )
    set /a "before= %%i + blocksize"
  )
)
pause
exit /b


Steffen

Post Reply