Sign in to follow this  
Followers 0
dew

How to edit a txt line

13 posts in this topic

I have created a txt file with the following command -

Send("tasklist /nh /fo ""csv"" /fi ""PID gt 100"" > ""c:\TaskList.txt {ENTER}")

The file is created successfully, but I would like to modify the file so that it remove everything from the second comma on and remove the quotes marks as well as the first "," by replacing it with PID.

sample of the text file created:

"smss.exe","504","Services","0","1,116 K"

"csrss.exe","572","Services","0","7,368 K"

"wininit.exe","640","Services","0","5,280 K"

"csrss.exe","652","Console","1","12,520 K"

So that the file would be like so:

smss.exe PID 504

csrss.exe PID 572

wininit.exe PID 640

csrss.exe PID 652

What I need is to create a list box where I can select an ".exe" file and force to kill the app by using the following command.

TASKKILL /PID 504

or multi select a kill command

TASKKILL /PID 504 /PID 572 /PID 640

Any suggestion on doing this I would be grateful and thanks ahead of time for you guys time.

-dew

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This is as far as I've gotten. Hope this is a good approach and gets your started.

$read = "test.txt"
$write = FileOpen("poop.txt",2)
$sData = FileRead($read)

; Check if file opened for reading OK
If ($sData == -1 or $write == -1) Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$sData = StringStripCR(StringStripWS($sData, 3))
$aData = StringSplit($sData, @LF) ;It's the array of our data to check

; Read in lines of text until the EOF is reached
for $i=1 to $aData[0]
    If $aData[$i] == "" Then ContinueLoop 
    
    $line = FileReadLine($read, $i)
    MsgBox(266144, "1", $line)
    If @error = -1 Then ExitLoop
    
    $fixedLine1 = StringReplace($line, ",", " ")
    $fixedLine2 = StringReplace($fixedLine1, '"', "")
    FileWriteLine($write, $fixedLine2)
Next
Edited by Roman9

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

_FmtFile ("c:\test.txt")

Func _FmtFile ($sFile)
   If Not FileExists ($sFile) Then Retun SetError (1, 0, 0)
   Local $aLines, $sString, $sFileData = ""
   If StringInStr (FileRead ($sFile), @LF) Then
      $aLines = StringSplit (StringStripCR (FileRead($sFile)), @LF)
   Else
      $aLines = StringSplit (FileRead($sFile), @CR)
   EndIf
   For $i = 1 to $aLines[0]
      $sString = StringReplace ($aLines[$i], """", ""); take out all quote marks.
      $sString = StringSplit ($sString, ",")
      $sFileData &= @CRLF & $sString[1] & " PID" & $sString[2]
   Next
   $hFile = FileOpen ($sFile, 2)
      FileWrite ($hFile, $sFileData)
   FileFlush ($hFile)
EndFunc; ==> _FmtFile

is that what you want?

MDiesel

Edited by mdiesel

Share this post


Link to post
Share on other sites

Roman9

I'm having a problem - it does not write the changes back to the file. The Msgbox reads each line ok, but it does not write back to the new file. Any suggestions?

-dew

This is as far as I've gotten. Hope this is a good approach and gets your started.

$read = "test.txt"
$write = FileOpen("poop.txt",2)
$sData = FileRead($read)

; Check if file opened for reading OK
If ($sData == -1 or $write == -1) Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$sData = StringStripCR(StringStripWS($sData, 3))
$aData = StringSplit($sData, @LF) ;It's the array of our data to check

; Read in lines of text until the EOF is reached
for $i=1 to $aData[0]
    If $aData[$i] == "" Then ContinueLoop 
    
    $line = FileReadLine($read, $i)
    MsgBox(266144, "1", $line)
    If @error = -1 Then ExitLoop
    
    $fixedLine1 = StringReplace($line, ",", " ")
    $fixedLine2 = StringReplace($fixedLine1, '"', "")
    FileWriteLine($write, $fixedLine2)
Next

Share this post


Link to post
Share on other sites

Hi mdiesel,

I am getting an error "undefined function for "FileFlush ($hFile). Is "FileFlush" a standard function

thanks for your help,

_FmtFile ("c:\test.txt")

Func _FmtFile ($sFile)
   If Not FileExists ($sFile) Then Retun SetError (1, 0, 0)
   Local $aLines, $sString, $sFileData = ""
   If StringInStr (FileRead ($sFile), @LF) Then
      $aLines = StringSplit (StringStripCR (FileRead($sFile)), @LF)
   Else
      $aLines = StringSplit (FileRead($sFile), @CR)
   EndIf
   For $i = 1 to $aLines[0]
      $sString = StringReplace ($aLines[$i], """", ""); take out all quote marks.
      $sString = StringSplit ($sString, ",")
      $sFileData &= @CRLF & $sString[1] & " PID" & $sString[2]
   Next
   $hFile = FileOpen ($sFile, 2)
      FileWrite ($hFile, $sFileData)
   FileFlush ($hFile)
EndFunc; ==> _FmtFile

is that what you want?

MDiesel

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

try this:

$sFile = 'TaskList.txt'
$hFile = FileOpen($sFile, 0)
$sData = FileRead($hFile)

$sNewData = StringRegExpReplace($sData, '(")(\w+.\w+)(",")(\d+)(")(.*")', '\2 PID \4')
FileClose($hFile)
$hFile = FileOpen($sFile, 2)
FileWrite($hFile, $sNewData)

EDIT: edited the script so it saves the new file

Edited by oMBRa

Share this post


Link to post
Share on other sites

Not that it matters as oMBra has a much better version, but FileFlush is a beta function, sorry - i should have used FileClose instead. Just replace FileFlush wit FileClose (or download the beta).

oMBra's mthod is much nicer though.

MDiesel

Share this post


Link to post
Share on other sites

My version saves the changes to a new file; in my example called poop.txt >.>

But oMBRa's sample looks much better.

Share this post


Link to post
Share on other sites

Hi oMBRa,

Thank you for helping. I looked up the StringRegExpReplace pattern and I do understand what you did. Nice.

Under "EDIT:...", I thought that FileClose($hFile) would saved the new string to 'TaskList.txt'. It did not, nothing happens. I was thinking that "(sNewData)" would replace (overwrite) the current text in TaskList.txt and would be saved when FileClose($hFile) was done. I added "FileClose($hFile) at the end of your code. What am I missing?

Again, thanks for your help.

try this:

$sFile = 'TaskList.txt'
$hFile = FileOpen($sFile, 0)
$sData = FileRead($hFile)

$sNewData = StringRegExpReplace($sData, '(")(\w+.\w+)(",")(\d+)(")(.*")', '\2 PID \4')
FileClose($hFile)
$hFile = FileOpen($sFile, 2)
FileWrite($hFile, $sNewData)

EDIT: edited the script so it saves the new file

Share this post


Link to post
Share on other sites

Hi oMBRa,

I added "MsgBox(0,"$sNewData", FileRead($sNewData), 1)" right after line 4 and it shows no change. I would really like to know why it did not changed for it should have removed the requested charactors.

thanks,

$sFile = 'c:\TaskList.txt'

$hFile = FileOpen($sFile, 0)

$sData = FileRead($hFile)

$sNewData = StringRegExpReplace($sData, '(")(\w+.\w+)(",")(\d+)(")(.*")', '\2 PID \4')

MsgBox(0,"$sNewData", FileRead($sNewData), 1)

FileClose($hFile)

$hFile = FileOpen($sFile, 2)

FileWrite($hFile, $sNewData)

Hi oMBRa,

Thank you for helping. I looked up the StringRegExpReplace pattern and I do understand what you did. Nice.

Under "EDIT:...", I thought that FileClose($hFile) would saved the new string to 'TaskList.txt'. It did not, nothing happens. I was thinking that "(sNewData)" would replace (overwrite) the current text in TaskList.txt and would be saved when FileClose($hFile) was done. I added "FileClose($hFile) at the end of your code. What am I missing?

Again, thanks for your help.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Hi oMBRa,

I added "MsgBox(0,"$sNewData", FileRead($sNewData), 1)" right after line 4 and it shows no change. I would really like to know why it did not changed for it should have removed the requested charactors.

thanks,

$sFile = 'c:\TaskList.txt'

$hFile = FileOpen($sFile, 0)

$sData = FileRead($hFile)

$sNewData = StringRegExpReplace($sData, '(")(\w+.\w+)(",")(\d+)(")(.*")', '\2 PID \4')

MsgBox(0,"$sNewData", FileRead($sNewData), 1)

FileClose($hFile)

$hFile = FileOpen($sFile, 2)

FileWrite($hFile, $sNewData)

the problem is ''FileRead($sNewData)'', replace that line with:

MsgBox(0,"$sNewData", $sNewData, 1)
Edited by oMBRa

Share this post


Link to post
Share on other sites

oMBRa,

Thank you for all your help. It now working.

Also, thanks goes to Roman9 and Mdiesel for their suggestions as well.

-dew

the problem is ''FileRead($sNewData)'', replace that line with:

MsgBox(0,"$sNewData", $sNewData, 1)

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