views:

55

answers:

2

I had asked a question about parsing a file in ruby. I accepted an answer and wrote the following ruby script:

file = File.open('X:myfile.txt', 'r')
file.each_line do |line|
    ccyy = line[53...57]
    mmdd = line[57...61]
    line[53...57] = mmdd
    line[57...61] = ccyy
    File.open('c:\myfile_MODIFIED.txt', 'a') do |f2|  
        f2.puts line
    end
end

This script will run in production and change the file, however, production is a windows box and only vbscript is allowed to run on it. Unfortunatley I havn't written anything in VB before. Can someone help in converting the meat of above code to vb?

What I have so far is:

Dim oFolder, oFile, sText, ots

Set OFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = OFSO.GetFolder("X:\myfolder")
Set RegXP=New RegExp
RegXP.IgnoreCase=1
RegXP.Pattern="PROD_FILE_\d+.txt"



For Each oFile in oFolder.Files 
    If (RegXP.test(oFile.Name)) Then
        WScript.Echo oFile.Name
        set ots = OFSO.opentextfile(oFile)
        Do While Not ots.AtEndOfStream
            sText = ots.ReadLine
                  'read file line by line. change characters in the line. write line to new file'   
        Loop
        ots.close

    End If
Next
A: 

Looks like you're 90% of the way there.

Since VBScript doesn't provide direct random access to string contents, you need to use some functions to do the string manipulation.

I'd go with Mid and Left.

E.g.

dim a, b
a = "Hello, World!"
b = left( mid( a, 8 ), 5 )
wscript.echo(b)

Output: World

dmb
thanks. but that still doesnt replace the string. from your example....say I am trying to replace 4th character of a with 6th character of a
john
dmb
A: 

Are you looking for Replace or Mid?

 ''Replace(expression, find, replace [, start ] [, count ] [, compare ] )
 Replace(sText,"a","b") 

 Mid(sText,4,1)=Mid(sText,6,1)
Remou