Solved

Parse ini file, write specific values to .csv

Posted on 2007-12-05
12
679 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:Phosphor
  • 6
  • 6
12 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20413705
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

0
 
LVL 1

Author Comment

by:Phosphor
ID: 20413984
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
0
 
LVL 12

Accepted Solution

by:
chandru_sol earned 375 total points
ID: 20414062
Try this..........

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

0
 
LVL 1

Author Comment

by:Phosphor
ID: 20414156
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.
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20414188
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

0
 
LVL 1

Author Comment

by:Phosphor
ID: 20414254
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?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:Phosphor
ID: 20421679
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

0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20421716
Hope it solved your purpose

regards
Chandru
0
 
LVL 1

Author Closing Comment

by:Phosphor
ID: 31413214
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.
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20421765
If that was the case i would have helped you to get what you wanted. I thought that solved your purpose

regards
Chandru
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20421792
May be if you think i can still help please let me know

I will be glad to help :-)
0
 
LVL 1

Author Comment

by:Phosphor
ID: 20422060
Nothing personal, I appreciate the help though you never answered me back.  I'll chalk it up to a learning experience.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now