Redbeard Posted January 18, 2009 Share 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 Link to comment Share on other sites More sharing options...
Pain Posted January 18, 2009 Share Posted January 18, 2009 I think _FileCountLines is better for that. Link to comment Share on other sites More sharing options...
Redbeard Posted January 18, 2009 Author Share Posted January 18, 2009 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. Link to comment Share on other sites More sharing options...
Andreik Posted January 18, 2009 Share Posted January 18, 2009 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)) When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Redbeard Posted January 18, 2009 Author Share Posted January 18, 2009 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. Link to comment Share on other sites More sharing options...
Andreik Posted January 18, 2009 Share Posted January 18, 2009 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 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Redbeard Posted January 18, 2009 Author Share Posted January 18, 2009 #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. Link to comment Share on other sites More sharing options...
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