Manipulating Strings

Author Message
Bushmen

  • Total Posts : 122
  • Scores: 0
  • Reward points : 0
  • Joined: 2/4/2005
  • Location:
  • Status: offline
Manipulating Strings Friday, March 04, 2005 5:42 AM (permalink)
0
Guys,

I am manipulating a registry key (string)

Basically the key is in the form:

"AppDir1;AppDir2"

I have a script that adds my new dir to the end of the line (say its called InstDir)
every time when my application is installed.

"AppDir1;AppDir2;InstDir;"

On uninstall, I want to be able to delete the InstDir, baring in mind that other applications might have added their own InstDir's at the end of the line. The actual string doesn't need a ";" as a very last character to function, so my possible scenarios could be:

1. "AppDir1;AppDir2;InstDir;"
2. "AppDir1;AppDir2;InstDir"
3. "AppDir1;AppDir2;InstDir;AppDir3"
4. "AppDir1;AppDir2;InstDir;AppDir3;"
5. "AppDir1;AppDir2;InstDir;AppDir3;AppDir4"
6. "AppDir1;AppDir2;InstDir;AppDir3;AppDir4;"

I want to be able to search the string then
if it finds InstDir & ";"
remove InstDir & ";"
else if finds InstDir with no ";" at the end
remove InstDir
else
it probably didn't find it
so do nothing
end
end

If anyone has any suggestions, it would be much appreciated.
I have a similar post before, but i was adding to the beginning of the string, so you
would always know where to find it. This has now changed and i need to add it to the end of the string.

Therefore, to take it out of the string again (on application uninstall) is a bit more complicated.

Regards,
Bushmen
Scripting is Fab!!
 
#1
    mbouchard

    • Total Posts : 2110
    • Scores: 29
    • Reward points : 0
    • Joined: 5/15/2003
    • Location: USA
    • Status: offline
    Re: Manipulating Strings Friday, March 04, 2005 6:01 AM (permalink)
    0
    Probably the simplest way would be to do this

    TheString = regread...
    InstDir = TheDirectory

    TheString = Replace(TheString,InstDir,"")
    TheString = Replace(TheString,";;",";")

     
    #2
      Bushmen

      • Total Posts : 122
      • Scores: 0
      • Reward points : 0
      • Joined: 2/4/2005
      • Location:
      • Status: offline
      Re: Manipulating Strings Friday, March 04, 2005 6:34 AM (permalink)
      0
      hi mbouchard,

      hmm.. i can see what you're saying.. yeah, that would work.

      One question though, from a "propper" scripting point of view,
      and also because i am still learning regarding string manipulation,
      what other way could i use?

      I thought of doing a revers instring, to the end of instdir,
      then if the next character is a ";", delete it with the instdir..

      is that too complicated... i guess, making a script as less complicated as possible is the way to go.. is it?/?

      Bushmen
       
      #3
        mbouchard

        • Total Posts : 2110
        • Scores: 29
        • Reward points : 0
        • Joined: 5/15/2003
        • Location: USA
        • Status: offline
        Re: Manipulating Strings Friday, March 04, 2005 7:51 AM (permalink)
        0
        I like posts like this, makes me look at different ways of doing things. What you could do is take the string, make an array using Split, then check for the InstDir and skip that while putting together a new string. This might not be the cleanest as I do not use array's too much, but here is an example:

        MyString = "AppDir1;AppDir2;InstDir;AppDir3;AppDir4;"
        MyArray = Split(MyString, ";", -1, 1)
        
        For j = 0 To UBound(MyArray)
        'Do what you want here.
        If NOT (MyArray(j) = "" OR MyArray(j) = "InstDir") then
        MyNewString = MyNewString & MyArray(j) & ";"
        End If
        Next
        
        msgbox MyNewString
        
         
        #4
          token

          • Total Posts : 1917
          • Scores: 0
          • Reward points : 0
          • Joined: 1/14/2005
          • Location:
          • Status: offline
          Re: Manipulating Strings Friday, March 04, 2005 11:07 AM (permalink)
          0
          I would suggest you to eliminate any ";" at the beginning or the end of the string to minimize problems when the application is added. Instead of adding "appdir;", it could be ";appdir".

          ====================================================================
          Option Explicit
          Dim temp
          temp = "AppDir1;AppDir2;InstDir;AppDir3;AppDir4;"
          WScript.Echo temp & vbcrlf
          WScript.Echo strDel(temp,"appdir1",";")
          WScript.Echo strDel(temp,"appdir2",";")
          WScript.Echo strDel(temp,"instdir",";")
          WScript.Echo strDel(temp,"appdir3",";")
          WScript.Echo strDel(temp,"appdir4",";")

          Function strDel(src, str, delim)
          Dim key, temp
          For Each key In Split(src,delim)
          If UCase(key) = UCase(str) Then
          temp = Replace(Replace(src,key,""),String(2,delim),delim)
          Exit For
          End If
          Next
          If Left(temp,1) = delim Then
          temp = Right(temp,Len(temp)-1)
          End If
          If Right(temp,1) = delim Then
          temp = Left(temp,Len(temp)-1)
          End If
          strDel = temp
          End Function
           
          #5
            Bushmen

            • Total Posts : 122
            • Scores: 0
            • Reward points : 0
            • Joined: 2/4/2005
            • Location:
            • Status: offline
            Re: Manipulating Strings Friday, March 04, 2005 11:46 AM (permalink)
            0
            mbouchard and token

            once again you guys have done a great job..

            I like both mbouchard options, as well as token's way.

            I am not sure which one to use yet. These forums are a great learning curve though.

            Token,
            Can you please explain to me how the
            "For each key in split(...)2 works?

            Bushmen
             
            #6
              token

              • Total Posts : 1917
              • Scores: 0
              • Reward points : 0
              • Joined: 1/14/2005
              • Location:
              • Status: offline
              Re: Manipulating Strings Friday, March 04, 2005 12:15 PM (permalink)
              0
              As you know, split returns an array of string and to access the elements of an array, one could use one of the two methods.

              First of which is to use an index to enumerate through all the elements in an array. This one has the benefits of allowing you access a specific item if the location (index) of the item wanted is known ahead of time. However, this also requires the use of additional variables (the index).

              Or you can choose to use the second method in teh form of FOR EACH <something> in <ARRAY>. This works the same as the dictionary object which I'm sure you should be familiar with by now :) If not, you could always check the scripts for that ports.txt thingy. When used, <something> returns the item in an array one at a time during each enumeration within the FOR EACH loop.

              Which one is better ? No idea. I don't really like the word "better". I truly believe that everything and anything has its pros and cons, depending on what your goals are and what you wish to obtain during which time your goals are achieved.

               
              #7
                Bushmen

                • Total Posts : 122
                • Scores: 0
                • Reward points : 0
                • Joined: 2/4/2005
                • Location:
                • Status: offline
                Re: Manipulating Strings Sunday, March 06, 2005 9:51 PM (permalink)
                0
                Token,

                yeah, i've been looking at the ports script as well as these, hence me asking about the For each key...

                Thanks for explaining, its all coming together..

                Just one more question regarding this:

                Is the syntax "For each key in", or could you use anything, like,
                "For each SplitValue in" or
                "For each Value in"

                Bushmen
                 
                #8
                  Zifter

                  • Total Posts : 315
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 1/5/2005
                  • Location: Belgium
                  • Status: offline
                  Re: Manipulating Strings Sunday, March 06, 2005 10:39 PM (permalink)
                  0
                  You can do it without arrays aswell:

                  If Not InStr(MyString,"InstDir") = 0 Then MyString = Mid(MyString,1,InStr(MyString,"InstDir")-1) & _ Mid(MyString,InStr(MyString,"InstDir")+8) End If


                  The syntax of the "For Each" loop is:
                  For Each <variable name> In <collection/array name>
                  ...
                  Next
                  So you may choose if you use "key" or "SplitValue" or whatever you want


                  HTH
                   
                  #9
                    Bushmen

                    • Total Posts : 122
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 2/4/2005
                    • Location:
                    • Status: offline
                    Re: Manipulating Strings Monday, March 07, 2005 1:43 AM (permalink)
                    0
                    Thanks for the reply Zifter.

                    Token,

                    One more question, after looking at your function, i decided i might as well make my adding to the string on install in the first place a function too...

                    Basically, on application install, I add the InstallDir to the end of the string.

                    what do you think of this:

                    '=======================================================================
                    Option Explicit

                    Dim ExistingData, InstallDir
                    ExistingData = "AppDir1;AppDir2;AppDir3;AppDir4"
                    InstallDir = "C:\Prog\Test\Test01"

                    WScript.Echo ExistingData & vbcrlf
                    WScript.Echo strAdd(ExistingData,InstallDir,";")

                    Function strAdd(src,str,delim)

                    Dim ExistingData

                    If Left(src,1) = delim Then
                    src = Right(src,Len(src)-1)
                    End If

                    If Right(src,1) = delim Then
                    src = Left(src,Len(src)-1)
                    End If

                    strAdd = src & delim & InstallDir
                    End Function
                    '========================================================================

                    Bushmen
                     
                    #10
                      token

                      • Total Posts : 1917
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 1/14/2005
                      • Location:
                      • Status: offline
                      Re: Manipulating Strings Monday, March 07, 2005 7:51 AM (permalink)
                      0
                      Adding is rather easy since we only need to be concerned with 2 possible case. Below is the modification I made; it seemd to me you are once again making it difficult than it should be.. hehe :)
                      ===================================================================
                      Option Explicit

                      Dim ExistingData, InstallDir
                      ExistingData = "AppDir1;AppDir2"
                      InstallDir = "C:\Prog\Test\Test01"

                      WScript.Echo ExistingData & vbcrlf
                      WScript.Echo strAdd(ExistingData,InstallDir,";")

                      Function strAdd(src,str,delim)
                      If src = "" Then
                      strAdd = str
                      Else
                      strAdd = src & delim & str
                      End If
                      End Function
                       
                      #11
                        Bushmen

                        • Total Posts : 122
                        • Scores: 0
                        • Reward points : 0
                        • Joined: 2/4/2005
                        • Location:
                        • Status: offline
                        Re: Manipulating Strings Monday, March 07, 2005 11:28 AM (permalink)
                        0
                        thanks for that token,

                        i still think we need to cater for if the data was

                        ExistingData = "AppDir1;AppDir2;"

                        instead of

                        ExistingData = "AppDir1;AppDir2"

                        then we need to check for the extra delim, and not add it again, isn't it?

                        regards,

                        bushmen
                         
                        #12
                          token

                          • Total Posts : 1917
                          • Scores: 0
                          • Reward points : 0
                          • Joined: 1/14/2005
                          • Location:
                          • Status: offline
                          Re: Manipulating Strings Monday, March 07, 2005 12:20 PM (permalink)
                          0
                          ";" will never be found at the end of the string according to the code I posted. :)

                          Try it and see.
                           
                          #13
                            Bushmen

                            • Total Posts : 122
                            • Scores: 0
                            • Reward points : 0
                            • Joined: 2/4/2005
                            • Location:
                            • Status: offline
                            Re: Manipulating Strings Monday, March 07, 2005 11:14 PM (permalink)
                            0
                            token

                            i know what you're saying,

                            although, the strDelete function runs second

                            First on application install, we add the InstallDir
                            Then on application uninstall, we delete the InstallDir

                            Also, say another application added their own installdir with the delim, we need to be able to be aware of it, hence me saying we need to do the extra check.

                            Thanks again for your help on this.

                            Regards,

                            Bushmen
                             
                            #14
                              token

                              • Total Posts : 1917
                              • Scores: 0
                              • Reward points : 0
                              • Joined: 1/14/2005
                              • Location:
                              • Status: offline
                              Re: Manipulating Strings Tuesday, March 08, 2005 8:49 AM (permalink)
                              0
                              hm... Sorry mate, I don't get what you saying. Are you saying that the code I posted will break under some situations ?

                              When an application is installed, it will determine whether the string is empty or not. If so, it will just place the "InstallDir" into the string and nothing else. If however, the string is not empty, it will then append ";" followed by "InstallDir". When the 3rd (or whichever) application is installed, it will append another ";" followed by "InstallDir". There will never be a ";" at the end of the string (or at least it shouldn't) after during installation.

                              During uninstallation, it will first get rid of the matching "InstallDir" and it will then check to see if ";;" exists. If so, it will be changed to a single ";". Single ";" within the string itself is alright as long as they don't appear either at the start (when deleting the first token in the string) or the end (when deleting the last token in the string) of the string. If ";" is found to be at either the start or the end of the string, they are removed.

                              I don't see where else we need to add extra code to check for the delimiter. Maybe I missed what you saying though :\
                               
                              #15
                                Bushmen

                                • Total Posts : 122
                                • Scores: 0
                                • Reward points : 0
                                • Joined: 2/4/2005
                                • Location:
                                • Status: offline
                                Re: Manipulating Strings Tuesday, March 08, 2005 11:17 AM (permalink)
                                0
                                token,

                                you are right what you're saying, except:

                                i am updating the devicepath registry key, which tells the computer where to look for drivers.

                                now, if it was only my applications updating that key, i would agree with you, but various vendor applications updates that key, and I can just not be sure that there won't be a ";" at the end of the string, since the registry key is still valid with an ";" at the end.

                                probably just me being over cautious, but rather safe than sorry, since that is quite an importand registry key and i wouldn't want something to go wrong with it.

                                But, it is a small thing, and i've made a small amendment to you code to cater for that.

                                All in All, it works great and I am very happy with it. Thanks for all your help on this.

                                Bushmen
                                 
                                #16
                                  token

                                  • Total Posts : 1917
                                  • Scores: 0
                                  • Reward points : 0
                                  • Joined: 1/14/2005
                                  • Location:
                                  • Status: offline
                                  Re: Manipulating Strings Tuesday, March 08, 2005 11:50 AM (permalink)
                                  0
                                  ah ha! Gotcha!

                                  You didn't mention (or perhaps I didn't noticed) that there will be appliactions write to that registry with ";" at the end. Yup, and I agree with you. As long as there is a chance of something might go wrong, it is always better to be prepared ahead of time. I just thought you will *conotrol* both the installation and uninstallation process in that the scripts will be used to add/remove those entries so that there shouldn't be any "abnormalies".

                                  Glad you got it all covered :)

                                  and you are welcome :)

                                   
                                  #17

                                    Online Bookmarks Sharing: Share/Bookmark

                                    Jump to:

                                    Current active users

                                    There are 0 members and 1 guests.

                                    Icon Legend and Permission

                                    • New Messages
                                    • No New Messages
                                    • Hot Topic w/ New Messages
                                    • Hot Topic w/o New Messages
                                    • Locked w/ New Messages
                                    • Locked w/o New Messages
                                    • Read Message
                                    • Post New Thread
                                    • Reply to message
                                    • Post New Poll
                                    • Submit Vote
                                    • Post reward post
                                    • Delete my own posts
                                    • Delete my own threads
                                    • Rate post

                                    2000-2012 ASPPlayground.NET Forum Version 3.9