dew Posted June 5, 2009 Share Posted June 5, 2009 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 Link to comment Share on other sites More sharing options...
Roman9 Posted June 5, 2009 Share Posted June 5, 2009 (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 June 5, 2009 by Roman9 Link to comment Share on other sites More sharing options...
Mat Posted June 5, 2009 Share Posted June 5, 2009 (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 June 5, 2009 by mdiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
dew Posted June 5, 2009 Author Share Posted June 5, 2009 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 Link to comment Share on other sites More sharing options...
dew Posted June 5, 2009 Author Share Posted June 5, 2009 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 Link to comment Share on other sites More sharing options...
Yashied Posted June 5, 2009 Share Posted June 5, 2009 Look at _FileReadToArray() and _FileWriteFromArray(). My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
oMBRa Posted June 5, 2009 Share Posted June 5, 2009 (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 June 5, 2009 by oMBRa Link to comment Share on other sites More sharing options...
Mat Posted June 5, 2009 Share Posted June 5, 2009 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 AutoIt Project Listing Link to comment Share on other sites More sharing options...
Roman9 Posted June 5, 2009 Share Posted June 5, 2009 My version saves the changes to a new file; in my example called poop.txt >.> But oMBRa's sample looks much better. Link to comment Share on other sites More sharing options...
dew Posted June 5, 2009 Author Share Posted June 5, 2009 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 Link to comment Share on other sites More sharing options...
dew Posted June 6, 2009 Author Share Posted June 6, 2009 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. Link to comment Share on other sites More sharing options...
oMBRa Posted June 6, 2009 Share Posted June 6, 2009 (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 June 6, 2009 by oMBRa Link to comment Share on other sites More sharing options...
dew Posted June 7, 2009 Author Share Posted June 7, 2009 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) 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