VBScript (QTP) - fixed width file - check contents

帅比萌擦擦* 提交于 2019-12-11 12:36:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!