China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-29 21:06
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Convert from original text file to another format text file View 3,811 Replies 40
Floor 31 Posted 2008-02-01 03:26 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
PS: The speed of vbs should be quite fast... I don't have specific data, you can test it yourself.
Floor 32 Posted 2008-02-01 11:30 ·  中国 福建 福州 联通
初级用户
★★
Credits 127
Posts 59
Joined 2008-01-29 01:05
18-year member
UID 109744
Status Offline
Thank you very much for the help from user slore. After using the code from you, the following small issues occur:

Please help take a look again:
1. OutStr = OutStr & "14080005020214080100112003624143" & AddZero(i,6)
OutStr = OutStr & "00000000001775000000000000000000072 " 'Is there a missing 2 above? The meaning of 2 is unclear.
I'm really sorry that I missed writing a 1. 2 represents the detailed line, and 1 represents the summary line. It should be as follows:
OutStr = OutStr & "114080005020214080100112003624143" & AddZero(i,6)
OutStr = OutStr & "00000000001775000000000000000000072 "
2. In addition, the 17750 in the last summary line of my file is not fixed but variable. The rules are as follows:
The last line in file b is the summary line. In this example, there are only three places that change. One is the sequence number: 8, one is the summary data: 17750, and one is the summary number: 7. That is to say, the preceding 114080005020214080100112003624143 is fixed. The following 000008 is variable and is filled in sequentially from the serial number of the previous line (for example: if the previous line is 000011, this place in the next line is 000012; if the previous line is 000111, this place becomes 000112). Then the following 00 is fixed. And the following 00000000177500 is variable. 00000000177500 is the summary number of the corresponding positions of all previous lines (for example: the data of the corresponding position of line 1 is 000000000270200 + the data of the corresponding position of line 1 is 000000000250000 +.... + the data of the corresponding position of line 7 is 000000000250000 = 00000000177500). Then the following 000000000000000007 is variable, which is used to represent the total number of previous lines (for example: if there are 100 lines before the last line, this position data should be 000000000000000100). The final 2 (including the blank) are all fixed.
3. In each line generated by your code, the blank bytes after the name are gone. The rule for "name + subsequent blank bytes" is as follows:
What is needed is the result that "name + subsequent blank" is all selected, and then copy and paste it into the cell of Excel. Use the function len to verify. If the name is two characters, the return value of len for "name + subsequent blank" is 51; if the name is three characters, the return value of len for "name + subsequent blank" is 50; if the name is four characters, the return value of len for "name + subsequent blank" is 49.
For the last summary line, the number of blank bytes needed after the last data 2 is represented by the return value of the len function in Excel as 82 bytes.
After all the above details are processed, the generated file, when all selected with Ctrl+A, the blue screen is filled for each line without any blank, and the number of bytes in each line should be equal in the text file.
Please continue to improve this code for me!
Thank you very much!
Floor 33 Posted 2008-02-01 12:31 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Can't understand VBA? Can't modify it appropriately by yourself... You get up really early...

Example2+.vbs:

Const ForReading = 1 'Set the text opening mode to read
Const ForWriting = 2 'Set the text opening mode to write
Const ForAppending = 8 'Set the text opening mode to append

Const InFile = "a.txt" 'Set the text to open

Dim i,Sum
Dim StrLine,OutStr
i = 1:Sum = 0

'On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject") 'Reference the FSO object, which is needed for reading and writing files
Set objFile = objFSO.OpenTextFile(InFile, ForReading) 'Open InFile in read mode

Do Until objFile.AtEndOfStream
StrLine = objFile.ReadLine 'Read a line
If Len(StrLine) Then 'If it's not an empty line
StrLine = FixStr(StrLine)
NeedData = Split(StrLine," ")
If Right(NeedData(1),1) = "*" Then NeedData(1) = Left(NeedData(1),Len(NeedData(1)) - 1)
NeedData(4) = NeedData(4) & Space(53 - Len(NeedData(4)))
OutLine = "2140800050202" & NeedData(1) & "3" & AddZero(i,6) & "00" & NeedData(2)
OutLine = OutLine & "000000000000000011 " & AddZero(i - 1,9) & NeedData(4)
OutStr = OutStr & OutLine & vbCrLf
i = i + 1
Sum= Sum+ CLng(NeedData(2))
End If
Loop
OutStr = OutStr & "14080005020214080100112003624143" & AddZero(i,6)
OutStr = OutStr & "00" & AddZero(Sum,15) & "0000000" & AddZero(i - 1,9) & "2" & Space(82)


objFile.Close 'Close the file object


OutFile = "140820" & FormatDate(Date) & "001.txt"
Set objFile = objFSO.OpenTextFile(OutFile, ForWriting,True) 'Open OutFile in write mode
objFile.Write OutStr
objFile.Close 'Close the file object

Set objFile = Nothing
Set
objFSO = Nothing

Function
FixStr(StrExp)
Do While InStr(StrExp," ")
StrExp = Replace(StrExp," "," ")
Loop
FixStr = StrExp
End Function

Function
FormatDate(tDate)
FormatDate = Right(Year(tDate),2) & AddZero(Month(tDate),2) & AddZero(Day(tDate),2)
End Function

Function
AddZero(sNum,iLen)
Addzero = Right("000000000" & sNum,iLen)
End Function
Floor 34 Posted 2008-02-01 12:54 ·  中国 福建 福州 联通
初级用户
★★
Credits 127
Posts 59
Joined 2008-01-29 01:05
18-year member
UID 109744
Status Offline
Sorry, big brother, my VBA is also very amateurish. I just started learning. I'm still in the process of studying. I can't say I can do it. I also came into this halfway. I just started learning for three months. At first, it was all about how to operate in Excel. So far, I've only learned about text import and export knowledge. I haven't tried it yet. After hearing your lesson, I'm very apprehensive. I'll reply to you first.

Studying hard. In the future, I still need big brother to continue to help!

Testing...
감사합니다!
Floor 35 Posted 2008-02-01 18:22 ·  中国 福建 福州 联通
初级用户
★★
Credits 127
Posts 59
Joined 2008-01-29 01:05
18-year member
UID 109744
Status Offline
Report to Great Slore, I used the great's code. Although some small problems occurred, with the great's guidance, I have learned a lot. These small problems are determined to be overcome by myself. Otherwise, how can I make progress... After working hard for a long time, I finally understood the main reason. The return value of the Len function when applied in Excel 2000 is 1 for one Chinese character, while one Chinese character occupies two bytes in a text file. Although the result may be scorned by the greats, after all, it is my own summary. I also explain to the great that I am actively studying, not just a lazy person who only wants code. At the same time, I have learned many essences while studying the great's code... Now I upload the slightly modified code of myself:

Const ForReading = 1 'Set the way to open the text as reading
Const ForWriting = 2 'Set the way to open the text as writing
Const ForAppending = 8 'Set the way to open the text as appending

Const InFile = "a.txt" 'Set the text to be opened

Dim i, Sum
Dim StrLine, OutStr
i = 1: Sum = 0

'On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject") 'Reference the FSO object, which is needed for reading and writing files
Set objFile = objFSO.OpenTextFile(InFile, ForReading) 'Open InFile in reading mode

Do Until objFile.AtEndOfStream
StrLine = objFile.ReadLine 'Read a line
If Len(StrLine) Then 'If it is not an empty line
StrLine = FixStr(StrLine)
NeedData = Split(StrLine, " ")
If Right(NeedData(1), 1) = "*" Then NeedData(1) = Left(NeedData(1), Len(NeedData(1)) - 1)
NeedData(4) = NeedData(4) & Space(53 - Len(NeedData(4)) * 2)
OutLine = "2140800050202" & mid((NeedData(1) & space(10)), 1, 19) & "3" & AddZero(i, 6) & "00" & NeedData(2)
OutLine = OutLine & "000000000000000011 " & AddZero(i - 1, 9) & NeedData(4)
OutStr = OutStr & OutLine & vbCrLf
i = i + 1
Sum = Sum + CLng(NeedData(2))
End If
Loop
OutStr = OutStr & "114080005020214080100112003624143" & AddZero(i, 6)
OutStr = OutStr & "00" & AddZero(Sum, 15) & "0000000" & AddZero(i - 1, 10) & "2" & Space(82)


objFile.Close 'Close the file object


OutFile = "140800050" & FormatDate(Date) & "001.txt"
Set objFile = objFSO.OpenTextFile(OutFile, ForWriting, True) 'Open OutFile in writing mode
objFile.Write OutStr
objFile.Close 'Close the file object

Set objFile = Nothing
Set objFSO = Nothing

Function FixStr(StrExp)
Do While InStr(StrExp, " ")
StrExp = Replace(StrExp, " ", " ")
Loop
FixStr = StrExp
End Function

Function FormatDate(tDate)
FormatDate = Right(Year(tDate), 2) & AddZero(Month(tDate), 2) & AddZero(Day(tDate), 2)
End Function

Function AddZero(sNum, iLen)
Addzero = Right("00000000000" & sNum, iLen)
End Function

Really thank you, Great Slore, for your help. Please be sure to continue to help me in the future... Thanks in advance!

This problem has been solved. Thank you all greats for your attention and support.

감사합니다!

Studying hard...
Floor 36 Posted 2008-02-01 18:56 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
I originally wanted to ask about that in Chinese. Just felt it was a bit strange.

But if you did that *2, it wouldn't be right if it's in English...
But it should all be in Chinese...
Floor 37 Posted 2008-02-01 19:02 ·  中国 福建 福州 联通
初级用户
★★
Credits 127
Posts 59
Joined 2008-01-29 01:05
18-year member
UID 109744
Status Offline
Yes, currently in actual operation, it's still only in Chinese. What if English appears? -- Can the Great One provide me with an improved general version?...... Hehe...... Don't blame me for being greedy......

감사합니다!
Floor 38 Posted 2008-02-01 19:36 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Even supports mixed Chinese and English names... Sweat~ There shouldn't be, right?

Only one place was changed, len was changed to MixLen.

Example++.vbs:


Const ForReading = 1 'Set the text opening method to reading
Const ForWriting = 2 'Set the text opening method to writing
Const ForAppending = 8 'Set the text opening method to appending

Const InFile = "a.txt" 'Set the text to open

Dim i,Sum
Dim StrLine,OutStr
i = 1:Sum = 0

'On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject") 'Reference the FSO object, needed for reading and writing files
Set objFile = objFSO.OpenTextFile(InFile, ForReading) 'Open InFile in reading mode

Do Until objFile.AtEndOfStream
StrLine = objFile.ReadLine 'Read a line
If Len(StrLine) Then 'If it's not an empty line
StrLine = FixStr(StrLine)
NeedData = Split(StrLine," ")
If Right(NeedData(1),1) = "*" Then NeedData(1) = Left(NeedData(1),Len(NeedData(1)) - 1)
NeedData(4) = NeedData(4) & Space(53 - MixLen(NeedData(4)))
OutLine = "2140800050202" & NeedData(1) & "3" & AddZero(i,6) & "00" & NeedData(2)
OutLine = OutLine & "000000000000000011 " & AddZero(i - 1,9) & NeedData(4)
OutStr &= OutLine & vbCrLf
i += 1
Sum += CLng(NeedData(2))
End If
Loop
OutStr &= "14080005020214080100112003624143" & AddZero(i,6)
OutStr &= "00" & AddZero(Sum,15) & "0000000" & AddZero(i - 1,9) & "2" & Space(82)


objFile.Close 'Close the file object


OutFile = "140820" & FormatDate(Date) & "001.txt"
Set objFile = objFSO.OpenTextFile(OutFile, ForWriting,True) 'Open OutFile in writing mode
objFile.Write OutStr
objFile.Close 'Close the file object

Set objFile = Nothing
Set
objFSO = Nothing


Function
AddZero(sNum,iLen)
Addzero = Right("000000000" & sNum,iLen)
End Function

Function
FixStr(StrExp)
Do While InStr(StrExp," ")
StrExp = Replace(StrExp," "," ")
Loop
FixStr = StrExp
End Function

Function
MixLen(StrExp)
Dim i,c
For i = 1 /color]To Len(StrExp)
c= Asc(Mid(StrExp, i, 1))
If Not (C /color]>= 0 /color]And C /color]< 128) Then MixLen /color]= MixLen /color]+ 1
MixLen /color]= MixLen /color]+ 1
Next
End Function

Function
FormatDate(tDate)
FormatDate = Right(Year(tDate),2) & AddZero(Month(tDate),2) & AddZero(Day(tDate),2)
End Function
Floor 39 Posted 2008-02-01 21:10 ·  中国 福建 泉州 电信
初级用户
★★
Credits 127
Posts 59
Joined 2008-01-29 01:05
18-year member
UID 109744
Status Offline
Wow, Great Slore, you're so cool...

Excuse me, may I ask about the Mixlen function?
Floor 40 Posted 2008-02-01 21:28 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
MsgBox MixLen("I am Slore") 'Returns the result 9.

'MixLen treats Chinese as 2 lengths, and numbers, English letters, and some symbols
'(Common characters have ASCII codes within 0 to 128)
'Treated as 1 length.
Function MixLen(StrExp)
Dim i,c
For i = 1 To Len(StrExp)
c = Asc(Mid(StrExp, i, 1))
If Not (C >= 0 And C < 128) Then MixLen = MixLen + 1
MixLen = MixLen + 1
Next
End Function
Floor 41 Posted 2008-02-01 21:33 ·  中国 福建 泉州 电信
初级用户
★★
Credits 127
Posts 59
Joined 2008-01-29 01:05
18-year member
UID 109744
Status Offline
Wow, this is awesome!!! ——Great job!
Learn first and then bookmark! ——Remember it firmly!

Whisper a word: Slore, little sister opened a new thread, come over and visit!

Thank you very much!

http://www.cn-dos.net/forum/viewthread.php?tid=37490&sid=eXmTWk
Forum Jump: