|
Post by tanger32au on Dec 18, 2018 0:39:12 GMT
Is it possible to open a text file, replace all the letters A with B and then save the file?
If so what would the code for that look like?
|
|
|
Post by B+ on Dec 18, 2018 1:17:35 GMT
' Replace A to B code file.txt for JB v2 B+ 2018-12-17 ReplaceFile$ = "Replace A to B.bak" open ReplaceFile$ for input as #1 open "Replaced A's with B's.txt" for output as #2 while eof(#1) = 0 line input #1, fline$ b$ = "" for i = 1 to len(fline$) if upper$(mid$(fline$, i, 1)) = "A" then b$ = b$ + "B" else b$ = b$ + mid$(fline$, i, 1) next print #2, b$ wend close #1 close #2 print "File work is done."
"Replace A to B.bak"
"Replaced A's with B's.txt"
Edit: oops, originally did more than asked.
|
|
|
Post by B+ on Dec 18, 2018 1:45:36 GMT
A 2nd way to get same results:
' Replace A to B code file #2.txt for JB v2 B+ 2018-12-17 ReplaceFile$ = "Replace A to B.txt" open ReplaceFile$ for input as #1 while eof(#1) = 0 line input #1, fline$ b$ = "" for i = 1 to len(fline$) if upper$(mid$(fline$, i, 1)) = "A" then b$ = b$ + "B" else b$ = b$ + mid$(fline$, i, 1) next if file$ = "" then file$ = b$ else file$ = file$ + CHR$(13) + CHR$(10) + b$ wend close #1
open "Replaced A's with B's.txt" for output as #2 print #2, file$ close #2
print "File work is done."
|
|
|
Post by tanger32au on Dec 18, 2018 2:38:42 GMT
Thanks for that. I now want to remove all double spaces, it does not seem to work for that?
|
|
|
Post by B+ on Dec 18, 2018 2:59:36 GMT
Do you mean remove double spaced lines?
If you mean double spaces, what if there are 3 spaces, OK? And do you want to replace double spaces with a single space?
|
|
|
Post by B+ on Dec 18, 2018 3:11:19 GMT
Here is third way to replace all A's with B's with Binary access:
' Replace A to B code file #3.txt for JB v2 B+ 2018-12-17
open "Binary Replace A with B.txt" for binary as #1 for i = 0 to lof(#1) - 1 seek #1, i f$ = input$(#1, 1) if upper$(f$) = "A" then seek #1, i print #1, "B" end if next close #1 print "File work is done."
Took me awhile to get that right with: f$ = input$(#1, 1)
You can scan a file character by character like a line with MID$.
|
|
|
Post by tanger32au on Dec 18, 2018 3:15:31 GMT
Do you mean remove double spaced lines? If you mean double spaces, what if there are 3 spaces, OK? And do you want to replace double spaces with a single space? I would like to remove all double spaces and replace with nothing. At the moment I am opening the text file in notepad manually and doing this but would love to automate it.
|
|
|
Post by tanger32au on Dec 18, 2018 3:18:55 GMT
Here is third way to replace all A's with B's with Binary access: ' Replace A to B code file #3.txt for JB v2 B+ 2018-12-17
open "Binary Replace A with B.txt" for binary as #1 for i = 0 to lof(#1) - 1 seek #1, i f$ = input$(#1, 1) if upper$(f$) = "A" then seek #1, i print #1, "B" end if next close #1 print "File work is done."
Took me awhile to get that right with: f$ = input$(#1, 1) You can scan a file character by character like a line with MID$. Thanks, should that be saving the changes back to the file? It does not seem to be.
|
|
|
Post by B+ on Dec 18, 2018 3:43:34 GMT
With Binary access, you can change the file character by character because you can read and write, both, when the file is opened.
Here is the part that writes:
if upper$(f$) = "A" then seek #1, i print #1, "B" end if
|
|
|
Post by B+ on Dec 18, 2018 3:57:20 GMT
Double Space Vacuum Cleaner:
' doubleSpaceVacuumCleaner test.txt for JB v2 B+ 2018-12-17
test$ = " Th e rai n i n S p a in f alls mai nl y on t he pl ai n ." print doubleSpaceVacuumCleaner$(test$)
function doubleSpaceVacuumCleaner$(text$) doubleSpace = instr(text$, " ") while doubleSpace text$ = mid$(text$, 1, doubleSpace - 1) + mid$(text$, doubleSpace + 2) doubleSpace = instr(text$, " ") wend doubleSpaceVacuumCleaner$ = text$ end function
|
|
|
Post by tanger32au on Dec 18, 2018 4:02:34 GMT
|
|
|
Post by B+ on Dec 18, 2018 4:16:15 GMT
Ah so! you are removing blank lines.
' Two many blank lines in the file remover.txt for JB v2 B+ 2018-12-17
FixMe$ = "Replace A to B.txt" open FixMe$ for input as #1 open "2 Blank Lines Removed.txt" for output as #2 while eof(#1) = 0 line input #1, fline$ if trim$(fline$) = "" then if blankLineFlag = 0 then print #2, fline$ end if blankLineFlag = 1 else blankLineFlag = 0 print #2, fline$ end if wend close #1 close #2 print "File work is done."
|
|
|
Post by B+ on Dec 18, 2018 4:26:57 GMT
While we are at it, how about removing all blank lines?
' Blank lines in the file remover.txt for JB v2 B+ 2018-12-17
FixMe$ = "Replace A to B.txt" open FixMe$ for input as #1 open "Blank Lines Removed.txt" for output as #2 while eof(#1) = 0 line input #1, fline$ if trim$(fline$) <> "" then print #2, fline$ end if wend close #1 close #2 print "File work is done."
|
|
|
Post by tanger32au on Dec 18, 2018 4:33:07 GMT
Thanks but the end result does not have the extra spaces / lines removed. Can I upload a file for you to work on?
|
|
|
Post by B+ on Dec 18, 2018 4:46:29 GMT
Sure, I will give it shot.
|
|