[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Import Multiple INI Files

Posted on 2009-02-23
17
Medium Priority
?
348 Views
Last Modified: 2012-05-06
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
Comment
Question by:JLAMERTON
  • 7
  • 6
  • 4
17 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 23710942
* 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
 

Author Comment

by:JLAMERTON
ID: 23711799
For 500 points I was after more the code to do this rather a pointer to the API required

Thanks
0
 
LVL 46

Expert Comment

by:aikimark
ID: 23713768
@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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:JLAMERTON
ID: 23713811
Apologies.. Didnt read your response correctly.. my fault!

That are located on a single directory on local pc!

Thanks
0
 
LVL 46

Expert Comment

by:aikimark
ID: 23714619
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
 
LVL 46

Expert Comment

by:aikimark
ID: 23714789
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23718455
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23718465
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
 

Author Comment

by:JLAMERTON
ID: 23719951
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
 

Author Comment

by:JLAMERTON
ID: 23720500
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
 

Author Comment

by:JLAMERTON
ID: 23720600
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23722343
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
 

Author Comment

by:JLAMERTON
ID: 23722970
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23723930
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23724089
In your examples are there really spaces? Between the =?

    ObjectName = "Adobe Reader"
    DisplayName = "Adobe Reader"
    Description = "Adobe Reader"
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 23724190
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
 

Author Closing Comment

by:JLAMERTON
ID: 31550033
Great thanks thats got it.. Just have to work out how to export it back out now
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

872 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