Results 1 to 30 of 30

Thread: How to search for a string in a text file

  1. #1

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    How to search for a string in a text file

    Hello again fellow programmers,

    I am looking to search for a string which is called: Hello in a text file, in the location of C:\TextFile.txt and then to change Hello for GoodBye, how am i able to do this problem of mine, quite quickly and also with minimal source coding, also...

    !! Thanks in advance !!
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  2. #2
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: How to search for a string in a text file

    Is something like this what you're looking for?



  3. #3

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Actually I am looking to programmatically do that at Run-Time and also Compile-Time and then not at Design-Time or using the IDE outside my app when it's running. But then I am looking to modify a text string that in all of it's instances in my text file, for like it repeats about more than once inside the text file, then to change them to the same value, which is GoodBye. If that make it clearer for you to understand me, even
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: How to search for a string in a text file

    What about a Split and (Re-)Join?
    Aircode!
    Code:
    Dim ReadFile As String
    Dim WriteFile As String
    'Read Textfile in one go --> ReadFile = complete content of Textfile
    Dim arrTemp() As string
    arrTemp=Split(ReadFile, "Hello")
    WriteFile = Join(arrTemp, "Goodbye")
    'Write textFile back on Disk
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Thanks for the input to my problem, at hand
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  6. #6

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Sorry I am having problems with your source code, in this area of being...
    Code:
    With Main
                aString = .lstMusic.Text
                aString = UCase(Replace(aString, " ", ""))
                bString = aString
                .rtfBox.TextRTF = StrConv(LoadResData(bString, "DOCUMENTS"), vbUnicode)
    
                DoEvents
    
                strBuff = StrConv(LoadResData("PLAYA", "DATA"), vbUnicode)
                Open "C:\zVideo-1234567890.htm" For Output As #1
                    Print #1, strBuff
                Close #1
                
                DoEvents
            
                strBuff = StrConv(LoadResData(bString, "MUSIC"), vbUnicode)
                Open "C:\zVideo-1234567890.dcr" For Output As #1
                    Print #1, strBuff
                Close #1
                
                DoEvents
                
                .Button.Enabled = False
                .Ctrl.PlayEnabled = True
                .Ctrl.PlayEnabled = True
                
                DoEvents
                
                arrTemp = Split(ReadFile, "C:\zVideo-1234567890.htm")
                WriteFile = Join("C:\zVideo-1234567890.htm", "swFrame = & Item1")
                
                DoEvents
                
                .Playa.Navigate2 ("C:\zVideo-1234567890.htm")
    End With
    End Sub
    This the text file, that I wish to edit for my app...
    Code:
    <html>
    <head>
    <title>Ambra Productons Inc.</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <!-- saved from url=(0013)about:internet -->
    <body bgColor="#000000">
    <object classid="clsid:233C1507-6A77-46A4-9443-F871F945D258"
     codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=12,0,0,111" name="Media" width=100% height=100%
     ID=Video VIEWASTEXT>
    <param name=src value="C:\zVideo-1234567890.dcr">
    <param name=swRemote value="swSaveEnabled='true' swVolume='false' swRestart='false' swPausePlay='false' swFastForward='false' swContextMenu='false'">
    <param name=swStretchStyle value=fill>
    <param name=PlayerVersion value=12>
    <PARAM NAME=bgColor VALUE=#000000> <PARAM NAME=name VALUE="C:\zVideo-1234567890.dcr">
    <param name="swFrame" value="1"> 
    <embed src="C:\zVideo-1234567890.dcr" type="application/x-director" width="100%" height="100%" bgColor=#000000 name="C:\zVideo-1234567890.dcr" swLiveConnect=TRUE swRemote="swSaveEnabled='true' swVolume='false' swRestart='false' swPausePlay='false' swFastForward='false' swContextMenu='false'" swStretchStyle=fill PlayerVersion=12 pluginspage="http://www.macromedia.com/shockwave/download/" swframe="1"></embed>
    </object>
    </body>
    </html>
    Last edited by ThEiMp; Jul 7th, 2020 at 05:58 AM.
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: How to search for a string in a text file

    What i can see:
    i don't see anywhere you opening your file "C:\zVideo-1234567890.htm" for Input and reading the contents of it into the Variable "ReadFile"

    arrTemp = Split(ReadFile, "C:\zVideo-1234567890.htm") --> Why htm? your file says "dcr"
    WriteFile = Join("C:\zVideo-1234567890.htm", "swFrame = & Item1") --> First argument of Join is an array. should be Join(arrTemp, "swFrame = & Item1")

    You have to write the contents of WriteFile back to a file called "C:\zVideo-1234567890.htm"
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to search for a string in a text file

    Well it appears your data is in a res file which you are reading in then writing to a file so you do not need to and should not be searching a file or replacing something in a file but rather you should be replacing in your variable before you write it to file in the first place.

    You should not be putting files into the root of C drive. You should not have doevents throughout your code.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to search for a string in a text file

    A couple suggestions, if they apply.

    My assumptions first
    1. It appears your listbox contains your resource file IDs
    2. It looks like you want to extract the music file from your resource and save to disk as its own file vs. using just one file and overwriting it.

    If above is correct...
    Edited: The idea is to write 2 files to disk, one .htm and one .dcr. These files have the same name except for the extension. The file name used is that of your resource ID. Ok, got that? Now all you need is to replace the hard-coded file within your "PLAYA" file, before saving to disk, with the new dcr file name.
    Code:
    Dim sFile As String, aData() As Byte
    
               ...
               bString = aString
               ...
    
               sFile = "C:\zVideo-" & bString
    
               strBuff = StrConv(LoadResData("PLAYA", "DATA"), vbUnicode)
               strBuff = Replace(strBuff, "C:\zVideo-1234567890.dcr", sFile & ".dcr")
               Open sFile & ".htm" For Output As #1
                    Print #1, strBuff
                Close #1
                
                aData() = LoadResData(bString, "MUSIC")
                Open sFile & ".dcr" For Binary As #2
                    Put #2, 1, aData()
                Close #2
                Erase aData()
                
                .Button.Enabled = False
                .Ctrl.PlayEnabled = True
                .Ctrl.PlayEnabled = True
                
                .Playa.Navigate2 sFile & ".htm"
    As mentioned already, never save to the C: root. That's ok for testing, but if your project was run on someone else's PC, they may not have permissions to write to that root. Notice I use a byte array for extracting your music file from your resources. Avoid extracting binary data into a VB string that may require ANSI > Unicode conversion. When data is binary, extract to byte array & write the byte array. You can avoid potential of bytes being changed during that conversion.
    Last edited by LaVolpe; Jul 7th, 2020 at 03:03 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Thanks I will try the source code, when I get into work, today
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  11. #11
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: How to search for a string in a text file

    Quote Originally Posted by DataMiser View Post
    You should not be putting files into the root of C drive. You should not have doevents throughout your code.
    Playing Monopoly much?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  12. #12

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    yes I know, I have to come up with a cache folder, for my objects to run around inside of
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  13. #13
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by ThEiMp View Post
    yes I know, I have to come up with a cache folder, for my objects to run around inside of
    Do you mean a temporary folder such as the one in your user profile or Windows system directory?

  14. #14

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Well a Windows Profile Temp Folder, eg: C:\Users\WinProfile\Temp\Video1234567890.tmp, etc I guess so, then would be the correct type of thing to go around on, hey
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  15. #15
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by ThEiMp View Post
    Well a Windows Profile Temp Folder, eg: C:\Users\WinProfile\Temp\Video1234567890.tmp, etc I guess so, then would be the correct type of thing to go around on, hey
    Great. Anything else you need help with?

  16. #16

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Actually how do I decipher the local current user, which is either remote or local profile, then
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  17. #17
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by ThEiMp View Post
    Actually how do I decipher the local current user, which is either remote or local profile, then
    Do you mean retrieve the username?

    If so:
    Dim Username As String
    Username = Environ$(“username”)

    The above should work but I haven’t got the opportunity to test it right now.

  18. #18

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Cool thanks, I am working on making a temp folder, which is what I have found the source code to do so, even.
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  19. #19
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by ThEiMp View Post
    Cool thanks, I am working on making a temp folder, which is what I have found the source code to do so, even.
    Great, good to hear.

  20. #20

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    :()+<
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  21. #21
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to search for a string in a text file

    A user profile's "display name" only matches the actual profile name if it has never been renamed. When a new user "Joe" is created they are the same, but if "Joe" ever gets renamed to "Sam" the profile name is still "Joe" even though the user logs on as "Sam."

    So use a Shell call to look up the current profile's Local AppData folder path (using ssfLOCALAPPDATA a.k.a. CSIDL_LOCAL_APPDATA), then add the "Temp" subdirectory to that, then add your app-specific subdirectory below that to avoid collisions.

    Goofy things like using environment variables can only lead to madness... or data corruption or data loss. It isn't 1981 any more.

  22. #22
    New Member
    Join Date
    Jul 2020
    Posts
    1

    Re: How to search for a string in a text file

    Quote Originally Posted by dilettante View Post
    A user profile's "display name" only matches the actual profile name if it has never been renamed. When a new user "Joe" is created they are the same, but if "Joe" ever gets renamed to "Sam" the profile name is still "Joe" even though the user logs on as "Sam."

    So use a Shell call to look up the current profile's Local AppData folder path (using ssfLOCALAPPDATA a.k.a. CSIDL_LOCAL_APPDATA), then add the "Temp" subdirectory to that, then add your app-specific subdirectory below that to avoid collisions.

    Goofy things like using environment variables can only lead to madness... or data corruption or data loss. It isn't 1981 any more.
    Hi, this is my first post. Just read the thread, what would you recommend instead of environment variables for retreiving the username in vb6?

  23. #23
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by dilettante View Post
    Goofy things like using environment variables can only lead to madness... or data corruption or data loss. It isn't 1981 any more.
    Hi dilettante,

    Code:
    Option Explicit
    
    Private Declare Function GetUserNameA Lib "Advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Private Const MAX_STRING As Long = 65535
    
    Private Sub Main()
    Dim Length As Long
    Dim UserName As String
    
       UserName = String$(MAX_STRING, vbNullChar)
       Length = GetUserNameA(UserName, Len(UserName))
       UserName = Left$(UserName, InStr(UserName, vbNullChar) - 1)
       
       MsgBox UserName, vbInformation
    End Sub
    Do you think this is a better method?

    I didn't know environment variables were obsolete for that long, even vb.net still supports them. Why are they so goofy according to you?

    Thank you.

  24. #24
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to search for a string in a text file

    @Vbcoder3, welcome to the forum

    Windows provides quite a few different APIs to get what you may need. For example:

    GetTempPath https://docs.microsoft.com/en-us/win...i-gettemppatha
    GetUserName https://docs.microsoft.com/en-us/win...e-getusernamea
    SHGertKnownFolderPath https://docs.microsoft.com/en-us/win...nownfolderpath
    and so many more
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  25. #25
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by ThEiMp View Post
    Well a Windows Profile Temp Folder, eg: C:\Users\WinProfile\Temp\Video1234567890.tmp, etc I guess so, then would be the correct type of thing to go around on, hey

    ThEiMp:
    Apologies for my rather short replies. Here's what should be the proper code to retreive the path of the temporary file folder and to generate a temporary filename:

    Code:
    Option Explicit
    
    Private Declare Function GetTempFileNameA Lib "Kernel32.dll" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
    Private Declare Function GetTempPathA Lib "Kernel32.dll" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    
    Private Const MAX_PATH As Long = 256
    
    Public Sub Main()
    Dim Length As Long
    Dim UniqueNumber As Long
    Dim TempFile As String
    Dim TempPath As String
    
       TempPath = String$(MAX_PATH, vbNullChar)
       Length = GetTempPathA(Len(TempPath), TempPath)
       If Length > 0 Then TempPath = Left$(TempPath, Length)
    
       TempFile = String$(MAX_PATH, vbNullChar)
       UniqueNumber = GetTempFileNameA(".", "tmp", 0, TempFile)
       If InStr(TempFile, vbNullChar) Then TempFile = Left$(TempFile, InStr(TempFile, vbNullChar))
    
       Debug.Print TempPath
       Debug.Print TempFile
       Debug.Print Hex$(UniqueNumber)
    End Sub
    Is the above useful?
    Last edited by Peter Swinkels; Jul 16th, 2020 at 03:00 AM. Reason: typo

  26. #26
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to search for a string in a text file

    Code:
        Const ssfLOCALAPPDATA = 28
    
        With CreateObject("Shell.Application")
            MsgBox .NameSpace(ssfLOCALAPPDATA).Self.Path & "\Temp\MyAppName"
        End With

  27. #27
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by Vbcoder3 View Post
    Hi, this is my first post. Just read the thread, what would you recommend instead of environment variables for retreiving the username in vb6?
    Hi vbcoder3, welcome to the forum!
    Last edited by Peter Swinkels; Jul 9th, 2020 at 11:04 AM.

  28. #28
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by Peter Swinkels View Post
    Hi dilettante,

    Code:
    Option Explicit
    
    Private Declare Function GetUserNameA Lib "Advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Private Const MAX_STRING As Long = 65535
    
    Private Sub Main()
    Dim Length As Long
    Dim UserName As String
    
       UserName = String$(MAX_STRING, vbNullChar)
       Length = GetUserNameA(UserName, Len(UserName))
       UserName = Left$(UserName, InStr(UserName, vbNullChar) - 1)
       
       MsgBox UserName, vbInformation
    End Sub
    Do you think this is a better method?

    I didn't know environment variables were obsolete for that long, even vb.net still supports them. Why are they so goofy according to you?

    Thank you.
    To answer my own question: StackOverflow offers a good reason for not retrieving usernames from environment variables, namely the fact a user can modify them and pass them on to a program. The method suggested there is:

    Code:
    Dim Network As WshNetwork
    Set Network = New WshNetwork
    
    Debug.Print "ComputerName: " & Network.ComputerName
    Debug.Print "UserDomain: " & Network.UserDomain
    Debug.Print "UserName: " & Network.UserName
    link:
    https://stackoverflow.com/questions/...-on-windows-xp

  29. #29

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: How to search for a string in a text file

    Thanks everyone for their input
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  30. #30
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to search for a string in a text file

    Quote Originally Posted by ThEiMp View Post
    Thanks everyone for their input
    You’re welcome!

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