VBS to solve the problems related to processing TXT text data

Time:2022-5-27

There is a small problem, as follows
Existing text file 1 Txt, as follows:
Numerical value_ Frequency of occurrence 12647
Numerical value_ 2 occurrence frequency 10000
Numerical value_ 3 occurrence frequency 12608
Numerical value_ 4 occurrence frequency 8712
Numerical value_ 5 occurrence frequency 10658
Numerical value_ 6 frequency of occurrence 8472
Numerical value_ 7 occurrence frequency 11232
Numerical value_ 8 occurrence frequency 8648
Numerical value_ 9 occurrence frequency 9264
Numerical value_ 10 frequency of occurrence 7192
Numerical value_ 11 occurrence frequency 7192
。。。。
About 100 lines
It is required to put the value of each line in the variable, and then output it as a text file 2 txt
For example: put 12674 in the first line into variable A1
Put 10000 in the second line into variable A2
Put 12608 in the third line into variable A2
…. Until the last line
Finally, the contents of the “2. TXT” text file are:
a1 = 12647
a2 = 10000
a3 = 12608
a4 = 8712
….
a11 = 7192
Hope to find the relevant code, and it can run under windows!! Look, look, look..
The implementation code is as follows
VB code:

Copy codeThe code is as follows:


set fso = createobject(“scripting.filesystemobject”)
set file=fso.opentextfile(“1.txt”)
ts = file.readall
file.close
set fil = fso.createtextfile(“2.txt”)
TS = replace (TS), “value”, “a”)
TS = replace (TS, “frequency of occurrence”, “=”)
” ‘if there are horizontal lines and blank lines, add this, and if not, comment it out
ts=replace(ts,”———————–“+vbnewline+vbnewline,””)
fil.write ts
fil.close
The code above msgbox “processing completed” is 1 Txt directly changed to 2 Txt, intermediate variables A1 ~ A100 are omitted. If intermediate variables are needed for other purposes, you can read 2 Txt and assign a value. The code is as follows:
VBScript code:
set fso = createobject(“scripting.filesystemobject”)
set ts = fso.opentextfile(“2.txt”)
i=0
do while ts.AtEndOfStream=false
str = ts.ReadLine
Execute STR ‘execute assignment
i=i+1
Execute (“value = a” & I) ‘get the value of variable A1
Response. Write (“a” & I & “value is:” & Value & “< br / >”) ‘output
Loop has another method, as shown in the following code:
VBScript code:
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set txt1 = fs.OpenTextFile(“1.txt”, 1)
Set txt2 = fs.CreateTextFile(“C:\FSO\ScriptLog.txt”)
Do Until txt1.AtEndOfStream
str_a = txt1.ReadLine
str_ A = replace (str_a, “degree”, “$”)
str_ar = split(str_a, “$”)
if isnumeric(str_ar(ubound(str_a))) then
txt2.writeline str_ar(ubound(str_a))
end if
Loop
txt1.close
txt2.close
set txt1 = nothing
set txt2 = nothing
set fs = nothing