Jump to content

How to copy file to a detected removable drive


Recommended Posts

Thanks for the help from this forum, I have been able to create a script to detect new removable drives when inserted.

But my problem is how it can copy a file to the  detected drive.

here is the code. Please what am i doing wrong here

$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
GUICreate("")
GUIRegisterMsg($WM_DEVICECHANGE , "MyFunc")
Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)
    If $WParam == $DBT_DEVICEARRIVAL Then
    $dg = DriveGetDrive( "REMOVABLE" )
        For $i = 1 to $dg[0]
            $dt = DriveGetType( $dg[$i] )
            If $dt = "REMOVABLE" And $dg[$i] <> "a:" And $dg[$i] <> "b:" Then
                ;;look for my marker file at $dg[$i] , if found do check...

                FileCopy(@DesktopDir & "\todolist\Monday.txt", "REMOVABLE")
            EndIf
        Next
    EndIf
EndFunc
While 1
    $GuiMsg = GUIGetMsg()
WEnd

 

Edited by AlexFing17
Link to comment
Share on other sites

  • Moderators

Is that really how you have your code setup or is that your attempt at a reproducer script?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

On 5/8/2017 at 3:03 PM, SmOke_N said:

Is that really how you have your code setup or is that your attempt at a reproducer script?

got the script from this forum but am trying to make changes.

The file i want to be copied is the monday.txt . how i make the script copy the file to any detected removable drive

                FileCopy(@DesktopDir & "\todolist\Monday.txt", "REMOVABLE")
Link to comment
Share on other sites

  • Moderators

I'm not sure if you're asking for help with something you've tried to accomplish or if you have just failed to look up the functions you're using and how they should be used.  Which would mean you're asking us to write the code for you.

1.  Get removable drives... 

#include <AutoItConstants.au3>
Local $aDrives = DriveGetDrive($DT_REMOVABLE)
If Not IsArray($aDrives) Then
    ; error here, should return, no removeable drives
EndIf

2. Got removable drives, now make sure my directory I want to move file to exists

... assume I've enumerated to found drives through array
... eg. 
;   For $i = 0 To UBound($aDrives) - 1 ; am I indexing my drive?
;       If $aDrives[$i] = "the drive I'm looking for" Then
;           If FileExists("The direcotry I'm looking for") Then
;               FileCopy("The from directory and file I want", $aDrives[$i])
;           Else
;               ; some path functions here to get just directory structure or your variable to the directory structure you want
;               DirCreate($the_above_information_in_a_variable)
;               FileCopy("The from directory and file I want", $aDrives[$i] & "\" & $the_above_information_in_a_variable)
;           EndIf

 

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Alex,

Interesting diversion...this code will detect insertion of a usb drive and write a "day" text file to it based on the current day.  It will always overwrite the usb drives file...

#include <array.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <FileConstants.au3>

Opt("TrayMenuMode", 3)

; definitions for device arrival notification routine

local Const $DBT_DEVICEARRIVAL = 0x00008000
local Const $tagDEV_BROADCAST_HDR = "dword dbchsize;dword dbchdevicetype;dword dbchreserved"
local Const $DBT_DEVTYP_VOLUME = 0x00000002
local Const $tagDEV_BROADCAST_VOLUME = "dword dbcvsize;dword dbcvdevicetype;dword dbcvreserved;dword dbcvunitmask;short dbcvflags"
local Const $DBTF_MEDIA = 0x0001
local Const $DBTF_NET = 0x0002

Local $sDrive = ''

; array to get the day of week name...the _DateDayOfWeek function could have been used but I chose to do it this way...

Local $aDaysOfWeek[7][2] = [ _
        [1, 'Sunday'], _
        [2, 'Monday'], _
        [3, 'Tuesday'], _
        [4, 'Wednesday'], _
        [5, 'Thursday'], _
        [6, 'Friday'], _
        [7, 'Saturday'] _
        ]

GUICreate("")

; tray menu for script control / info

Local $lastdevicemenu = TrayCreateMenu('Last Detected Device and Status')
Local $lastdeviceitem = TrayCreateItem('None', $lastdevicemenu)
Local $exit = TrayCreateItem('Exit')

GUIRegisterMsg($WM_DEVICECHANGE, "MyFunc")

; routine to get unitmask from device arrival notification

Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)
    $iEvent = $WParam
    If $iEvent = $DBT_DEVICEARRIVAL Then
        $temp = DllStructCreate($tagDEV_BROADCAST_HDR, $LParam)
        $iDeviceType = DllStructGetData($temp, "dbchdevicetype")
        If $iDeviceType = $DBT_DEVTYP_VOLUME Then
            $struct = DllStructCreate($tagDEV_BROADCAST_VOLUME, $LParam)
            $iUnitMask = DllStructGetData($struct, "dbcvunitmask")
            $iFlags = DllStructGetData($struct, "dbcvflags")
            If Not BitAND($iFlags, $DBTF_MEDIA) And Not BitAND($iFlags, $DBTF_NET) Then
                $sDrive = FirstDriveFromMask($iUnitMask)
                ConsoleWrite($sDrive & " was inserted!" & @CRLF)
            EndIf
        EndIf
    EndIf
EndFunc   ;==>MyFunc

; routine to translate unitmask to volume name

Func FirstDriveFromMask($unitmask)
    Local $i
    For $i = 0 To 25
        If BitAND($unitmask, 0x1) Then ExitLoop
        $unitmask = BitShift($unitmask, 1)
    Next
    Return Chr($i + Asc('A')) & ':'
EndFunc   ;==>FirstDriveFromMask

While 1

    $msg = TrayGetMsg()
    Switch $msg
        Case $exit
            Exit
    EndSwitch

    ; if $sDrive is populated then do my copy routine

    If $sDrive Then

        If $sDrive = 'A:' Or $sDrive = 'B:' Then ContinueLoop
        If FileCopy(@ScriptDir & '\todolist\' & $aDaysOfWeek[@WDAY - 1][1] & '.txt', $sDrive & '\todolist\' & $aDaysOfWeek[@WDAY - 1][1] & '.txt', $FC_CREATEPATH + $FC_OVERWRITE) = 0 Then
            TrayItemSetText($lastdeviceitem, $sDrive & ' ToDo List Copy Failed - Day = ' & $aDaysOfWeek[@WDAY - 1][1])
        Else
            TrayItemSetText($lastdeviceitem, $sDrive & ' ToDo List Copy Successfull for ' & $aDaysOfWeek[@WDAY - 1][1])
        EndIf

        $sDrive = ''

    EndIf

WEnd

There's a lot to chew on here for a new user.  I hope you take the time to understand what is going on.

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

On 5/8/2017 at 11:09 PM, kylomas said:

Alex,

Interesting diversion...this code will detect insertion of a usb drive and write a "day" text file to it based on the current day.  It will always overwrite the usb drives file...

#include <array.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <FileConstants.au3>

Opt("TrayMenuMode", 3)

; definitions for device arrival notification routine

local Const $DBT_DEVICEARRIVAL = 0x00008000
local Const $tagDEV_BROADCAST_HDR = "dword dbchsize;dword dbchdevicetype;dword dbchreserved"
local Const $DBT_DEVTYP_VOLUME = 0x00000002
local Const $tagDEV_BROADCAST_VOLUME = "dword dbcvsize;dword dbcvdevicetype;dword dbcvreserved;dword dbcvunitmask;short dbcvflags"
local Const $DBTF_MEDIA = 0x0001
local Const $DBTF_NET = 0x0002

Local $sDrive = ''

; array to get the day of week name...the _DateDayOfWeek function could have been used but I chose to do it this way...

Local $aDaysOfWeek[7][2] = [ _
        [1, 'Sunday'], _
        [2, 'Monday'], _
        [3, 'Tuesday'], _
        [4, 'Wednesday'], _
        [5, 'Thursday'], _
        [6, 'Friday'], _
        [7, 'Saturday'] _
        ]

GUICreate("")

; tray menu for script control / info

Local $lastdevicemenu = TrayCreateMenu('Last Detected Device and Status')
Local $lastdeviceitem = TrayCreateItem('None', $lastdevicemenu)
Local $exit = TrayCreateItem('Exit')

GUIRegisterMsg($WM_DEVICECHANGE, "MyFunc")

; routine to get unitmask from device arrival notification

Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)
    $iEvent = $WParam
    If $iEvent = $DBT_DEVICEARRIVAL Then
        $temp = DllStructCreate($tagDEV_BROADCAST_HDR, $LParam)
        $iDeviceType = DllStructGetData($temp, "dbchdevicetype")
        If $iDeviceType = $DBT_DEVTYP_VOLUME Then
            $struct = DllStructCreate($tagDEV_BROADCAST_VOLUME, $LParam)
            $iUnitMask = DllStructGetData($struct, "dbcvunitmask")
            $iFlags = DllStructGetData($struct, "dbcvflags")
            If Not BitAND($iFlags, $DBTF_MEDIA) And Not BitAND($iFlags, $DBTF_NET) Then
                $sDrive = FirstDriveFromMask($iUnitMask)
                ConsoleWrite($sDrive & " was inserted!" & @CRLF)
            EndIf
        EndIf
    EndIf
EndFunc   ;==>MyFunc

; routine to translate unitmask to volume name

Func FirstDriveFromMask($unitmask)
    Local $i
    For $i = 0 To 25
        If BitAND($unitmask, 0x1) Then ExitLoop
        $unitmask = BitShift($unitmask, 1)
    Next
    Return Chr($i + Asc('A')) & ':'
EndFunc   ;==>FirstDriveFromMask

While 1

    $msg = TrayGetMsg()
    Switch $msg
        Case $exit
            Exit
    EndSwitch

    ; if $sDrive is populated then do my copy routine

    If $sDrive Then

        If $sDrive = 'A:' Or $sDrive = 'B:' Then ContinueLoop
        If FileCopy(@ScriptDir & '\todolist\' & $aDaysOfWeek[@WDAY - 1][1] & '.txt', $sDrive & '\todolist\' & $aDaysOfWeek[@WDAY - 1][1] & '.txt', $FC_CREATEPATH + $FC_OVERWRITE) = 0 Then
            TrayItemSetText($lastdeviceitem, $sDrive & ' ToDo List Copy Failed - Day = ' & $aDaysOfWeek[@WDAY - 1][1])
        Else
            TrayItemSetText($lastdeviceitem, $sDrive & ' ToDo List Copy Successfull for ' & $aDaysOfWeek[@WDAY - 1][1])
        EndIf

        $sDrive = ''

    EndIf

WEnd

There's a lot to chew on here for a new user.  I hope you take the time to understand what is going on.

kylomas

Thanks very much.from your code, i have been able to edit it to my like. Thank you

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