Jump to content

i am doing something simple wrong... but cant see were is the mistake... please help

Recommended Posts

i did a script ho supost read a list in txt ad start a loop kiling proces on this list.... jus to keep away some proces with auto reload stuped sistens like microsofth ofice 1 clik to run and another bulshits of an pc werer i want some focus on do quic some simple jobs...


the problem is... the script is running like all fine but the proces are no closing.... and i have no idea abouth why....

for some reason this part (    ProcessClose ($sFileRead)  ) is just doing nothing....

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
$file = "list.txt"
$iCountLines = 1
$g = 2

Until $g = 1

Func kill()
    $iCountLines = _FileCountLines("list.txt")
        $iCountLines -= 1
    Until $iCountLines = 1
EndFunc   ;==>kill

Func Example()
    ; Open the file for reading and store the handle to a variable.
    $hFileOpen = FileOpen($file, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False

    ; Read the fist line of the file using the handle returned by FileOpen.
    $sFileRead = FileReadLine($hFileOpen, $iCountLines)

    ; Display procees ure kiling.
    ToolTip($iCountLines & " task kill:" & @CRLF & $sFileRead, 0, 0)
;~  shold kill the preces but just make me wate time

    ; Close the handle returned by FileOpen.
EndFunc   ;==>Example


Edited by Jos
Added script to post in codetags
Link to comment
Share on other sites

@ETverde Please use tags when you post code as described in the link, instead of attaching your script as a file.

Could you show the content of the list.txt, since the process close is based on it.

Also add an error handler after ProcessClose so we know the reason why it is failing...


Edited by Nine
Link to comment
Share on other sites

  • Developers

You haven't provided any details of which process you are trying to kill. Also ensure that Script runs at the same level as the task you are trying to kill.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
Live for the present,
Dream of the future,
Learn from the past.

Link to comment
Share on other sites

You can use FileReadToArray to get a line-by-line array of the file:

Global $FileListProcess = @ScriptDir & "\ListProcess4Kill.txt"
Global $ArrayListProcess, $ProcessCount = 0

While 1
    If Not FileExists($FileListProcess) Then Exit MsgBox(16 + 262144, "ERROR !", "File list process to kill is not Exists: " & @CRLF & $FileListProcess)
    $ArrayListProcess = _GetListProcess($FileListProcess)
    $ProcessCount = @extended
    If @error Then Exit MsgBox(16 + 262144, "ERROR !", "ERROR on Read File: " & @CRLF & $FileListProcess)
    For $i = 0 To $ProcessCount             ; Loop through the array.
        ConsoleWrite("! Kill process: " & $ArrayListProcess[$i] & @CRLF)  ; Display the contents of the array.
        If FileExists($ArrayListProcess[$i]) Then
            _ProcessClose($ArrayListProcess[$i], 1)

Func _GetListProcess($FileListProcess)
    ConsoleWrite("+ Reading the file: " & $FileListProcess & @CRLF)
    Local $_ArrayListProcess = FileReadToArray($FileListProcess)
    Local $_ProcessCount = @extended - 1
    If @error Then
        ConsoleWrite("! There was an error reading the file. @error: " & @error & @CRLF)        ; An error occurred reading the current script file.
        Return SetError(1, 0, 0)
        Return SetError(0, $_ProcessCount, $_ArrayListProcess)
EndFunc   ;==>_GetListProcess

Func _ProcessClose($sProcess, $All = 0)
    Local $sPID = ProcessExists($sProcess)
    If Not $sPID Then Return SetError(0, 1, 0)
    Dim $Q = 0
    If $All Then
        While ProcessExists($sProcess)
            $Q += 1
            If $Q > 10 Then ExitLoop
        If ProcessExists($sProcess) Then RunWait(@ComSpec & " /c taskkill /T /F /IM " & $sProcess, @SystemDir, @SW_HIDE)
        While ProcessExists($sPID)
            $Q += 1
            If $Q > 10 Then ExitLoop
        If ProcessExists($sPID) Then RunWait(@ComSpec & " /c taskkill /T /F /PID " & $sPID, @SystemDir, @SW_HIDE)
    Return SetError(0, 0, ProcessExists($sPID) = 0)
    ; Dao Van Trong - TRONG.LIVE
EndFunc   ;==>_ProcessClose
; * -----:|
Func _ProcessCloseByPath($sPath)     ;
    Local $colItems = "", $strComputer = "localhost", $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20     ;
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\CIMV2"), $sFailure = 0, $sSuccess = 1     ;
    Local $cQuery = "SELECT ExecutablePath,ProcessId FROM Win32_Process WHERE ExecutablePath = " & '"' & StringReplace($sPath, "\", "\\") & '"'
    $colItems = $objWMIService.ExecQuery($cQuery, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)     ;
    If IsObj($colItems) Then     ;
        For $objItem In $colItems     ;
            Local $sPID = $objItem.ProcessId     ;
            ;ConsoleWrite('closing pid = ' & $sPID & ' path = ' & $objitem.executablepath & @CRLF)
            If ProcessExists($sPID) Then $sFailure += 1     ;
        Next     ;
    EndIf     ;
    If $sFailure Then $sSuccess = 0     ;
    Return SetError($sFailure, 0, $sSuccess)
    ; Dao Van Trong - TRONG.LIVE
EndFunc   ;==>_ProcessCloseByPath


Edited by VIP


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

  • Recently Browsing   0 members

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