Results 1 to 23 of 23

Thread: Replace text in file

  1. #1

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815

    Unhappy Replace text in file

    Hi,

    I've been trying to get this working:

    VB Code:
    1. Dim iFreeFile1 As Integer, iFreeFile2 As Integer, varData As String, s As String
    2.  
    3. iFreeFile1 = FreeFile
    4. Open App.Path & "\_SettingsDump.dat" For Input As #iFreeFile1
    5. iFreeFile2 = FreeFile
    6. Open sFile For Output As #iFreeFile2
    7. Do While Not EOF(iFreeFile1)
    8.     Line Input #iFreeFile1, varData
    9.     s = varData ' I'll add the replace later
    10.     Print #iFreeFile2, s
    11. Loop
    12. Close #iFreeFile2
    13. Close #iFreeFile1

    I want to open a file, replace all instances of a string with another, and save it to another file. I wrote the above code, but the resultant file was just full of rubbish (loads of those square character things). So I took the replace function out and it still produces the same thing.

    If I replace the Print #iFreeFile2, s line with Print #iFreeFile2, "a" & s then it works fine, only has the "a" at the start of each line.

    I cant see why this shouldnt work, and why adding a character for the Print function suddenly makes it work. What am I doing wrong?
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    yes that is wierd.. hmmm....

    maybe it's a problem with the source file? try reading the whole thing at once, THEN split it into different lines if you must:

    Wholefile = input$(lof(#iFreeFile1),#iFreeFile1)
    IndividualLines() = split(wholefile,vbcrlf)
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  3. #3
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    Weird indeed; perhaps you can replace the Print statement with: "" & s; it might work then...
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  4. #4

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    TheVader, I tried all different things. It doesnt work if I add null strings to either end. I've tried using Trim$, Cstr and anything I can think of. The only thing that seems to work is if I add characters to the start or end of each line.

    BuggyProgrammer, the size of the file will vary. I'm looking for a way to just replace all instances of a string in a text file with something else and end up with another file. I tried your method before using Line Input, and I got an error (input past end of file), so I thought it may have been because the file was too big, but maybe it wasnt.

    Hmm, anyway, I need a function to replace text in a text file that could be quite large. Is there another way to go about doing this?
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  5. #5
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    maybe the file is a binary file. have you tried opening it in notepad and seeing what it lookes like?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  6. #6

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Its a .reg file from regedit.
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    When I do this it works perfectly.

    VB Code:
    1. Dim iFreeFile1 As Integer, iFreeFile2 As Integer, varData As String, s As String
    2.  
    3. iFreeFile1 = FreeFile
    4. Open "C:\temp\vbctrls.reg" For Input As #iFreeFile1
    5. iFreeFile2 = FreeFile
    6. Open "C:\temp\xxx.reg" For Output As #iFreeFile2
    7. Do While Not EOF(iFreeFile1)
    8.     Line Input #iFreeFile1, varData
    9.     s = Replace(varData, "key", "xxx")
    10.     Print #iFreeFile2, s
    11. Loop
    12. Close #iFreeFile2
    13. Close #iFreeFile1

  8. #8

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Ok, I apologize, it only works if I add chars before varData. It still comes out as junk if I add something to the end of it (varData & "a" for example).

    MartinLiss, this is my entire function, and it still doesnt work unless I add a char before varData:

    VB Code:
    1. Public Sub ExportSettingsFile(sFile As String)
    2.  
    3.     Dim sKey As String, iFreeFile1 As Integer, iFreeFile2 As Integer, varData As String, s As String
    4.    
    5.     sKey = """" & "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\SmartBarXP BETA4.7"""
    6.     Shell "Regedit /e """ & App.Path & "\_SettingsDump.reg"" " & sKey, vbHide
    7.    
    8.    
    9.     iFreeFile1 = FreeFile
    10.     Open App.Path & "\_SettingsDump.reg" For Input As #iFreeFile1
    11.     iFreeFile2 = FreeFile
    12.     Open sFile For Output As #iFreeFile2
    13.     Do While Not EOF(iFreeFile1)
    14.         Line Input #iFreeFile1, varData
    15.         s = "a" & varData
    16.         Print #iFreeFile2, s
    17.     Loop
    18.     Close #iFreeFile2
    19.     Close #iFreeFile1
    20.  
    21.     Kill App.Path & "\_SettingsDump.reg"


    I tried changing the extension of the "temp" file I'm using to reg instead of dat, and it doesnt solve the problem, and now my code looks exactly like yours.
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  9. #9

  10. #10

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Sure, its attached.

    Thanks
    Attached Files Attached Files
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  11. #11

  12. #12

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Ok, well what I'm trying to do is make a function that puts all the reg values from a key and its sub keys into a file, and another function that will load the file up again.

    I was using this regedit method fine until I came across the problem that the files will only import to the same version that they were exported from. I use different reg keys for different versions of my app so users can use older versions if they want to without problems arising.

    I just wanted to have my export function replace all the key names in the exported file ("SmartBarXP BETA4.7" in this case) with something like "$appkeyname$", and then replace this with the correct key name for the import function, so it will work fine with future releases.

    Is there another way to do what I want? How can I export and import reg settings to and from a file without using regedit?
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  13. #13

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    *bump*
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  14. #14
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    I downloaded your sample file and used a hex editor on it. Your problem is that the file is NOT a text file. You will need read it in as a binary file to make the changes to it, or have several different files, one for each version that you need so you don't have to edit them. I suggest you download a hex editor so you can actually see the file contents. You won't be able to simply replace text in the file, since the file starts with FF FE and all words have nulls (00) between each actual text character.

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by jdc2000
    I downloaded your sample file and used a hex editor on it. Your problem is that the file is NOT a text file....
    I'm not questioning your statement but how is it that Notepad displays the file as readable text?

  16. #16
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    Notepad won't display the nulls.

  17. #17

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Hmm, weird. Is there a way to convert it to a text file?
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  18. #18
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    If you are planning on using the edited file to import keys back into the registry, you'll have to use a different method if you use a text file. You'll have to check on this, but I think you can export registry entries as .txt files instead of .reg files, and also import them back again. See if you can do that instead.

  19. #19
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    VB Code:
    1. Option Explicit
    2. Private Sub Form_Load()
    3.     Dim bFile() As Byte
    4.     Dim i As Long
    5.     Dim arrLines() As String
    6.    
    7.     Open "D:\a.reg" For Binary As #1
    8.         bFile = Space(LOF(1))
    9.         Get #1, , bFile
    10.     Close #1
    11.    
    12.     arrLines = Split(Mid(CStr(bFile), 2), vbCrLf)
    13.    
    14.     For i = 0 To UBound(arrLines)
    15.        arrLines(i) = Replace(arrLines(i), "$appkeyname$", "My app")
    16.     Next
    17.    
    18.     Debug.Print Join(arrLines, vbCrLf)
    19. End Sub

    hmmm try that

    actually you don't need the split..

    VB Code:
    1. Option Explicit
    2. Private Sub Form_Load()
    3.     Dim bFile() As Byte
    4.     Dim i As Long
    5.     Dim sFile As String
    6.    
    7.     Open "D:\a.reg" For Binary As #1
    8.         bFile = Space(LOF(1))
    9.         Get #1, , bFile
    10.     Close #1
    11.    
    12.     sFile = Mid(CStr(bFile), 2)
    13.     sFile = Replace(sFile, "$appkeyname$", "My app")
    14.    
    15.    
    16.     Debug.Print sFile
    17. End Sub
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  20. #20
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    ummm you should also do a Trim(sFile) to get rid of those spaces at the end.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  21. #21
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    india
    Posts
    418
    hi,

    use replace command.

    Replace(expression, find, replacewith[, start[, count[, compare]]])

  22. #22
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    Unfortunately, the Replace command as shown won't work unless you change the text to search for and the text to replace it to the format that is in the .reg file. That text has nulls separating each character.

    Example:

    OldRegKeyName = "OldAppName"
    NewRegKeyName = "MyAppName"

    Searching for either of these will return a 'not found result', because the actual data in the file is:

    "O" & <NUL> & "l" & <NUL> & "d" & "A" & <NUL> ... etc.

  23. #23

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Thanks a lot for the help on this. I've decided to leave the files as they are and have them work only with the version they were exported from. Although this does take away their primary purpose, I've decided to leave it at that and write my own function that enumerates all the reg values and exports/imports them myself. I dont have the time to overcome the problems with this method for the next release of my program.

    I'm going to have a think about it and sort it out at a later date.

    Thanks again for the help.
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

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