Results 1 to 5 of 5

Thread: INI Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119

    Question

    I am trying to read multiple items from an INI file located in the same directory as my application. I can read different areas of the INI file but I cannot display them or use them in the same msgbox. Please see code below:

    Private Declare Function GetPrivateProfileString _
    Lib "kernel32" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As String, _
    ByVal nSize As Long, _
    ByVal lpFileName As String) As Long

    Dim GetRunDay As Long, GetRunTime As Long, GetLocalPath As Long, GetRemotePath As Long, GetOrigFileName As Long, GetNewFileName As Long

    Dim RunDay As String * 100, RunTime As String * 100, LocalPath As String * 100, RemotePath As String * 100, OrigFileName As String * 100, NewFileName As String * 100

    Private Sub Main()
    Call Schedule
    End Sub

    Public Function iniGrab()
    Dim iniFilePath

    iniFilePath = App.Path & "\" & App.EXEName & ".ini"

    GetRunDay = GetPrivateProfileString("RunDay", "Day", iniFilePath, RunDay, Len(RunDay), iniFilePath)
    GetRunTime = GetPrivateProfileString("RunTime", "Time", iniFilePath, RunTime, Len(RunTime), iniFilePath)
    GetLocalPath = GetPrivateProfileString("LocalPath", "LPath", iniFilePath, LocalPath, Len(LocalPath), iniFilePath)
    GetRemotePath = GetPrivateProfileString("RemotePath", "RPath", iniFilePath, RemotePath, Len(RemotePath), iniFilePath)
    GetOrigFileName = GetPrivateProfileString("OrigFileName", "OName", iniFilePath, OrigFileName, Len(OrigFileName), iniFilePath)
    GetNewFileName = GetPrivateProfileString("NewFileName", "NName", iniFilePath, NewFileName, Len(NewFileName), iniFilePath)

    End Function

    Private Sub Schedule()
    Dim fso As New FileSystemObject

    Call iniGrab

    MsgBox RemotePath & OrigFileName <--*** Problem ***

    Set fso = CreateObject("scripting.filesystemObject")
    'fso.CopyFile RemotePath & OrigFileName, LocalPath & NewFileName

    'MsgBox "File copy complete!"

    End Sub

    I can use both varibales, RemotePath and OrigFileName in two seperate message boxes but not in the same. Not sure why or what I am doing wrong. Once I am able to use these variables together I can use them when doing my file copy and rename. Hopefully someone can shed some light.

    Here is my INI file:

    [RunDay]
    Day=Friday

    [RunTime]
    Time=12:00

    [LocalPath]
    LPath=D:\McAfee\

    [RemotePath]
    RPath=\\servername\mcafee\DAT\

    [OrigFileName]
    OName=sdat4086.exe

    [NewFileName]
    NName=Setup.exe

  2. #2
    Guest
    Try this code.

    Code:
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    
    Public Function Readini(strsection As String, strkey As String, strfullpath As String) As String
       Dim strbuffer As String
       Let strbuffer$ = String$(750, Chr$(0&))
       Let Readini$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    End Function
    
    Public Sub Writeini(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
        Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
    End Sub
    
    Usage:
    'read
    'x is the Object that gets what's in the ini line->(Text1.text = Readini....)
    x = readini("test.ini", "test", "C:\test.ini")
    'MsgBox X
    
    'write
    'Call writeini("test.ini", "test", "just a test", "C:\test.ini")
    Arranged your code:

    Code:
    Public Sub iniGrab() 
    Dim iniFilePath 
    iniFilePath = App.Path & "\" & App.EXEName & ".ini" 
    GetRunDay = readini("RunDay", "Day", iniFilePath) 
    GetRunTime = readini("RunTime", "Time", iniFilePath) 
    GetLocalPath = readini("LocalPath", "LPath", iniFilePath) 
    GetRemotePath = readini("RemotePath", "RPath", iniFilePath) 
    GetOrigFileName = readini("OrigFileName", "OName", iniFilePath) 
    GetNewFileName = readini("NewFileName", "NName", iniFilePath) 
    End Sub
    Hope that works. And why do you have iniGrab() as a function? Why not just call it like a sub?


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119
    Tried it and I am getting close however it still does not work properly. Keep getting run time errors out of stack space. Take another look and see if you cannot see what I am doing wrong. I really appreciate your help. By they way I sent you an email as well.

    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

    Public Function Readini(strsection As String, strkey As String, strfullpath As String) As String
    Dim strbuffer As String
    Let strbuffer$ = String$(750, Chr$(0&))
    Let Readini$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))

    'Calls schedule function
    Schedule
    End Function

    Private Sub Main()
    Call iniGrab
    End Sub

    Public Sub iniGrab()
    Dim iniFilePath As String

    iniFilePath = App.Path & "\" & App.EXEName & ".ini"

    GetRunDay = Readini("RunDay", "Day", iniFilePath)
    GetRunTime = Readini("RunTime", "Time", iniFilePath)
    GetLocalPath = Readini("LocalPath", "LPath", iniFilePath)
    GetRemotePath = Readini("RemotePath", "RPath", iniFilePath)
    GetOrigFileName = Readini("OrigFileName", "OName", iniFilePath)
    GetNewFileName = Readini("NewFileName", "NName", iniFilePath)
    End Sub

    Public Function Schedule()
    Dim fso As New FileSystemObject

    x = Readini("FileManager.ini", "RunDay", "D:\FileManager\FileManager.ini")
    MsgBox x

    'Set fso = CreateObject("scripting.filesystemObject")
    'fso.CopyFile RemotePath & OrigFileName, LocalPath & NewFileName

    'MsgBox "File copy complete!"

    End Function



  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119
    I figured it out after quite a bit of playing! Thank you so much for you help!!!!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Location
    Dayton, OH USA
    Posts
    119
    In case anyone wants the code for referance here it is:Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

    Public Function Readini(strsection As String, strkey As String, strfullpath As String) As String
    Dim strbuffer As String
    Let strbuffer$ = String$(750, Chr$(0&))
    Let Readini$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    End Function

    Private Sub Main()
    Call iniGrab
    End Sub

    Public Sub iniGrab()
    Dim iniFilePath As String

    iniFilePath = App.Path & "\" & App.EXEName & ".ini"

    GetRunDay = Readini("RunDay", "Day", iniFilePath)
    GetRunTime = Readini("RunTime", "Time", iniFilePath)
    GetLocalPath = Readini("LocalPath", "LPath", iniFilePath)
    GetRemotePath = Readini("RemotePath", "RPath", iniFilePath)
    GetOrigFileName = Readini("OrigFileName", "OName", iniFilePath)
    GetNewFileName = Readini("NewFileName", "NName", iniFilePath)

    Call Schedule(GetRunDay, GetRunTime)
    End Sub

    Public Sub Schedule(RunDay, RunTime)
    Dim fso As New FileSystemObject

    MsgBox RunDay & " " & RunTime

    End Sub





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width