Sign in to follow this  
Followers 0
copekyle

Format SD-Card when inserted

11 posts in this topic

I'm new to autoit and I'm not sure where to start.  I have to move a file to 128 micro sd cards and then format them and check to make sure the file is erased.  (Basically checking their ability to r/w and then formatting for use in a product)  I'm not sure how to recognize that I plugged in my card.  Everything else I can pretty much figure out.

Anyone know an easy way to do this?

Kyle

Share this post


Link to post
Share on other sites



Maybe use DriveStatus and when it becomes ready execute a file write, if its successful format it

Share this post


Link to post
Share on other sites

There is an example, please read it before using it

#include <MsgBoxConstants.au3>
#include <File.au3>
HotKeySet("{ESC}", "Terminate") ; Hotkey to exit the program
HotKeySet("{F4}", "aTestFile") ; hotkey to check for the SD card, try to write file and then format it
Global $sDrive = "F:" ; insert the card's letter here
Global $eFormat = 0 ; set this to 1 to enable formating, made like this so i will be sure noone will accidently format his drives because he didnt read

While 1
    Sleep(100)
WEnd

Func Terminate()
    Exit
EndFunc

Func aTestFile()
    Local $i
    Local $ready = "READY"
For $i = 1 to 60
    sleep(1000)
    Local $sStatus = DriveStatus($sDrive)
        If $sStatus = $ready Then
            _FileCreate($sDrive & "\test.tmp")
                If @error Then
                    MsgBox($MB_SYSTEMMODAL, "Error", " Error creating temp file",5)
                    ExitLoop
                EndIf
            FileDelete($sDrive & "\test.tmp")
                If @error Then
                    MsgBox($MB_SYSTEMMODAL, "Error", " Error deleting temp file",5)
                    ExitLoop
                EndIf
            sleep(5)
            If $eFormat = 1 Then
                RunWait(@ComSpec & " /C format " & $sDrive & " /Q /y")
                ;ProcessClose("cmd.exe")
                ProcessWaitClose("cmd.exe")
                MsgBox($MB_SYSTEMMODAL, "Success","Format completed, please change the card and press the hotkey again", 5)
            Else
                MsgBox($MB_SYSTEMMODAL, "Error", "Format C: /Q /y is the way of the hero",5) ; yea
            EndIf
            ExitLoop
        ElseIf $i < 30 And $sStatus <> $ready Then
            ConsoleWrite($i & @CRLF)
            ConsoleWrite("Status of the drive : " & $sStatus & @CRLF)
        ElseIf $i = 30 OR $i > 30 And $sStatus <> $ready Then
            MsgBox($MB_SYSTEMMODAL, "Error", " Drive was not become ready within 30 seconds,card might be broken",5)
            ExitLoop
        EndIf
Next

EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

ok.  I like the idea of using the DriveStatus command.  Now I have to figure out a way for the program to loop.  I can't think of a good way to do this.  If I do this:

while DriveStatus(H:)= "Ready"

;send file

;format

wend

Then it will just do it over and over again while it's plugged in.  But if I use an "If" statement then it will only execute once.  Maybe I'm over thinking this?

You replied while I was typing this... I'll give it a try

Edited by copekyle

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

ok.  I like the idea of using the DriveStatus command.  Now I have to figure out a way for the program to loop.  I can't think of a good way to do this.  If I do this:

while DriveStatus(H:)= "Ready"

;send file

;format

wend

Then it will just do it over and over again while it's plugged in.  But if I use an "If" statement then it will only execute once.  Maybe I'm over thinking this?

You replied while I was typing this... I'll give it a try

 

Just make triplesure you have read it and changing it according your needs

EDIT: if it works for you, mark this as solved to help other people in the future, if they need this

Edited by JustSomeone

Share this post


Link to post
Share on other sites

I was hoping that I could just wait for the card to be plugged in and execute automatically when it is ready.  Then give a success box when finished and I could unplug the finished card and plug the next one and have it start when the device is ready.

Share this post


Link to post
Share on other sites

I feel like an idiot asking this but why is the " /y " switch used while formatting?  I don't recognize it for formatting in dos?  Is it part of the RunWait command like the " /c " ?

RunWait(@ComSpec & " /C format " & $sDrive & " /Q /y")

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I feel like an idiot asking this but why is the " /y " switch used while formatting?  I don't recognize it for formatting in dos?  Is it part of the RunWait command like the " /c " ?

RunWait(@ComSpec & " /C format " & $sDrive & " /Q /y")

This is there to avoid cmd window asking you dumb questions. /y can be read as "just format the ^*censored**&!@#&* card" 

EDIT: you can edit it and make it wait for another card, but i didnt make it like this to give you more time to change the card

another edit : change $eFormat = 0 to 1 when you are triplesure you wont screw your harddrive or something imporant to you. Don't blame if you format the wrong drive. I've added an harmless msgbox reminding you what can happen if you do it wrong :D 

Edited by JustSomeone

Share this post


Link to post
Share on other sites

haha... I saw the comment for $eFormat = 0 and appreciate you idiot proofing this just in case I was one of those people that executes code with out understanding exactly what it is doing. (I am not one of those people.)  This will work for my application. 

Thanks

Share this post


Link to post
Share on other sites

Glad i helped :)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

#include <File.au3>
HotKeySet("{ESC}", "_Exit")
Func _Exit()
    Exit
EndFunc   ;==>_Exit
Func _CloseWindow()
    If WinClose("[class:#32770]", "") = 1 Then AdlibUnRegister("_CloseWindow")
EndFunc   ;==>_CloseWindow
While 1
    $a1 = DriveGetDrive("REMOVABLE")
    For $i = 1 To $a1[0]
        If DriveStatus($a1[$i]) = "READY" Then $i = MsgBox(262144, Default, "Remove media from drive  " & $a1[$i], 1)
    Next
    While 1
        MsgBox(262144, Default, "Insert SD card to be formatted", 1)
        For $i = 1 To $a1[0]
            If DriveStatus($a1[$i]) = "READY" Then ExitLoop 2
        Next
    WEnd
    $sDrive = $a1[$i]
    AdlibRegister("_CloseWindow", 100)
    If Not _FileCreate($sDrive & "\test.tmp") Then ExitLoop 0 * MsgBox(16 + 262144, Default, "Error creating temp file", 0)
    If Not FileDelete($sDrive & "\test.tmp") Then ExitLoop 0 * MsgBox(16 + 262144, Default, "Error deleting temp file", 0)
    RunWait(@ComSpec & " /c format " & $sDrive & " /Q /y || pause") ; pause is executed when format fails
    Beep(440, 200) ; ready to change SD cards
WEnd

With this code, there is no need to press any keys, ... just change the SD cards.

 

When it beeps, just insert the next SD card.

If format command fails, the command window will stay until you press any key.

Edited by Exit

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

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  
Followers 0