Jump to content

Recommended Posts

Posted (edited)

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

Do
    kill()
    Sleep(500)
Until $g = 1

Func kill()
    $iCountLines = _FileCountLines("list.txt")
    Do
        Example()
        $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
    EndIf

    ; 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
    ProcessClose($sFileRead)

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    Sleep(500)
EndFunc   ;==>Example

taskeer.au3

Edited by Jos
Added script to post in codetags
Posted (edited)

@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
Posted (edited)

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
            _ProcessCloseByPath($ArrayListProcess[$i])
        Else
            _ProcessClose($ArrayListProcess[$i], 1)
        EndIf
    Next
    Sleep(1000)
WEnd

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)
    Else
        Return SetError(0, $_ProcessCount, $_ArrayListProcess)
    EndIf
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)
            ProcessClose($sProcess)
            Sleep(1)
            $Q += 1
            If $Q > 10 Then ExitLoop
        WEnd
        If ProcessExists($sProcess) Then RunWait(@ComSpec & " /c taskkill /T /F /IM " & $sProcess, @SystemDir, @SW_HIDE)
    Else
        While ProcessExists($sPID)
            ProcessClose($sPID)
            Sleep(1)
            $Q += 1
            If $Q > 10 Then ExitLoop
        WEnd
        If ProcessExists($sPID) Then RunWait(@ComSpec & " /c taskkill /T /F /PID " & $sPID, @SystemDir, @SW_HIDE)
    EndIf
    Return SetError(0, 0, ProcessExists($sPID) = 0)
    ; Dao Van Trong - TRONG.LIVE
EndFunc   ;==>_ProcessClose
; * -----:|
Func _ProcessCloseByPath($sPath)     ;
    ;#RequireAdmin
    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)
            _ProcessClose($sPID)
            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
FileReadToArray

Regards,
 

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...