Page 1 of 4 123 ... LastLast
Results 1 to 15 of 54
  1. #1
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: INI File Manipulation Mess

    *** edited Geoff W ***

    Ok, I see it on <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=vb&Number=28031&page=0& view=expanded&sb=5>this post</A>



    Steve,

    >I'm attaching the code

    Did you attach the code?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  2. #2
    sgerber
    Guest

    INI File Manipulation Mess

    Anybody in the mood for a real toughie?

    I'm attempting to translate a piece of old WordBasic code into VBA. It's supposed to delete a key from an INI file, then renumber the remaining keys.

    The INI file section looks like this:

    [Series]
    NumSeries=9
    Series0=Nevada
    Series1=Slam Punjab
    Series2=Last Son of Earth
    Series3=Ace Brazil
    Series4=Magda Queen of Gnats
    Series5=Zibfrew of the Police
    Series6=Bell Jar Babes
    Series7=Wugada the Enormous
    Series8=Penny Peony

    (They're names of comic book series -- not all of them real.)

    The code, up to the point where it's supposed to renumber the remaining entries, works. The trouble starts with the section called "Renumber:". (I'm attaching the code and the INI file as dumpit.zip, for reference.)

    Much of the code was cribbed from Woody's old HACKER'S GUIDE book (which, unfortunately, got lost the last time I changed addresses), but I'm sure I've made some colossal mistake in adapting it for my purposes. Unfortunately, being a decidedly part-time programmer, I'm not sharp enough to spot where I went wrong.

    There's a 1976 "Howard the Duck for President" button (a little late, yeah, but they're popular on eBay) and a copy of the trade paperback of "Nevada" in it for anybody who can help me out of this mess! [img]/forums/images/smilies/wink.gif[/img]

  3. #3
    sgerber
    Guest

    Re: INI File Manipulation Mess

    Geoff,

    Sorry about that. The first attachment didn't "take," for whatever reason.

    I've edited the original post to include the attachment.

  4. #4
    sgerber
    Guest

    Re: INI File Manipulation Mess

    Chris,

    And now, to reveal my *true* ignorance...

    Exactly how would I call this function from the earlier version of my macro?

    And no, I won't tell anyone you've been dabbling in comic books. In fact, just to protect your reputation, I'll ask the Academy to take back the Emmy I got for working on that embarrassing Batman cartoon show. And whatever you do, NEVER admit you've been hanging around with some guy who's written at least one or two of the X-Men characters at some point in his career! Mucho uncool. [img]/forums/images/smilies/wink.gif[/img]

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: INI File Manipulation Mess

    >Exactly how would I call this function from the earlier version of my macro?

    You're not the only ignorant one. I was unsure of how you felt about your coding/reading skills. I mean that in the nicest way. When two (or more) people start communicating on a topic, there's a period of adjustment as we discover each other's level of understanding.


    You could look through your macro and locate the places where are defined/described (1) the INI file name (2) the section name (3) the key name for the maximum number of entries etc and place a call to my function using those values.

    For an example of a call to my function, look at the embedded TEST SUBroutine in the foot of my function, one line of which I reproduce below:


    MsgBox intRemoveKey("d:20010330script2k.ini", "series", "series", "numseries", 3)


    I have just pulled these two lines from your supplied BAS file:
    <pre>INIFile$ = WordBasic.[GetDocumentVar$]("inifile")
    Section$ = "Series"
    </pre>


    so somewhere below this you'ld have a call that (partially completed) looks like this:

    MsgBox intRemoveKey(INIFile$, Section$, etc. etc. etc.



    Be warned: If you want to use my strGP/strPP functions (and I think they're pretty good and I use them all the time) you'll need to search for them in the VB/VBA forum and obtain them and their associated support functions. It's not complicated.

    Or as I suggested before, you could roll-your-own. Those "GetPrivateProfileInt" chunks ought to be embedded in neat little cover functions, IMNSHO (In My No So Humble Opinion).


    Do I still get the button and the paperback?

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: INI File Manipulation Mess

    Send us a toughie next time.

    Here's a solution using my INI file management routines strGP and strPP functions posted some weeks ago. My strGP/strPP are cover functions for accessing INI files. You could write your own strGP/strPP and access the registry. I have not tested this algoritm (below) for use via the registry.

    <pre>Public Function intRemoveKey(strFile As String,_
    strSection As String, _
    strKey As String, _
    strKeyMax As String, _
    intKeyRemove As Integer) As Integer
    ' Procedure : intRemoveKey
    ' Description: Remove a key from the INI file.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING file name of ini file
    ' STRING section name within ini file
    ' STRING key name within section
    ' STRING name of key containing maximum key number
    ' INTEGER key to be deleted.
    ' Returns: A variant array of strings.
    ' Assumes: None.
    ' Side Effects: None.
    ' Tested: By the calls shown below.

    ' Method (an approach which lends its components to other appliactions)
    ' (1) Load an internal array with all relevant keys from the INI file.
    ' (2) Filter the nominated key during loading.
    ' (3) Write the array back to the INI file.

    ' String array to hold the keys and values from the INI file.
    Dim strArrayKey() As String
    ReDim strArrayKey(0)


    ' (1) Load an internal array with all relevant keys from the INI file.
    Dim intMaxKeys As Integer ' Obtain the current limit of keys
    intMaxKeys = Val(strGp(strFile, strSection, strKeyMax, 0))
    Dim intKey As Integer ' Loop to load the array of keys
    For intKey = 0 To intMaxKeys
    ' (2) Filter the nominated key during loading.
    If intKey <> intKeyRemove Then ' load the key to the array
    strArrayKey(UBound(strArrayKey)) = strGp(strFile,_
    strSection, strKey & Trim(str(intKey)), "")
    ReDim Preserve strArrayKey(UBound(strArrayKey) + 1)
    Else ' ignore the key
    End If
    Next intKey
    ReDim Preserve strArrayKey(UBound(strArrayKey) - 1)

    ' (3) Write the array back to the INI file.
    For intKey = 0 To intMaxKeys - 1
    Call strPP(strFile, strSection, strKey & Trim(str(intKey)),_
    strArrayKey(intKey))
    Next intKey
    Call strPP(strFile, strSection, strKeyMax, UBound(strArrayKey))
    'Sub TESTintRemoveKey()
    'MsgBox intRemoveKey("d:20010330script2k.ini", "series", "series",_
    "numseries", 3)
    'End Sub
    End Function
    </pre>



    I have tested this, and can think of several improvments, but have held off until your comments arrive. I can make the code more robust and add features.

    You can obtain my mailing address (for the free T-shirt, beer-mug, shrink-wrapped copies of VB6 etc from my home page, a link to which is conveniently provided at the foot of this post.


    Oh yeah. One last thing. Don't breathe a WORD of this outside of Woody's Lounge. I have a reputation and it will suffer horribly if those who worship me discover that I've been dabbling in Comic Books (grin!)

  7. #7
    sgerber
    Guest

    Re: INI File Manipulation Mess

    >> You're not the only ignorant one. I was unsure of how you felt about your coding/reading skills. I mean that in the nicest way. When two (or more) people start communicating on a topic, there's a period of adjustment as we discover each other's level of understanding. <<

    Put it this way: I was a lot better at faking it with WordBasic than I am at faking it with VBA.

    >> Do I still get the button and the paperback? <<

    Uhm, let's not get ahead of ourselves here.

    >> Be warned: If you want to use my strGP/strPP functions (and I think they're pretty good and I use them all the time) you'll need to search for them in the VB/VBA forum and obtain them and their associated support functions. It's not complicated. <<

    I found strGP and strPP easily enough and copied them into a VBA module, as you suggested in the accompanying messages.

    Another function, though, strBreakFileString, is necessary to run intRemoveKey, and I can't seem to get that one off the forum in a useable format. (You really should attach these things as .bas files, as well as posting the code in the message body.)

    I don't mind having to piece things together and learn more about VBA in order to solve the problem. (It builds character.) But it is a solution I'm looking for, and I haven't got that yet.

  8. #8
    sgerber
    Guest

    Re: INI File Manipulation Mess

    Chris,

    FYI: I had the same problem trying to copy strPPS out of the forum message.

  9. #9
    sgerber
    Guest

    Re: INI File Manipulation Mess

    Cute signature.

    Thanks much for the template. Now let me see if I can make these work -- or at least get back to you with some very specific questions.

    I'm already picking up a lot about the concept of modular coding from seeing how these functions can be used.

  10. #10
    sgerber
    Guest

    Re: INI File Manipulation Mess

    Chris,

    Okay -- specific question #1.

    The various functions call for "inputs." Do I assume correctly, then, that what I have to do is create a form that sends the input data to the appropriate "test" subroutine?

    (Incidentally, speaking as someone who once co-wrote a "Dummies" book, one thing you might do to make the code more understandable to VBA neophytes like myself is specify in your comments section which variable should correspond to which input data -- e.g., "strFileVal: name of INI file".)

    --------------------------------

    UPDATE: Some hours later...

    Ol' Doofus Prime here has begun to catch on a little. I've figured out what inputs are needed and how to specify them (at least for test purposes), and I'm beginning to understand how to use the various functions.

    I'll play with these for a while longer and then probably get back to you with some quasi-intelligent questions.

    --------------------------------

    FURTHER UPDATE: Many hours later...

    Okay, I see now what each of the functions does.

    I'm curious about a couple of things.

    Do I need to create a strDigitsOnly function (complementary to the strAlphaOnly one) in order to assemble a complete keyname for intKeyRemove to delete? I've figured out how to do that; I just want to know if I'm on the right track here.

    Also, running intKeyRemove, I noticed a bit of odd behavior.

    I modified the test routine to run as follows:

    -----------------------------------------

    Sub TESTintRemoveKey()
    Dim strFile As String
    Dim strSection As String
    Dim strKey As String
    Dim strKeyMax As String

    strFile = Options.DefaultFilePath(wdUserTemplatesPath) + "script2k.ini"
    strSection = "Series"
    strKey = "Series"
    intKeyRemove = 3
    strKeyMax = "Numseries"

    Msgbox intRemoveKey(strFile, strSection, strKey, strKeyMax, 3)
    End Sub

    -----------------------------------------

    The "Series" section looked like this before running the procedure:

    [Series]
    NumSeries=9
    Series0=Nevada
    Series1=Slam Punjab
    Series2=Last Son of Earth
    Series3=Ace Brazil
    Series4=Magda Queen of Gnats
    Series5=Zibfrew of the Police
    Series6=Bell Jar Babes
    Series7=Wugada the Enormous
    Series8=Penny Peony

    This is what it looked like after:

    [Series]
    NumSeries=8
    Series0=Nevada
    Series1=Slam Punjab
    Series2=Last Son of Earth
    Series3=Magda Queen of Gnats
    Series4=Zibfrew of the Police
    Series5=Bell Jar Babes
    Series6=Wugada the Enormous
    Series7=Penny Peony
    Series8=
    series9=

    The original Series3 key was deleted and the remaining keys, renumbered -- but the process left behind a blank Series8 key and added a blank Series9 key that never existed before. Any idea what's going on there?

    FYI: The reason this function had me utterly baffled was that I didn't realize the strKey variable didn't include the number of the series. I assume the reason for separating "Series3" into a string and an integer has to do with the renumbering process...?

    One other thing before I forget: I also tried another variation on the test routine:

    Sub TESTintRemoveKey()
    Dim strFile As String
    Dim strSection As String
    Dim strKey As String
    Dim strKeyMax As String
    Dim intKeyRemove As Integer

    strFile = Options.DefaultFilePath(wdUserTemplatesPath) + "script2k.ini"
    strSection = "Series"
    strKey = "Series"
    intKeyRemove = 3
    strKeyMax = "Numseries"

    Msgbox intRemoveKey(strFile, strSection, strKey, strKeyMax, intKeyRemove)
    End Sub

    This one did nothing to the INI file, and I can't figure out why.

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: INI File Manipulation Mess

    How are you with the later post in which I attached a stripped-down template with all the basics?

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: INI File Manipulation Mess

    >The various functions call for "inputs."

    No. Those comment lines at the head of the procedure are to remind me in a general way of the parameters required by the function. For example, the function "strAlphaOnly" takes as a parameter "' Inputs: A data string.", i.e. some string of characters. If you look at the TESTstrAlphaOnly function, you'll see some examples of the strings being passed to the function.


    >which variable should correspond to which input data -- e.g., "strFileVal: name of INI file".

    Thank you for this. You are quite right. I tend not do do that with functions that have two or less parameters, and then I forget to do it with functions that have more.

    >Do I need to create a strDigitsOnly function

    Yes. Oh great caught-on-one. A simple exercise since you already have strAlphaOnly. When done you could serach the VB/VBA lounge ("search" at top of your screen) for "strdigitsonly" I just posted it.

    > but the process left behind a blank Series8 key

    Yup. Gonna happend. I did not write this version for absolute cleanliness. Since you already have a "Number" key, I don't need to do housekeeping.

    If you didn't want blank keys, I have at least two strategies (1) have me read the INI file as a plain text file and physically remove the key entry or (2) use but one key for the entries, and build the key as a set of delimited strings (I have utility functions that assemble and split strings) so that the key would look like this:

    Series=Nevada, Slam Punjab, Last Son of Earth, Ace Brazil, Magda Queen of Gnats


    >This one did nothing to the INI file, and I can't figure out why.

    Er. Ahem. Did you close and reopen Notepad while running the test? (How do I phrase this delicately so as not to miss out on the beer, mug, the unwrapped lady, the wet t-shirt, ...).


    I open the INI file in Notepad and inspect it. I run the test. I alt-tab back to the INI file. Nothing has changed (because I'm looking at a stale copy of the INI file).


    I open, inspect and close the INI file. I run the test. I open & inspect the INI file and see the changes.





    Thanks for persevering, and for your valued comments.

    I make great use of INI files for the environment of the applications. Don't be waylaid by arguments (good ones, too!) about INI vs. Registry. If you decide to go the registry route later on, it's but a small change to these two functions, and all the code in all your applications remains untouched.

    I'm asking another member of VB/VBA forum to confirm this. (not by actually doing it, but by comparing my code with theirs to see that it's a small change in a cover function).

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: INI File Manipulation Mess

    > create a file called eraseme.ini (not SECTION1.INI or SECTION2.INI) in the template directory, with the following contents.


    ... and by now you've worked this out, I hope.

    The TEST subroutines supplied with each function aim at little more than establishing that a function does SOMETHING. A second use for them is to single-step (in debug mode) through the execution of code to watch the pulleys going around, the pistons going up and down (or down and up for our Aussie readers) etc.

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: INI File Manipulation Mess

    >I get a "Subscript Out of Range" error a

    This too? (or in Latin "Es Too Brtuus force approach?")

    I think I warned (very fine print) in the initial posting that "I have tested this, and can think of several improvments," One of them was to make the routines more robust.


    Since the INI file is available to the user, it could easily become corrupted. Complete code would test to see that there really WERE as many keys as specified and check a whole lot of stuff.

    I was mainly wanting to let you see that there IS a solution. You can use my code ONLY as an idea to draft your own.

  15. #15
    sgerber
    Guest

    Re: INI File Manipulation Mess

    >> but the process left behind a blank Series8 key <<

    >> Yup. Gonna happend. I did not write this version for absolute cleanliness. Since you already have a "Number" key, I don't need to do housekeeping.

    If you didn't want blank keys, I have at least two strategies (1) have me read the INI file as a plain text file and physically remove the key entry or (2) use but one key for the entries, and build the key as a set of delimited strings (I have utility functions that assemble and split strings) so that the key would look like this:

    Series=Nevada, Slam Punjab, Last Son of Earth, Ace Brazil, Magda Queen of Gnats <<

    To be honest, the second approach scares me a little, since I have no idea how to turn a comma-delimited key value into an array, and I make use of the array of series names in several different userforms in the template.

    I guess it doesn't matter if those empty keys exist, apart from the fact that I'm something of a neatness freak. Rather than actually write the code for me, could you give me a hint or two how I could go about eliminating those empty keys? I'd like to see if I could figure out the actual solution myself.

    >>Er. Ahem. Did you close and reopen Notepad while running the test? (How do I phrase this delicately so as not to miss out on the beer, mug, the unwrapped lady, the wet t-shirt, ...).

    I open the INI file in Notepad and inspect it. I run the test. I alt-tab back to the INI file. Nothing has changed (because I'm looking at a stale copy of the INI file). <<

    I swear to you -- I thought I had closed the file and reopened it. That is, after all, how I was checking what the macro did when I fed it the absolute value rather than the variable.

    This morning, though, I ran the macro using the variable, and it worked. Very embarrassing. Maybe my eyes were just too bleary by the end of the day yesterday.

    >> I make great use of INI files for the environment of the applications. Don't be waylaid by arguments (good ones, too!) about INI vs. Registry. If you decide to go the registry route later on, it's but a small change to these two functions, and all the code in all your applications remains untouched. <<

    At least for the time being, I'm not inclined to muck around with the Registry. There probably are some advantages to it, but for me those are outweighed by the potential dangers. I know from bitter experience (unrelated to VBA) what happens when a Registry gets corrupted. I wouldn't want to be responsible for inflicting that kind of grief, even accidentally, on anyone else.

    I'll have more questions on this, I'm sure, but now I want to try to create a userform that will feed the necessary variables to the intKeyRemove function. I'll report back later. (Can you stand the suspense?)

Page 1 of 4 123 ... LastLast

Posting Permissions

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