Jump to content

Replace Entire line in file


Recommended Posts

First time poster and first time using autoit. Running into a problem and after searching the forums / help wasn't able to come up with anything that can solve my problem.

I made this gem to give me a report on the current processes running on my machine through a webserver I have set up on it.

Everything works in this code. However, I think it would be nice to find the processes "cmd.exe" and the "process.exe" and remove those lines from the file since they are only running for several milli seconds to gather this information. Of course this is not a big deal but its been bugging me.

Here is what I have to give you an idea

#include <constants.au3>
$file = FileOpen("process.html", 2)
If $file = -1 Then
    exit
Endif

FileWrite($file, "<html>" & @CRLF)
FileWrite($file, "<body>" & @CRLF)
FileWrite($file, "Process List" & @CRLF)
FileWrite($file, "<PRE>" & @CRLF)

$foo = Run(@ComSpec & " /c tasklist /svc", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
Local $data = ""
    While True
        $data &= StdoutRead($foo)
        If @error Then ExitLoop
        Sleep(25)
    WEnd
FileWrite($file, $data & @CRLF & @CRLF)
    
FileWrite($file, "</PRE>" & @CRLF)
FileWrite($file, "</body>" & @CRLF)
FileWrite($file, "</html>" & @CRLF)

FileClose($file)

_ReplaceStringInFile doesn't seem like it will be able to handle this because the PID is always changing and it can't delete an entire line? I don't really know where to look.

Link to comment
Share on other sites

It is better to replace/skip these lines while you have your data in a string form ($data) than replacing the string in the file.

That's what I did; take a look and try it .... then try to understand what I did :)

#include <constants.au3>
$file = FileOpen("process.html", 2)
If $file = -1 Then
    exit
Endif

FileWrite($file, "<html>" & @CRLF)
FileWrite($file, "<body>" & @CRLF)
FileWrite($file, "Process List" & @CRLF)
FileWrite($file, "<PRE>" & @CRLF)

$foo = Run(@ComSpec & " /c tasklist /svc", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
Local $data = ""
    While True
        $data &= StdoutRead($foo)
        If @error Then ExitLoop
        Sleep(25)
    WEnd


$tempdata = StringSplit($data, @CRLF, 1)
$newdata = ""
For $k=1 to UBound ($tempdata)-1
    If StringInStr($tempdata[$k], "cmd.exe") Or StringInStr($tempdata[$k], "process.exe") Then ;your exe name here
        ;do nothing
    Else
        $newdata &= $tempdata[$k]&@CRLF
    EndIf
Next


FileWrite($file, $newdata & @CRLF & @CRLF)
    
FileWrite($file, "</PRE>" & @CRLF)
FileWrite($file, "</body>" & @CRLF)
FileWrite($file, "</html>" & @CRLF)

FileClose($file)

Keep in mind to replace "process.exe" with your executable's name.

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Oh wow yeah I understand. Thank you. Much easier.

Out of curiosity how close was I with this? And any ideas why this was failing?

#include <constants.au3>
$file = FileOpen("process.html", 2)
If $file = -1 Then
    exit
Endif

FileWrite($file, "<html>" & @CRLF)
FileWrite($file, "<body>" & @CRLF)
FileWrite($file, "Process List" & @CRLF)
FileWrite($file, "<PRE>" & @CRLF)

$foo = Run(@ComSpec & " /c tasklist /svc", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
Local $data = ""
    While True
        $data &= StdoutRead($foo)
        If @error Then ExitLoop
        Sleep(25)
    WEnd

FileWrite($file, $data & @CRLF & @CRLF)

$i = 0

while 1
    $line = FileReadLine($file)
    If StringInStr($line, "cmd.exe") Then _FileWriteToLine($file, $i, "")
    $i = $i + 1
WEnd
   
FileWrite($file, "</PRE>" & @CRLF)
FileWrite($file, "</body>" & @CRLF)
FileWrite($file, "</html>" & @CRLF)

FileClose($file)
Edited by SoulA
Link to comment
Share on other sites

You were close enough with your script :)

I see only 2 issues:

1. You have no exit ways from this loop:

while 1
    $line = FileReadLine($file)
    If StringInStr($line, "cmd.exe") Then _FileWriteToLine($file, $i, "")
    $i = $i + 1
WEnd

while 1 means to cycle always; You need to put an exit condition here otherwise your script will be stuck in a loop.

Correct loop:

while 1
    $line = FileReadLine($file)
    If @error Then ExitLoop ;this is the condition to exit ... when end-of-file is reached or another error it will exit
    If StringInStr($line, "cmd.exe") Then _FileWriteToLine($file, $i, "")
    $i = $i + 1
WEnd

2. This line will "write" an empty line in your file breaking the continuity of the output:

_FileWriteToLine($file, $i, "")

because _FileWriteToLine automatically adds a @CRLF at the end of the line.

Anyway - nice try :P keep up the good work :P

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Ah yes thank you very much I understand now. I also wrote this and got it working based on the code you posted previously. Your way is obviously the best and most efficient I was still curious enough to try and figure out how to parse through the file and change something. Maybe help someone else just starting out figure out a problem.

#include <constants.au3>
$file = FileOpen("process.html", 2)
If $file = -1 Then
    exit
Endif

FileWrite($file, "<html>" & @CRLF)
FileWrite($file, "<body>" & @CRLF)
FileWrite($file, "Process List" & @CRLF)
FileWrite($file, "<PRE>" & @CRLF)

$foo = Run(@ComSpec & " /c tasklist /svc", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
Local $data = ""
    While True
        $data &= StdoutRead($foo)
        If @error Then ExitLoop
        Sleep(25)
    WEnd

FileWrite($file, $data & @CRLF & @CRLF)
  
FileWrite($file, "</PRE>" & @CRLF)
FileWrite($file, "</body>" & @CRLF)
FileWrite($file, "</html>" & @CRLF)

FileClose($file)

$file = FileOpen("process.html", 0)
$data = FileRead($file)
FileClose($file)

$file = FileOpen("process.html", 2)
$tempdata = StringSplit($data, @CRLF, 1)
$newdata = ""
For $k=1 to UBound ($tempdata)-1
    If StringInStr($tempdata[$k], "cmd.exe") Or StringInStr($tempdata[$k], "process.exe") Or StringInStr($tempdata[$k], "tasklist.exe") Then ;your exe name here
        ;do nothing
    Else
        $newdata &= $tempdata[$k]&@CRLF
    EndIf
Next
FileWrite($file, $newdata & @CRLF & @CRLF)
FileClose($file)
Edited by SoulA
Link to comment
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
 Share

  • Recently Browsing   0 members

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