Jump to content
Sign in to follow this  
pintas

Loop not working

Recommended Posts

pintas

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

Share this post


Link to post
Share on other sites
Kip

Case $msg = $Exit
 Exit
 ExitLoop
 EndSelect
The "ExitLoop" is useless here.

EndIf
Next
Wend
How does this script even run? You close a For statement that doesn't even exist. Edited by Kip

Share this post


Link to post
Share on other sites
pintas

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

Share this post


Link to post
Share on other sites
pintas

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

Share this post


Link to post
Share on other sites
JohnOne

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.

Share this post


Link to post
Share on other sites
pintas

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.

Share this post


Link to post
Share on other sites
Kip

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?

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.