Dim iFreeFile1 As Integer, iFreeFile2 As Integer, varData As String, s As String
iFreeFile1 = FreeFile
Open App.Path & "\_SettingsDump.dat" For Input As #iFreeFile1
iFreeFile2 = FreeFile
Open sFile For Output As #iFreeFile2
Do While Not EOF(iFreeFile1)
Line Input #iFreeFile1, varData
s = varData ' I'll add the replace later
Print #iFreeFile2, s
Loop
Close #iFreeFile2
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?
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?
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:
Public Sub ExportSettingsFile(sFile As String)
Dim sKey As String, iFreeFile1 As Integer, iFreeFile2 As Integer, varData As String, s As String
sKey = """" & "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\SmartBarXP BETA4.7"""
Open App.Path & "\_SettingsDump.reg" For Input As #iFreeFile1
iFreeFile2 = FreeFile
Open sFile For Output As #iFreeFile2
Do While Not EOF(iFreeFile1)
Line Input #iFreeFile1, varData
s = "a" & varData
Print #iFreeFile2, s
Loop
Close #iFreeFile2
Close #iFreeFile1
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.
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?
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.
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.
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.
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.