Jump to content

Check all removables for a file then do action - Error :(


Recommended Posts

Hey,

I got a little problem while making a little tool. It works this way: You plug in a removable drive (USB) and as soon as it get's plugged in it checks

for a file called "password.dat", if it exists it reads the file and if my chosen password "U28e+dje33eefF" matches, i gives a massagebox/executes a function.

Now i wrote this: 

#include <File.au3>

While 1
drives ()
check ()
sleep(500)
WEnd




func check ()
$file = "database.dat"
FileOpen($file, 0)
For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
If $line = "U28e+dje33eefF" Then
MsgBox (0,"","ITS IT")
Else
sleep(500)
EndIf
Next
FileClose($file)
EndFunc

func drives ()
$hFO = FileOpen("database.dat", 2)
FileWrite($hFO, "")
FileClose($hFO)
Local $aArray = DriveGetDrive("REMOVABLE")
If @error Then
   
Else
    For $i = 1 To $aArray[0]
      FileWrite("database.dat", ($aArray[$i]) & "\password.dat"& @CRLF)      
    Next
 EndIf
 EndFunc

Now exactly what my program should currently do: Always check for all current removables and then check if the file exists, if not it does nothing, if it

exists it reads the file for the password. If the password matches, it should execute a message box saying "ITS IT".

I know there is an error: Where the password gets checked, it checks the database.dat file instead of the password.dat file. When changing it to this:

#include <File.au3>

While 1
drives ()
check ()
sleep(500)
WEnd




func check ()
$file = "database.dat"
FileOpen($file, 0)
For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
    $password = FileReadLine($line,1)
If $password = "U28e+dje33eefF" Then
MsgBox (0,"","ITS IT")
Else
sleep(500)
EndIf
Next
FileClose($file)
EndFunc

func drives ()
$hFO = FileOpen("database.dat", 2)
FileWrite($hFO, "")
FileClose($hFO)
Local $aArray = DriveGetDrive("REMOVABLE")
If @error Then
   
Else
    For $i = 1 To $aArray[0]
      FileWrite("database.dat", ($aArray[$i]) & "\password.dat"& @CRLF)      
    Next
 EndIf
 EndFunc

It gives me another error, that there is no drive :(

I would really appreciate it if someone could help me with this... I read so many tutorials, however i couldn't find a solution.

Thanks in advance & best regards

Cyton

Link to comment
Share on other sites

Try this, it gets rid of the database.dat file and passes an array around instead that holds the drives found and the password file.

Global $Return, $Return2
While 1
    $Return = drives()
    If UBound($Return) Then
        $Return2 = check($Return)
        If $Return2 Then
            ConsoleWrite('! Password found on drive: ' & StringLeft($Return[$Return2], 2) & @CRLF)
            ;do something here
        EndIf
    EndIf
    Sleep(500)
WEnd
Func check($aArray)
    For $I = 1 To $aArray[0]
        $password = FileReadLine($aArray[$I], 1)
        If $password = "U28e+dje33eefF" Then
            MsgBox(0, "", "ITS IT")
            Return $I
        Else
            Sleep(500)
        EndIf
    Next
    Return 0
EndFunc   ;==>check

Func drives()
    Local $aArray = DriveGetDrive("REMOVABLE")
    Local $TempArray[1], $Error = 0
    If @error Then
        $TempArray = 1
        $Error = @error
    Else
        ReDim $TempArray[$aArray[0] + 1]
        $TempArray[0] = $aArray[0]
        For $I = 1 To $aArray[0]
            $TempArray[$I] = $aArray[$I] & "\password.dat"
        Next
    EndIf
    Return SetError($Error, 0, $TempArray)
EndFunc   ;==>drives

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thanks! Unfortunatly im still getting this error, but it's nice without the database which i was

trying to use as a work around. Here is a screenshot of the error:

37p9ys9k.png

Translation: No medium/data storage

There is no medium in the drive. Please insert a medium into the drive DeviceHarddisk3DR3.

---------------------------------------------------------------------------------------------------------------------------

I really don't know if this has something to do with AutoIT or with Windows 7 x64.

Thanks in advance & best regards

Cyton

Edited by cytonwidee
Link to comment
Share on other sites

Do you have a memory card reader on the computer? Sounds to me that it's finding a card reader and trying to read a file off it causes the error. Try using DriveGetFileSystem to see if the function returns 1, which means there's nothing in the drive, or if it returns a valid file system type.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thank you so much! This was the solution to my problem! Finally got it... i disabled the card reader since im not

using it that much. Is there a way to ignore the card reader? With the DriveGetFileSystem command? I tried

it with the DriveGetFileSystem command and it returned three times "NTFS" for all three drives.

How can i "implent" it into the script?

Thanks in advance.

Link to comment
Share on other sites

If Not DriveGetFileSystem("<insert drive letter here>\") = "1" Then
     ; attempt to find the file here
Endif

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

cytonwidee,

I use DriveStatus to bypass my card reader drives. Om my machine they only return "READY" if they have a card inserted - otherwise I just skip that drive letter from the DriveGetDrive("ALL") return. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you both for you solutions! In my case i found DriveStatus to be working really good.

I made this to test if it is a drive: (This just shows how to find out if it is a card reader and

gives people an idea on how to implent it)

$drive = InputBox("CR-Test","Please enter the drive letter to test if it is a card reader.")
$status = DriveStatus($drive & ":\")

if $status = "INVALID" Then
MsgBox(0,"Info","It is a card reader.")
Elseif $status = "NOTREADY" Then
MsgBox(0,"Info","It is a card reader.")
else 
MsgBox(0,"Info","It is not a card reader.")
EndIf 

And as a function to check all drives:

Local $aArray = DriveGetDrive("REMOVABLE")
If @error Then
   
Else
    For $i = 1 To $aArray[0]
      
      $status = DriveStatus("" & $aArray[$i])
          
if $status = "INVALID" Then
MsgBox(0,"Info",$aArray[$i] & " is a card reader.")
Elseif $status = "NOTREADY" Then
MsgBox(0,"Info",$aArray[$i] & " is a card reader.")
else 
MsgBox(0,"Info",$aArray[$i] & " is not a card reader.")
EndIf
    Next
 EndIf

Always works =) Thank you so much, i'll post the final code in the next hour for people who want to this too.

Best regards...

Cyton

Edited by cytonwidee
Link to comment
Share on other sites

Okay guys, if anyone needs this, i again modified it so its easier to understand:

#include <File.au3>
 
While 1
check ()
sleep(500)
WEnd
 
 
 
func check ()
Local $aArray = DriveGetDrive("REMOVABLE")
If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox(4096, "DriveGetDrive", "It appears an error occurred.")
Else
    For $i = 1 To $aArray[0]
        ;check if drives are OK
        $status = DriveStatus("" & $aArray[$i])
          
if $status = "INVALID" Then
;No Action because its not a valid drive.
Elseif $status = "NOTREADY" Then
;No Action because its not ready
else 
$password = FileReadLine(($aArray[$i]) & "/password.dat");Action because drive is OK   
If $password = "U28e+dje33eefF" Then
      ;do something here *start*
      MsgBox(0,"","BAMM")
      ;do something here *end*
Else
EndIf
EndIf
     
Next
EndIf
EndFunc

Have fun with it, can be closed.

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