• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Import Multiple INI Files

I have about 100 ini files that contain configuration data for Citrix Applications.
I need to create a batch import Job into Excel or Access with VBScript that will go through all the INI files and import them so i can see the Values
Each Ini file has for example
Name= This is Application Nam
Path=This is Application Path

Id like to dump them to table so i had

Name                                              Path
Application1                                    C:\App1
Application2                                    C:\App2

Thanks
0
JLAMERTON
Asked:
JLAMERTON
  • 7
  • 6
  • 4
1 Solution
 
aikimarkCommented:
* use the GetPrivateProfileString Windows API to most conveniently read the contents of an INI file.

* how to loop through the list of INI files depends on where they are located.
0
 
JLAMERTONAuthor Commented:
For 500 points I was after more the code to do this rather a pointer to the API required

Thanks
0
 
aikimarkCommented:
@JLAMERTON

I was (still am) awaiting your response to my earlier comment item:
"...depends on where they are located"

No need in giving you a partial answer.  My earlier answer assumed you'd never processed INI files and was looking for a clue.  I don't mind posting code, regardless of the points.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
JLAMERTONAuthor Commented:
Apologies.. Didnt read your response correctly.. my fault!

That are located on a single directory on local pc!

Thanks
0
 
aikimarkCommented:
I started this code using VB and then caught the part of your question about using this in the VB Script environment.  Forget what I wrote earlier about GetPrivateProfileString.  There are limitations using Windows APIs from within VB Script files.  (I learned something new)

You will need to change the line where the cSection constant is defined.
In your VB Script file, invoke the ExtractCitrixData subroutine

NOTE: I used some code posted here:
http://www.motobit.com/tips/detpg_asp-vbs-read-write-ini-files/

Public Sub ExtractCitrixData(parmDir)
    Dim strINIfile, strINIDir
    Dim oExcel  'Excel application
    Dim oWS     'worksheet
    Dim lRow
    Dim strName, strPath
    
    Const cSection = "The Section Name"  '<== CHANGE THIS STRING
    
    If Right(parmDir, 1) = "\" Then
        strINIDir = parmDir
    Else
        strINIDir = parmDir & "\"
    End If
    
    Set oExcel = CreateObject("Excel.Application")
    
    oExcel.Visible = True
    oExcel.Workbooks.Add
    oExcel.Cells(1, 1).Value = "Name"
    oExcel.Cells(1, 2).Value = "Path"
    
    lRow = 2
    strINIfile = Dir(strINIDir & "*.INI")
    Do Until Len(strINIfile) = 0
        strName = GetINIString(cSection, "Name", "", strINIDir & strINIfile)
        strPath = GetINIString(cSection, "Path", "", strINIDir & strINIfile)
        oExcel.Cells(lRow, 1).Value = strName
        oExcel.Cells(lRow, 2).Value = strPath
 
        strINIfile = Dir    'next INI file
    Loop
 
    oExcel.Save  'should prompt you for location
 
End Sub
 
'=============================================
'Work with INI files In VBS (ASP/WSH)
'v1.00
'2003 Antonin Foller, PSTRUH Software, http://www.motobit.com
'Function GetINIString(Section, KeyName, Default, FileName)
'=============================================
Function GetINIString(Section, KeyName, Default, FileName)
  Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
  
  'Get contents of the INI file As a string
  INIContents = GetFile(FileName)
 
  'Find section
  PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
  If PosSection > 0 Then
    'Section exists. Find end of section
    PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
    '?Is this last section?
    If PosEndSection = 0 Then PosEndSection = Len(INIContents) + 1
    
    'Separate section contents
    sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
 
    If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare) > 0 Then
      Found = True
      'Separate value of a key.
      Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
    End If
  End If
  If IsEmpty(Found) Then Value = Default
  GetINIString = Value
End Function
 
'Separates one field between sStart And sEnd
Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
  Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
  If PosB > 0 Then
    PosB = PosB + Len(sStart)
    Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
    If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
    If PosE = 0 Then PosE = Len(sFrom) + 1
    SeparateField = Mid(sFrom, PosB, PosE - PosB)
  End If
End Function
 
 
'File functions
Function GetFile(ByVal FileName)
  Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
  'Go To windows folder If full path Not specified.
  If InStr(FileName, ":\") = 0 And Left(FileName, 2) <> "\\" Then
    FileName = FS.GetSpecialFolder(0) & "\" & FileName
  End If
  On Error Resume Next
 
  GetFile = FS.OpenTextFile(FileName).ReadAll
End Function

Open in new window

0
 
aikimarkCommented:
0
 
AmazingTechCommented:
Perhaps this is all you need.

Save the code as .bat or .cmd extension.

Modify

Set InputFolder=C:\CitrixINIFolder
Set Output=CitrixINIValues.csv
SETLOCAL ENABLEDELAYEDEXPANSION
Set InputFolder=C:\CitrixINIFolder
Set Output=CitrixINIValues.csv
 
Echo "File","Application Name","Application Path">"%output%"
 
Set CitrixININame=
Set CitrixINIPath=
For /r "%InputFolder%" %%a In (*.ini) do (
    For /f "Tokens=* delims=" %%b In ('Findstr /i /c:"NAME=" /c:"PATH=" "%%a"') do (
        Set CitrixINI%%b
        If Defined CitrixININame If Defined CitrixINIPath ECHO "%%a","!CitrixININame!","!CitrixINIPath!">>"%Output%" && Set CitrixININame=&& Set CitrixINIPath=
    )
)
Start excel %output%

Open in new window

0
 
AmazingTechCommented:
Maybe this one will be a little better if your INI files have leading spaces before NAME= or PATH=
SETLOCAL ENABLEDELAYEDEXPANSION
Set InputFolder=C:\CitrixINIFolder
Set Output=CitrixINIValues.csv
 
Echo "File","Application Name","Application Path">"%output%"
 
Set CitrixININame=
Set CitrixINIPath=
For /r "%InputFolder%" %%a In (*.ini) do (
    For /f "Tokens=*" %%b In ('Findstr /i /c:"NAME=" /c:"PATH=" "%%a"') do (
        Set CitrixINI%%b
        If Defined CitrixININame If Defined CitrixINIPath ECHO "%%a","!CitrixININame!","!CitrixINIPath!">>"%Output%" && Set CitrixININame=&& Set CitrixINIPath=
    )
)
Start excel %output%

Open in new window

0
 
JLAMERTONAuthor Commented:
The VBS file doesnt seem to be doing anything.
The Batch option is creating the header rows but not importing any data in from the INI files.. The actual syntax of the ini is the following

    ObjectName = "Adobe Reader"
    DisplayName = "Adobe Reader"
    Description = "Adobe Reader"

So there are spaces both before after any during so dont know if this is causing the issue.
Will play around here as well with that code to see if i can further

Thanks all
0
 
JLAMERTONAuthor Commented:
Here is my error log

C:\Citrix>citrix

C:\Citrix>SETLOCAL ENABLEDELAYEDEXPANSION

C:\Citrix>Set InputFolder=C:\Citrix

C:\Citrix>Set Output=C:\Citrix\CitrixINIValues.csv

C:\Citrix>Echo "ObjectName" 1>"C:\Citrix\CitrixINIValues.csv"

C:\Citrix>Set CitrixININame=

Set CitrixINIPath=

For /R "C:\Citrix" %a In (*.ini) do (For /F "Tokens=* delims=" %b In (
'Findstr /i /c:"NAME=" /c:"PATH=" "%a"') do (Set CitrixINI%b
 If Defined CitrixININame If Defined CitrixINIPath ECHO "%a","!CitrixININame!","
!CitrixINIPath!"  1>>"C:\Citrix\CitrixINIValues.csv"  && Set CitrixININame=  &&
Set CitrixINIPath=
) )

(For /F "Tokens=* delims=" %b In ('Findstr /i /c:"NAME=" /c:"PATH=" "C:\Citrix\Joe - Copy.INI"') do (
Set CitrixINI%b
 If Defined CitrixININame If Defined CitrixINIPath ECHO "C:\Citrix\Joe - Copy.IN
I","!CitrixININame!","!CitrixINIPath!"  1>>"C:\Citrix\CitrixINIValues.csv"  && S
et CitrixININame=  && Set CitrixINIPath=
) )

(
Set CitrixINIObjectName=Joe
 If Defined CitrixININame If Defined CitrixINIPath ECHO "C:\Citrix\Joe - Copy.INI","!CitrixININame!","!CitrixINIPath!"  1>>"C:\Citrix\CitrixINIValues.csv"  && Set CitrixININame=  && Set CitrixINIPath=
)

(For /F "Tokens=* delims=" %b In ('Findstr /i /c:"NAME=" /c:"PATH=" "C:\Citrix\Joe.INI"') do (
Set CitrixINI%b If Defined CitrixININame If Defined CitrixINIPath ECHO "C:\Citrix\Joe.INI","!Ci
trixININame!","!CitrixINIPath!"  1>>"C:\Citrix\CitrixINIValues.csv"  && Set CitrixININame=  && Set CitrixINIPath=
) )

(
Set CitrixINIObjectName=Joe
 If Defined CitrixININame If Defined CitrixINIPath ECHO "C:\Citrix\Joe.INI","!CitrixININame!","!CitrixINIPath!" 1>>"C:\Citrix\CitrixINIValues.csv"  && Set CitrixININame=  && Set CitrixINIPath=
)

Start excel C:\Citrix\CitrixINIValues.csv
0
 
JLAMERTONAuthor Commented:
Right the code is now working as follows however it only works as one file at a time... If you point 2 files to it rather than going to the next row in the CSV it overwrites the first!

SETLOCAL ENABLEDELAYEDEXPANSION
Set InputFolder=C:\Citrix
Set Output=C:\Citrix\CitrixINIValues.csv
 
Echo "FilePath","ObjectName","Path">"%output%"
 
Set CitrixINIObjectName=
Set CitrixINIPath=
For /r "%InputFolder%" %%a In (*.App) do (
    For /f "Tokens=* delims=" %%b In ('Findstr /i /c:"ObjectName=" /c:"PATH=" "%%a"') do (
        Set CitrixINI%%b
        If Defined CitrixINIObjectName If Defined CitrixINIPath ECHO "%%a","!CitrixINIObjectName!","!CitrixINIPath!">>"%Output%" && Set CitrixINIObjectName=&& Set CitrixINIPath=
    )
)
Start excel %output%

Open in new window

0
 
AmazingTechCommented:
Really?

Do you mean the second run of the batch file or 2 .app files in C:\Citrix?

If it the second run of the batch file then try this.

SETLOCAL ENABLEDELAYEDEXPANSION
Set InputFolder=C:\Citrix
Set Output=C:\Citrix\CitrixINIValues.csv
 
If Not Exist "%output%" Echo "FilePath","ObjectName","Path">"%output%"
 
Set CitrixINIObjectName=
Set CitrixINIPath=
For /r "%InputFolder%" %%a In (*.App) do (
    For /f "Tokens=* delims=" %%b In ('Findstr /i /c:"ObjectName=" /c:"PATH=" "%%a"') do (
        Set CitrixINI%%b
        If Defined CitrixINIObjectName If Defined CitrixINIPath ECHO "%%a","!CitrixINIObjectName!","!CitrixINIPath!">>"%Output%" && Set CitrixINIObjectName=&& Set CitrixINIPath=
    )
)
Start excel %output%

Open in new window

0
 
JLAMERTONAuthor Commented:
No its 2 .app files in C:\Citrix in this case it just overwrites the first row with data.
(.App files are same structure as INI files)

Thanks
0
 
AmazingTechCommented:
That's really weird because we're only ever piping to the file here with double >>

ECHO "%%a","!CitrixINIObjectName!","!CitrixINIPath!">>"%Output%"

It seemed to work for me.

Attached code fixes the double quote problem.
SETLOCAL ENABLEDELAYEDEXPANSION
Set InputFolder=C:\Citrix
Set Output=C:\Citrix\CitrixINIValues.csv
 
If Not Exist "%output%" Echo "FilePath","ObjectName","Path">"%output%"
 
Set CitrixINIObjectName=
Set CitrixINIPath=
For /r "%InputFolder%" %%a In (*.App) do (
    For /f "Tokens=* delims=" %%b In ('Findstr /i /c:"ObjectName=" /c:"PATH=" "%%a"') do (
        Set CitrixINI%%b
        If Defined CitrixINIObjectName If Defined CitrixINIPath ECHO "%%a","!CitrixINIObjectName:"=!","!CitrixINIPath:"=!">>"%Output%" && Set CitrixINIObjectName=&& Set CitrixINIPath=
    )
)
Start excel %output%

Open in new window

MSOffice.app.txt
adobe.app.txt
0
 
AmazingTechCommented:
In your examples are there really spaces? Between the =?

    ObjectName = "Adobe Reader"
    DisplayName = "Adobe Reader"
    Description = "Adobe Reader"
0
 
AmazingTechCommented:
Try this to account for spaces.
SETLOCAL ENABLEDELAYEDEXPANSION
Set InputFolder=C:\Citrix
Set Output=C:\Citrix\CitrixINIValues.csv
 
If Not Exist "%output%" Echo "FilePath","ObjectName","Path">"%output%"
 
Set CitrixINIObjectName=
Set CitrixINIPath=
For /r "%InputFolder%" %%a In (*.App) do (
    For /f "Tokens=1,* delims== " %%b In ('Findstr /i /c:"ObjectName" /c:"PATH" "%%a"') do (
        Set CitrixINI%%b=%%~c
        If Defined CitrixINIObjectName If Defined CitrixINIPath ECHO "%%a","!CitrixINIObjectName!","!CitrixINIPath!">>"%Output%" && Set CitrixINIObjectName=&& Set CitrixINIPath=
    )
)
Start excel %output%

Open in new window

0
 
JLAMERTONAuthor Commented:
Great thanks thats got it.. Just have to work out how to export it back out now
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 7
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now