vbs script to read an ini file and copy lines

August 25, 2011 at 09:28:35
Specs: ws 2003
hello everyone,

i am a vbs beginner and i'm working on my first script. what i need is a vbs cript to read an ini file and copy specific lines (see example)

[abc]
000
000
000
abc=123456

[def]
bla
bla
456
abc=123456

[ghi]
4r5
d8d
abc=123

and so on...

i need the script to ouput fillowing data into another file;

[abc]
abc=123456

[ghi]
abc=123

and so on...

i have some code but it's wouldn't help to post it here since it's not working at all :(

really appreciate your help!


See More: vbs script to read an ini file and copy lines

Report •

#1
August 25, 2011 at 11:00:07
i have some code but it's wouldn't help to post it here since it's not working at all :(
Translation: The mean men on the Internet would make fun of it.
You know what? You're right; I totally would.

Const iniPath = "path\to\some.ini"
Const outPath = "out.txt"
Const searchStr = "abc"

With CreateObject("Scripting.FileSystemObject")
  Set iniFile = .OpenTextFile(iniPath)
  Set outFile = .OpenTextFile(outPath, 2, True)
End With

Do Until iniFile.AtEndOfStream
  line = Trim(iniFile.ReadLine)
  If Left(line, 1) = "[" And Right(line, 1) = "]" Then
    outFile.WriteLine vbNewLine & line
  ElseIf Len(line) Then
    If Split(line, "=")(0) = searchStr Then _
      outFile.WriteLine line
  End If
Loop

How To Ask Questions The Smart Way


Report •

#2
August 25, 2011 at 11:58:12
hello razor,

thanks for your help. when i run your script i also get [def] which i actually don't need.

greatly appreciate your help!


Report •

#3
August 25, 2011 at 13:16:55
Question: How can you tell if you need it or not? (I'm assuming it's more complicated than, "If it's not 'def,' grab it.")

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 25, 2011 at 14:07:28
i actually have a list of values i need to look for and they can be hard coded into the script.

the ini file has about 10-15 brackets with different values and i only need 5. the problem is that i need the values in the brackets and a few additional values below the brackets to be included in the output file.

let me know if you want me to post the ini file with values i'm looking for.

thanks!


Report •

#5
August 25, 2011 at 14:20:27
i have modified your script a bit and i'm able to copy the brackets and values but not the values below the brackets.

and of course this is not the cleanest solution but i don't want you to think that i'm chilling out and waiting on your replies :)

Const iniPath = "c:\scripts\lab\some.ini"
Const outPath = "out1.txt"
Const searchStr = "start"

With CreateObject("Scripting.FileSystemObject")
Set iniFile = .OpenTextFile(iniPath)
Set outFile = .OpenTextFile(outPath, 2, True)
End With

Do Until iniFile.AtEndOfStream
line = Trim(iniFile.ReadLine)
If Left(line, 1) = "[" And Right(line, 4) = "abc]" Then
outFile.WriteLine vbNewLine & line
ElseIf Len(line) Then
If Split(line, "")(0) = searchStr Then _
outFile.WriteLine line
If Left(line, 1) = "[" And Right(line, 4) = "efg]" Then
outFile.WriteLine vbNewLine & line
ElseIf Len(line) Then
If Split(line, "")(0) = searchStr Then _
outFile.WriteLine line
If Left(line, 1) = "[" And Right(line, 4) = "rrr]" Then
outFile.WriteLine vbNewLine & line
ElseIf Len(line) Then
If Split(line, "")(0) = searchStr Then _
outFile.WriteLine line
End If
End If
End If
Loop


Report •

#6
August 25, 2011 at 14:28:16
let me know if you want me to post the ini file with values i'm looking for.
This would help, as I'm going to have to rewrite the script.

How To Ask Questions The Smart Way


Report •

#7
August 26, 2011 at 13:00:50
here is the ini file

[servers]
#location
#times
#installed
location=home-office
run=weekly
installed=yes


[printers-home]
#location
#times
#installed
location=home-office
run=never
installed=yes


[printers-client]
#location
#times
#installed
location=client-office
run=never
installed=no


[monitors]
#location
#times
#count
location=null
run=null
count=50


[scanners]
#location
#times
#installed
location=home-office
run=null
installed=yes

[firewalls]
#location
#times
#certificate
location=null
run=null
certificate=0558-GHN-0125

the output file should look like this

[servers]
location=home-office
run=weekly
installed=yes

[printers-home]
location=home-office
run=never
installed=yes

[monitors]
location=null
run=null
count=50

[firewalls]
location=null
run=null
certificate=0558-GHN-0125

thank you!


Report •

#8
August 26, 2011 at 13:15:40
Just so we're clear, you want the not-comments for sections "servers", "printer-home", "monitors", and "firewalls"? Should this be configurable?

How To Ask Questions The Smart Way


Report •

#9
August 26, 2011 at 13:42:03
when you say "configurable" do you mean if the data is going to be constant or dynamic?

if that's the question, the data will always be the same as shown in the ini file. i might have more data but it won't change from ie "run=never" to "run=always".

thanks


Report •

#10
August 26, 2011 at 14:00:10
when you say "configurable" do you mean if the data is going to be constant or dynamic?
No, I mean, "Should there be a file that lists the ini sections you care about?" so you could easily add, say, scanners in the future.

How To Ask Questions The Smart Way


Report •

#11
August 26, 2011 at 14:10:03
ohh i see. yes, that would be great and would make things easier for me.

Report •

#12
August 31, 2011 at 09:51:04
hi razor, did you have time to take a look at the script?

appreciate your help


Report •

#13
September 1, 2011 at 10:18:18

Report •

#14
September 2, 2011 at 14:08:14
Alright, first the sections list. I called it sections.lst. It's one line per section, brackets can be included or not; doesn't matter. Example:
servers
printers-home
[monitors]
[firewalls]

Next, the actual script. It's kinda big, but that's how it goes.

Const iniPath = "path\to\some.ini"
Const outPath = "out.ini"
Const sectionPath = "sections.lst"

Set fso = CreateObject("Scripting.FileSystemObject")
Set section = CreateObject("Scripting.Dictionary")

'Build allowed sections list
With fso.OpenTextFile(sectionPath)
  Do Until .AtEndOfStream
    line = LCase(Trim(.ReadLine))
    If Len(line) > 0 Then
      'Hack-ish; don't care
      line = Replace(Replace("[" & line & "]", "[[", "["), "]]", "]")
      section(line) = True
    End If
  Loop
End With

'Parse ini
Set outFile = fso.OpenTextFile(outPath, 2, True)
printSection = False
With fso.OpenTextFile(iniPath)
  Do Until .AtEndOfStream
    line = Trim(.ReadLine)
    If InStr(line, "#") Then _
      line = Trim(Split(line, "#")(0))
    
    If Len(line) > 0 Then
      'Line has non-comment text
      If Left(line, 1) = "[" And Right(line, 1) = "]" Then
        If printSection Then _
          outFile.WriteLine ""
        printSection = section(LCase(line))
      End If
      If printSection Then _
        outFile.WriteLine line
    End If
  Loop
End With

How To Ask Questions The Smart Way


Report •

#15
September 26, 2011 at 12:24:22
this is awesome and works great. would it be to difficult to use a list of servers instead of the "iniPath" and have just one server or use it on the local box?

thanks


Report •

#16
September 26, 2011 at 14:06:57
would it be to difficult to use a list of servers instead of the "iniPath" and have just one server or use it on the local box?
Depends on how many different places on a box it could be. It'd probably just be easier to use it as a GPO startup script.

How To Ask Questions The Smart Way


Report •

Ask Question