问题
If I had a fixed width file (.txt) with specs (which characters form which field) such as:
1-10 id_no
11-25 seq
26-30 cur_code
31-40 first
41-90 cur_desc
91-120 misa
Example 3 lines in the file:
7284585 98354u38654 347 USD jfsnkjndf;kjsdgn;jndfsjngjdsngnkjdsfngkjsdnfgjnfhUnited States Dollar
728458598354u38654 347 USD jfsnkjndf;kjsdgn;jndfsjngjdsngnkjdsfngkjsdnfgjn Euro
7284585 98354u38654347 GBP jfsnkjndf;kjsdgn;jndfsjngjdsngnkjdsfngkjsdnfgjn Pound
What vbscript code would I have to write that:
- read all lines in the file
- outputted any line numbers and cur_desc values to another file (ie. 'test_currency_DDMMYYYY.txt') that met this condition:
(cur_code = 'USD' and cur_desc != 'United States Dollar')
?
回答1:
Reading text file line by line, parsing and writing to another text file:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set outPut = objFSO.CreateTextFile("c:\\output.txt", true);
Set objTextFile = objFSO.OpenTextFile _
("c:\mytextfile.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.Readline
' parse strLine
id_no = Mid(strLine, 1, 9)
seq = Mid(strLine, 11, 14) ' and so on
outPut.WriteLine(id_no & "_" & seq);
Loop
outPut.Close();
Set objFSO = Nothing
Set outPut = Nothing
Parsing string may be perfomed with VB string functoins such as Split, Mid, Len etc. E.g
id_no = Mid(strLine, 1, 9)
seq = Mid(strLine, 11, 14) ' and so on
来源:https://stackoverflow.com/questions/7415987/vbscript-qtp-fixed-width-file-check-contents