Sign in to follow this  
Followers 0
Redbeard

_ReplaceStringInFile to find line feeds

7 posts in this topic

#1 ·  Posted (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 by Redbeard

Share this post


Link to post
Share on other sites



I think _FileCountLines is better for that. :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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. :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#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
Thanks 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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0