Jump to content

DirCopy|FileMove|FileCopy and Error Checking


Recommended Posts

Good day,

I would like someone to get me started on the right foot, with devising "an error checking routine" for the following script:

; -----------------------------------------------
#include <FileConstants.au3>
; -----------------------------------------------
Opt("MustDeclareVars", 1)
; -----------------------------------------------
_UpdateUserData()
; -----------------------------------------------
Func _UpdateUserData()
    ; -----------------------------------------------
    ; Source data
    Local $_sSrcPath1 = "D:\Install\App_Install\Digital_Audio\2_GR5\Assets\Tapedeck"
    Local $_sSrcPath2 = "D:\Install\App_Install\Digital_Audio\2_GR5\Assets\User_Impulses"
    Local $_sSrcPath3 = "C:\RML\SAC\VST_PlugIns\Guitar Rig 5.dll"
    Local $_sSrcPath4 = "D:\Install\App_Install\Digital_Audio\2_GR5\Assets\Ampsim.ini"
    ; ----------------
    ; Destination data
    Local $_sDstPath1 = "C:\Program Files\Common Files\Native Instruments\Guitar Rig 5\Content\Tapedeck"
    Local $_sDstPath2 = "C:\Program Files\Native Instruments\Reflektor\User_Impulses"
    Local $_sDstPath3 = "C:\RML\SAC\VST_PlugIns\Ampsim\"
    Local $_sDstPath4 = "C:\RML\SAC\VST_PlugIns"
    ; -----------------------------------------------
    SplashTextOn("NOTICE!!", "Update User data...", 350, 50, -1, -1)
    Sleep(1000)
    ; -----------------------------------------------
    DirCopy($_sSrcPath1, $_sDstPath1, $FC_OVERWRITE)
    DirCopy($_sSrcPath2, $_sDstPath2, $FC_OVERWRITE)
    FileMove($_sSrcPath3, $_sDstPath3, $FC_OVERWRITE + $FC_CREATEPATH)
    FileCopy($_sSrcPath4, $_sDstPath4, $FC_OVERWRITE)
    ; -----------------------------------------------
    SplashTextOn("NOTICE!!", "Update User data completed...", 350, 50, -1, -1)
    Sleep(1000)
    SplashOff()
EndFunc   ;==>_UpdateUserData
; -----------------------------------------------

There are two questions:

Question #1: "Can the above be accomplished via a single 'If, Then, Else' statement?"

Question #2: Can the above be configured to employ arrays?

Any assistance in this matter would be greatly appreciated!

Edited by mr-es335
Link to comment
Share on other sites

Hi mr-es335,

here's one possible solution. Using an array with 3 Columns - Source, Destination and Mode.

If you don't need to copy one file and move the other file, you could make this thing easier to maintain. Kick out the last column and check if the Source is a file or a directory and then choose the appropriate action.

#include <FileConstants.au3>
Opt("MustDeclareVars", 1)

_UpdateUserData()

Func _UpdateUserData()
    ; Array with source paths, destination paths, and operation type (1 = DirCopy, 2 = FileCopy, 3 = FileMove)
    Local $aFileOperations[4][3] = [ _
        ["D:\Install\App_Install\Digital_Audio\2_GR5\Assets\Tapedeck", "C:\Program Files\Common Files\Native Instruments\Guitar Rig 5\Content\Tapedeck", 1], _
        ["D:\Install\App_Install\Digital_Audio\2_GR5\Assets\User_Impulses", "C:\Program Files\Native Instruments\Reflektor\User_Impulses", 1], _
        ["C:\RML\SAC\VST_PlugIns\Guitar Rig 5.dll", "C:\RML\SAC\VST_PlugIns\Ampsim\", 3], _
        ["D:\Install\App_Install\Digital_Audio\2_GR5\Assets\Ampsim.ini", "C:\RML\SAC\VST_PlugIns", 2] _
    ]

    SplashTextOn("NOTICE!!", "Update User data...", 350, 50, -1, -1)
    Sleep(1000)

    ; Perform copy/move operations with error handling
    For $i = 0 To UBound($aFileOperations) - 1
        Local $result = 0

        ; Determine the operation type based on the third column
        Switch $aFileOperations[$i][2]
            Case 1  ; DirCopy
                $result = DirCopy($aFileOperations[$i][0], $aFileOperations[$i][1], $FC_OVERWRITE)
            Case 2  ; FileCopy
                $result = FileCopy($aFileOperations[$i][0], $aFileOperations[$i][1], $FC_OVERWRITE)
            Case 3  ; FileMove
                $result = FileMove($aFileOperations[$i][0], $aFileOperations[$i][1], $FC_OVERWRITE + $FC_CREATEPATH)
        EndSwitch

        ; Error handling
        If $result = 0 Then
            MsgBox(16, "Error", "Failed to process: " & $aFileOperations[$i][0])
        Else
            ConsoleWrite("Successfully processed: " & $aFileOperations[$i][0] & @CRLF)
        EndIf
    Next

    SplashTextOn("NOTICE!!", "Update User data completed...", 350, 50, -1, -1)
    Sleep(1000)
    SplashOff()
EndFunc   ;==>_UpdateUserData

best regards,
Marc

P.S: this code is powered by ChatGPTĀ šŸ˜

AnyĀ of my own codes posted on the forum areĀ free for use by others without any restrictionĀ of any kind. (WTFPL)

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