Link to home
Start Free TrialLog in
Avatar of Phosphor
Phosphor

asked on

Parse ini file, write specific values to .csv

Hello experts,

I have an ini file where the desired values are located on specific lines denoted by quotation marks.
So far I have the line-values echoing back as needed, but not the values only. I need to trim them, but they are not all the same trim parameters. Left trimming varies by 5-9 chars and the right trim is always 1.
It may be easier to just read line by line and only get the values between the quotation marks, but I don't know how to do it. Instead of echoing back the output, I need to save it as a csv file of just the values.

Any help would be greatly appreciated.

Here is my working script:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\currdat.txt", ForReading)

For i = 1 to 24
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 26 to 28
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 30 to 31
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 33 to 34
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 36 to 38
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 40 to 42
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 44 to 47
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 49 to 53
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 55 to 57
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 59 to 61
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 63 to 65
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 67 to 69
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 71 to 73
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
objTextFile.Close
Avatar of chandru_sol
chandru_sol
Flag of India image

Try this script........

regards
Chandru
Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "your text file"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
   If Trim(sText) <> "" Then
    WScript.Echo sText
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."

Open in new window

Avatar of Phosphor
Phosphor

ASKER

I got an error "Expected 'End'", added it, then "Expected 'If'". The loop worked but not what I am looking for. I only want the values that between the quotation marks. Which are on specific lines. e. g.
line 60        locat="NW"
line 64        temp="99.0"
line 68        WWV3="187"

Output to text, not echo.
Sorry, there's a lot I am trying to learn in a short amount of time...
Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "H:\Scripting\HW\hw.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
   If Trim(sText) <> "" Then
    WScript.Echo sText
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If
ASKER CERTIFIED SOLUTION
Avatar of chandru_sol
chandru_sol
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This one returns fewer lines (any with [bracketed]  text are omitted because they don't have "=" signs), it seems to return any lines with an "=" sign, which is interesting and possibly useful down the road. Is it possible to to use the " " marks as tags? The values I need are only within the quotation marks.
This should work for you


regards
Chandru
Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "c:\scripts\testcomma.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
if instr(stext,"""") then
    WScript.Echo sText
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If

Open in new window

This one returns only lines with quotation marks. (which is good because values I need all have quotation marks, though still need to just get the values within the quotation marks. Is it possible to create an array out of those lines and then trim them in another process perhaps?
Using Chr(34) with a really long script implementing intermediate text files got me where I needed to go.  The second iteration you posted works to strip out all the stuff I absolutely don't need (blank lines, ini tags etc.) Thanks for helping out
Const ForReading = 1
Const ForWriting = 2
 
'Dim oFSO, sFile, oFile, sText, sNText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "H:\Scripting\HW\hw.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do Until oFile.AtEndOfStream
  sText = oFile.ReadLine
if instr(stext,"=") then
    'WScript.Echo sText
sNText = sNText & sText & vbCrLf
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If
 
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile _
    ("h:\Scripting\hw\CSWrite.txt", ForWriting)
oFile.Write(sNText)
oFile.Close
 
Const ForAppending = 8
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("h:\Scripting\hw\CSWrite.txt", ForAppending)
 
objFile.Write "Timestamp=" & Chr(34) & Now & Chr(34)
 
objFile.Close

Open in new window

Hope it solved your purpose

regards
Chandru
Accepting answer in protest!

I don't believe the replies were exactly what I was looking for. I wanted to make this script  as concise as possible, though I ended up stringing a bunch of scripts together to get my desired results.  Using a variety of splits/replacement routines being performed on intermediate files I was able to get my project going, but I did it by myself. I would not have awarded any points for this query and I'd be hard-pressed to award any points to my my other scripting question that is still open. I think you need to be able to adjust points, or outright deny them for half-assed, incomplete answers.
I am beginning to think that experts-exchange is going downhill as far as quality feedback is concerned.
If that was the case i would have helped you to get what you wanted. I thought that solved your purpose

regards
Chandru
May be if you think i can still help please let me know

I will be glad to help :-)
Nothing personal, I appreciate the help though you never answered me back.  I'll chalk it up to a learning experience.