By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,456 Members | 1,494 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,456 IT Pros & Developers. It's quick & easy.

Format a file to get desired output using VBScript

P: 5
Hello ,

I tried a code which reads the file InputFile.txt (attached) grep particular values and write it to a file in a desired output format is in file OutputFile.txt

Below is the code partially I have done..

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim re, targetString, colMatch, objMatch, nMT
  3.  
  4. Set objFS = CreateObject("Scripting.FileSystemObject")
  5.  
  6. Set rs = New RegExp
  7.  
  8. strFileIN = "..\Inputfile.txt"
  9. strFileOUT = "..\Outputfile.txt"
  10.  
  11. With rs
  12.   .Pattern = "\w+\(\w\.+\)" & "\w+\(\w+\)" 
  13.   .Global = True
  14.   .IgnoreCase = True
  15. End With 
  16.  
  17. Set objFile = objFS.OpenTextFile(strFileIN)
  18. strText = objFile.ReadAll
  19.  
  20.  
  21.  
  22. Set colMatch = rs.Execute(strText)
  23.  
  24. Set objOutFile = objFS.CreateTextFile(strFileOUT,True)    
  25.  
  26.  
  27. For nMT = 0 To colMatch.Count - 4
  28.  
  29. objOutFile.WriteLine colMatch(nMT + 0) &" "& colMatch(nMT + 1) &" "& colMatch(nMT + 2) &" "& colMatch(nMT + 3)
  30.  
  31. Next 
  32.  
  33.  
  34.  
  35. objOutFile.Close
  36.  
  37.  
Help me on correcting my code to get to my desired output as in file OutputFile.txt.
Attached Files
File Type: txt InputFile.txt (847 Bytes, 278 views)
File Type: txt OutputFile.txt (280 Bytes, 178 views)
Aug 14 '12 #1
Share this Question
Share on Google+
10 Replies


Rabbit
Expert Mod 10K+
P: 12,315
You haven't said what is wrong with the code. Not working is too general a statement.

Also, since VBS is closer to VBA than VB6, I'm moving this to the Access forum where you may get more help.
Aug 14 '12 #2

P: 5
QUEUE(TEST.QA.Q1) TYPE(QLOCAL)
CURDEPTH(0) MAXDEPTH(100000)

On my inputfile.txt , How do I form the regular expression pattern to match above pattern's. pls note word "TEST.QA.Q1" may change and the words delimited by "." dots may vary in numbers.

The pattern That i formed in my code didn't match QUEUE(TEST.QA.Q1).
Aug 14 '12 #3

Rabbit
Expert Mod 10K+
P: 12,315
Your regex pattern is malformed. It should be \w+\([\w\.]+\)
Aug 14 '12 #4

P: 5
Thanks Rabbit . It worked. I will try my code.
Aug 14 '12 #5

P: 5
Expand|Select|Wrap|Line Numbers
  1. QUEUE(Q1.A1.C3.TEMP)         TYPE(QLOCAL)
  2.    CURDEPTH(0)                             MAXDEPTH(1000)


How to take values between ( and ) parenthesis and assign it to a variable ? I tried with mid function, but since my number of characters change it is not a solution. Do we have a special function ?



Expand|Select|Wrap|Line Numbers
  1. QUEUE=Q1.A1.C3.TEMP TYPE=QLOCAL CURDEPTH=0 MAXDEPTH=1000
Aug 16 '12 #6

Rabbit
Expert Mod 10K+
P: 12,315
You already have the pattern, it's the one I posted earlier.
Aug 16 '12 #7

P: 5
Corrected my question to be specific. Please check now.
Aug 16 '12 #8

Rabbit
Expert Mod 10K+
P: 12,315
You can use the replace function.
Aug 16 '12 #9

100+
P: 332
You are probably looking after placeholders ($1, $2) to use the expression you put between parenthesis in your regular expression. Here is an exemple I have in my codes. It takes a line of text, search for numbers having a negative sign at the end and move it at the beginning of the number.
Expand|Select|Wrap|Line Numbers
  1. Function BringForwardNegativeSign(LineToFix As String) As String
  2. 'This is to put negative sign in front 6.54- to -6.54 that exist in the DRMIS file.
  3.     Dim re As New RegExp
  4.     re.pattern = "\|( +)([0-9]*,?[0-9]*,?[0-9]*\.[0-9]{2})-"
  5.     re.Global = True
  6.     If (re.test(LineToFix) = True) Then
  7.         BringForwardNegativeSign = re.Replace(LineToFix, "|$1-$2")
  8.     Else
  9.         BringForwardNegativeSign = LineToFix
  10.     End If
  11. End Function
  12.  
Aug 16 '12 #10

Expert 5K+
P: 8,435
I'd just like to mention here that Instr() may be the function you were looking for. It searches a string for another string and tells you where (or whether) it occurs.
Aug 22 '12 #11

Post your reply

Sign in to post your reply or Sign up for a free account.