Jump to content

Recommended Posts

Posted

Hi,

I'm searching for a specific string in a text file. Once the string is found I would like to known the line number where the string is located.

The return line number will then be use with FileWriteToLine and/or Func _FileDeleteLine

I cannot find any function that will return a line number.

#Include <File.au3>

#Include <Array.au3>

Func _FileDeleteLine($hFile, $i_Last = 0); If other than 0 it will delete whatever line you want

Local $hFileOpen = '', $aArrayRead = StringSplit(FileRead($hFile, FileGetSize($hFile)), @CRLF, 1)

$hFileOpen = FileOpen($hFile, 2)

If $i_Last = 0 And UBound($aArrayRead) - 1 > 0 Then

For $i_Count = 1 To UBound($aArrayRead) - 2

If $i_Count = UBound($aArrayRead) - 2 Then

FileWrite($hFileOpen, $aArrayRead[$i_Count])

Else

FileWrite($hFileOpen, $aArrayRead[$i_Count] & @CRLF)

EndIf

Next

FileClose($hFileOpen)

Return 1

ElseIf $i_Last <> 0 And UBound($aArrayRead) - 1 > 0 Then

For $i_Count = 1 To UBound($aArrayRead) - 1

If $i_Count = $i_Last Then ContinueLoop

If $i_Count = UBound($aArrayRead) - 1 Then

FileWrite($hFileOpen, $aArrayRead[$i_Count])

Else

FileWrite($hFileOpen, $aArrayRead[$i_Count] & @CRLF)

EndIf

Next

FileClose($hFileOpen)

Return 1

EndIf

FileClose($hFileOpen)

Return 0

EndFunc

$Dir = @WorkingDir

$FileList=_FileListToArray(@WorkingDir, "*.dat", 1)

If @Error=1 Then

MsgBox (0,"","No Files\Folders Found.")

EndIf

;_ArrayDisplay($FileList, "Files Found")

For $X = 1 to $Filelist[0]

$Find = "Registered Owner:"

$replace = "Registered Owner: "

_FileWriteToLine($Filelist[$X], 18, "Registered Owner:", 1)

;MsgBox (0,"replace",$replace)

;$count = _ReplaceStringInFile($Dir & "\" & $Filelist[$X],$find,$replace)

Next

;For $X = 1 to $Filelist[0]

; $find = @CR

; $replace = StringTrimRight($find, 1)

; $count = _ReplaceStringInFile($Dir & "\" & $Filelist[$X],$find,$replace)

;Next

Posted

$retval = _FileReadToArray("C:\test.txt", $content)


for $iLine = 1 to $content[0] step 1

    if StringInStr($content[$iLine], "HELLO") then
              msgbox(0, "INFO", "Found 'HELLO' in line " & $iLine)
    endif

next; $iLine

Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Posted (edited)

If you are just replacing text in lines or deleting lines with certain text, try a Regular Expression. From your example:

For $x = 1 To $FileList[0]
    $Find = "Registered Owner:"
    $Replace = "Registered Owner: "
    
    ;To remove entire line where "Registerd Owner:" is found
    $String = StringRegExpReplace(FileRead($FileList[$x]), "(?i)(?m)^.*" & $Find & "[^ ].*$", "")
    
    ;To replace "Registered Owner:" with "Registered Owner: "
    $String = StringRegExpReplace(FileRead($FileList[$x]), "(?i)(?m)^.*" & $Find & "[^ ].*$", $Replace)
Next

Using regular expressions in multiline mode: (?m), you will not have to split the files into separate lines

EDIT: If you want to remove blank lines(maybe after you have deleted the line with unwanted string), just yell

Edited by Varian
Posted

ajag solution Works perfectly.

I needed to re-write the line where registered owner: was found in order to remove what was after the ":"

The other issue is it as to be done on aprox 1000 different files.

And lastly the line number is always different in each file.

The line to modify was like this:

Registered owner: Alxm2001

the final result is only

register owner:

My final code is

#Include <File.au3>

#Include <Array.au3>

Dim $content

$Dir = @WorkingDir

$FileList=_FileListToArray(@WorkingDir, "*.dat", 1)

If @Error=1 Then

MsgBox (0,"","No Files\Folders Found.")

EndIf

;_arrayDisplay($FileList, "Files Found")

;Remove Owner in text file

For $X = 1 to $Filelist[0]

$Find = "Registered Owner:"

$retval = _FileReadToArray($Filelist[$X], $content)

for $iLine = 1 to $content[0] step 1

if StringInStr($content[$iLine], $find) then

;msgbox(0, "INFO", $Find & $iLine)

_FileWriteToLine($Filelist[$X], $iLine, "Registered Owner:", 1)

endif

next;$iLine

Next

Thanks all for your help

Posted (edited)

Or:

For $x = 1 To $FileList[0]
    $Find = "Registered Owner:"
    ;removes everything before and after line in which  "Registered Owner:" is found
    $String = StringRegExpReplace(FileRead($FileList[$x]), "(?i)(?m)^(.*)" & $Find & "(.*)$", $Find)
    ;MsgBox(32, 'Fixed File Reads:', $String) ;Test To See Result
    FileOpen($FileList[$x], 2)
    FileWrite($FileList[$x], $String)
    FileClose($FileList[$x])
Next
Edited by Varian

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...