Redbeard Posted January 18, 2009 Posted January 18, 2009 (edited) Hello, Is there any way to make _ReplaceStringInFile find line feeds? I want to find and replace certain lines in a text file that contain line feeds. I've tried searching for @LF, @CR and @CRLF but no luck. Example script: $A = _ReplaceStringInFile(@ScriptDir & "\text file.txt", @CR, @CR & "test") MsgBox(0,"",$A) This script always returns 0. Am I doing something wrong? Edited January 18, 2009 by Redbeard
Redbeard Posted January 18, 2009 Author Posted January 18, 2009 Pain said: I think _FileCountLines is better for that. Actually, I'm not trying to count lines, I'm trying to do an actual find/replace. The code I posted was just an example of how I can't get _ReplaceStringInFile to find line feeds. Sorry for the confusion.
Andreik Posted January 18, 2009 Posted January 18, 2009 Redbeard said: Actually, I'm not trying to count lines, I'm trying to do an actual find/replace. The code I posted was just an example of how I can't get _ReplaceStringInFile to find line feeds. Sorry for the confusion.This should work: Func CountCharInFile($PATH,$CHAR) $FILE = FileOpen($PATH,0) $DATA = FileRead($FILE) FileClose($FILE) $COUNT = 0 For $INDEX = 1 To StringLen($DATA) If StringMid($DATA,$INDEX,1) == $CHAR Then $COUNT += 1 Next Return $COUNT EndFunc $PATH = FileOpenDialog("SELECT",@ScriptDir,"All (*.*)",1) MsgBox(0,"",CountCharInFile($PATH,@CR))
Redbeard Posted January 18, 2009 Author Posted January 18, 2009 Andreik said: This should work: Func CountCharInFile($PATH,$CHAR) $FILE = FileOpen($PATH,0) $DATA = FileRead($FILE) FileClose($FILE) $COUNT = 0 For $INDEX = 1 To StringLen($DATA) If StringMid($DATA,$INDEX,1) == $CHAR Then $COUNT += 1 Next Return $COUNT EndFunc $PATH = FileOpenDialog("SELECT",@ScriptDir,"All (*.*)",1) MsgBox(0,"",CountCharInFile($PATH,@CR))Thanks for the post but I'm trying to search and replace, not count. I read my original post and it was misleading so I corrected it.
Andreik Posted January 18, 2009 Posted January 18, 2009 Redbeard said: Thanks for the post but I'm trying to search and replace, not count. I read my original post and it was misleading so I corrected it. #Include <File.au3> $PATH = FileOpenDialog("SELECT",@ScriptDir,"All (*.*)",1) If @error Then Exit Replace($PATH,@CR,@CR & "test") Func Replace($FILE,$SEARCH,$REPLACE) $FILE = FileOpen($PATH,0) $DATA = FileRead($FILE) FileClose($FILE) $DATA = StringReplace($DATA,$SEARCH,$REPLACE) $FILE = FileOpen($PATH,2) FileWrite($FILE,$DATA) FileClose($FILE) EndFunc
Redbeard Posted January 18, 2009 Author Posted January 18, 2009 Andreik said: #Include <File.au3> $PATH = FileOpenDialog("SELECT",@ScriptDir,"All (*.*)",1) If @error Then Exit Replace($PATH,@CR,@CR & "test") Func Replace($FILE,$SEARCH,$REPLACE) $FILE = FileOpen($PATH,0) $DATA = FileRead($FILE) FileClose($FILE) $DATA = StringReplace($DATA,$SEARCH,$REPLACE) $FILE = FileOpen($PATH,2) FileWrite($FILE,$DATA) FileClose($FILE) EndFuncThanks Andreik, that's how I've been doing it but I thought _ReplaceStringInFile might be faster if I could get it to work. It's strange to me that _ReplaceStringInFile will find things line @TAB but not @LF, @CR or @CRLF. Anyway, it seems I will have to go back to doing it the way you suggested. Thanks for your help.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now