Jump to content

Loop not working


pintas
 Share

Recommended Posts

I've taken a small piece of code i found here to monitor usb insertion and removal, and i'm trying to add some features to it, but i came across a problem.

#Include <Constants.au3>

$strComputer = "."
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

Opt("TrayMenuMode", 1)
$Pen = "Program"

$Exit = TrayCreateItem("Exit")
TraySetState()

While 1
    Sleep(100)
    $msg = TrayGetMsg()
        Select
        Case $msg = 0
        ContinueLoop
        Case $msg = $Exit
        Exit
        ExitLoop
        EndSelect
    $objEvent = $colEvents.NextEvent
    $autorun = $objEvent.TargetInstance.DeviceId & "\autorun.inf"
    $drive = $objEvent.TargetInstance.DeviceId
    $i = 0
    If $objEvent.TargetInstance.DriveType = 2 Then 
        Select
        Case $objEvent.Path_.Class()="__InstanceCreationEvent"
            $UsedToBe = 0
                If Not FileExists($autorun) And $UsedToBe = 0 Then
                    TrayTip("Pen Security", $autorun & " not present ! ;)", 2, 1)
                EndIf           
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)
                    PlaySound()
                    For $I = 1 To 3
                        If FileExists($autorun) Then
                            $UsedToBe = 1
                            FileSetAttrib($autorun, '-SHR-A')
                            FileDelete($autorun)
                            Sleep(500)
                        Else
                    ExitLoop
                        EndIf
                    Next
                        If Not FileExists($autorun) And $UsedToBe = 1 Then
                            TrayTip("Pen Security", $autorun & " deleted ! ;)", 2, 1)
                        EndIf
                        If FileExists($autorun) And $UsedToBe = 1 Then
                            TrayTip("Pen Security", $drive & "\AUTORUN.INF NOT DELETED !" & @CRLF & "Pen-Drive Probably Write-Protected.", 5, 1)
                        EndIf
            Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
                ExitLoop
        EndSelect
    EndIf
Next
Wend

What am i doing wrong here? How can add functions to my tray without losing the loop to detect usb? How can i get that Exit Case working inside the loop? Can i use two loops?

Edited by pintas
Link to comment
Share on other sites

You're right, i forgot to paste some important parts, but the script is very long now and it's kind of hard to keep track of everything all the time. But the For statement is in my original full two thousand lines script... :)

The problem is the case $msg = $Exit doesn't do anything. ;) Why???

btw, that's just an example, i've added many more of those, but they don't work...

Edited by pintas
Link to comment
Share on other sites

Well, if you leave out important parts, then how can we help you?

I meant the for statement. As you can see i just removed the Next in the end of this example, and still there is no way i can get it working.

Do i need to have two different files running side by side to have the same effect?

Btw... I got an eye on X programming :)

#Include <Constants.au3>

$strComputer = "."
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

Opt("TrayMenuMode", 1)
$Pen = "Program"

$Exit = TrayCreateItem("Exit")
TraySetState()

While 1
    Sleep(100)
    $msg = TrayGetMsg()
        Select
        Case $msg = 0
        ContinueLoop
        Case $msg = $Exit
        Exit
        ExitLoop
        EndSelect
    $objEvent = $colEvents.NextEvent
    $autorun = $objEvent.TargetInstance.DeviceId & "\autorun.inf"
    $drive = $objEvent.TargetInstance.DeviceId
    $i = 0
    If $objEvent.TargetInstance.DriveType = 2 Then 
        Select
        Case $objEvent.Path_.Class()="__InstanceCreationEvent"
            $UsedToBe = 0
                If Not FileExists($autorun) And $UsedToBe = 0 Then
                    TrayTip("Pen Security", $autorun & " not present ! ;)", 2, 1)
                EndIf           
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)
                    PlaySound()
                    For $I = 1 To 3
                        If FileExists($autorun) Then
                            $UsedToBe = 1
                            FileSetAttrib($autorun, '-SHR-A')
                            FileDelete($autorun)
                            Sleep(500)
                        Else
                    ExitLoop
                        EndIf
                    Next
                        If Not FileExists($autorun) And $UsedToBe = 1 Then
                            TrayTip("Pen Security", $autorun & " deleted ! ;)", 2, 1)
                        EndIf
                        If FileExists($autorun) And $UsedToBe = 1 Then
                            TrayTip("Pen Security", $drive & "\AUTORUN.INF NOT DELETED !" & @CRLF & "Pen-Drive Probably Write-Protected.", 5, 1)
                        EndIf
            Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
                ExitLoop
        EndSelect
    EndIf
Wend
Link to comment
Share on other sites

Are you saying it dosent exit ? because thats what it looks like it would do

Anyhow, have a look at continuecase, or possibly continueloop

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Are you saying it dosent exit ? because thats what it looks like it would do

Anyhow, have a look at continuecase, or possibly continueloop

Thanks JohnOne, i'll take a look at it.

With or without the for statement the script simply doesn't work.

Link to comment
Share on other sites

I meant the for statement. As you can see i just removed the Next in the end of this example, and still there is no way i can get it working.

Do i need to have two different files running side by side to have the same effect?

Btw... I got an eye on X programming :)

#Include <Constants.au3>

$strComputer = "."
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
 $colEvents = $objWMIService.ExecNotificationQuery _
 ("Select * From __InstanceOperationEvent Within 5 Where " _
 & "TargetInstance isa 'Win32_LogicalDisk'")

Opt("TrayMenuMode", 1)
$Pen = "Program"

$Exit = TrayCreateItem("Exit")
TraySetState()

While 1
 Sleep(100)
 $msg = TrayGetMsg()
 Select
 Case $msg = 0
 ContinueLoop
 Case $msg = $Exit
 Exit
 ExitLoop
 EndSelect
 $objEvent = $colEvents.NextEvent
 $autorun = $objEvent.TargetInstance.DeviceId & "\autorun.inf"
 $drive = $objEvent.TargetInstance.DeviceId
 $i = 0
 If $objEvent.TargetInstance.DriveType = 2 Then 
 Select
 Case $objEvent.Path_.Class()="__InstanceCreationEvent"
 $UsedToBe = 0
 If Not FileExists($autorun) And $UsedToBe = 0 Then
 TrayTip("Pen Security", $autorun & " not present ! ;)", 2, 1)
 EndIf 
 Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)
 PlaySound()
 For $I = 1 To 3
 If FileExists($autorun) Then
 $UsedToBe = 1
 FileSetAttrib($autorun, '-SHR-A')
 FileDelete($autorun)
 Sleep(500)
 Else
 ExitLoop
 EndIf
 Next
 If Not FileExists($autorun) And $UsedToBe = 1 Then
 TrayTip("Pen Security", $autorun & " deleted ! ;)", 2, 1)
 EndIf
 If FileExists($autorun) And $UsedToBe = 1 Then
 TrayTip("Pen Security", $drive & "\AUTORUN.INF NOT DELETED !" & @CRLF & "Pen-Drive Probably Write-Protected.", 5, 1)
 EndIf
 Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
 Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
 ExitLoop
 EndSelect
 EndIf
Wend

Does the example you post here work?
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...