mr-es335 Posted 5 hours ago Posted 5 hours ago (edited) Good day, I have been working with ioa747's most excellent backup|restore script [Click_me], to meet the following 4 conditions: 1st condition: ; If both exist, then call Number 1 2nd condition: ; If only $sourceFile1 exists, then call Number 2 3rd condition: ; If only $sourceFile2 exists, then call Number 3 4th condition: ; If neither $sourceFile1 and $sourceFile2 exist, then call Number 4 Here is the sample test script: Spoiler expandcollapse popup; Remove necessary comments for testing! ; If both exist, then call Number 1 ;~ Local $sSourceData1 = "Path to file|folder" ;~ Local $sSourceData2 = "Path to file|folder" ; If only $sSourceData1 exists, then call Number 2 ;~ Local $sSourceData1 = "Path to file|folder" ;~ Local $sSourceData2 = "" ; If only $sSourceData2 exists, then call Number 3 ;~ Local $sSourceData1 = "" ;~ Local $sSourceData2 = "Path to file|folder" ; If neither $sSourceData2 and $sSourceData2 exist, then call Number 4 ;~ Local $sSourceData1 = "" ;~ Local $sSourceData2 = "" ; Number 1 If FileExists($sSourceData1) And FileExists($sSourceData2) Then MsgBox(0, "Notice", "Both files exist...exiting!", 2) Exit EndIf ; Number 2 If FileExists($sSourceData1) And Not FileExists($sSourceData2) Then MsgBox(0, "Notice", "$sSourceData2 does not exist...so, backup", 2) EndIf ; Number 3 If FileExists($sSourceData2) And Not FileExists($sSourceData1) Then MsgBox(0, "Notice", "$sSourceData1 does not exist...so, restore", 2) EndIf ; Number 4 ;~ If Not FileExists($sSourceData2) And Not FileExists($sSourceData1) Then ; Not required?? MsgBox(0, "Notice", "$sSourceData1 and $sSourceData2 do not exist...so, cancel", 2) Exit EndIf I have the following 2 questions to ask of you... Question 1: To please critique the above? Question 2: Would Select|EndSelect or Switch|Case be more beneficial - from a purely "cosmetic perspective" I mean, than implementing If|Then? As always...any assistance in this matter would be greatly appreciated! Edited 4 hours ago by mr-es335 mr-es335 Sentinel Music Studios
Solution ioa747 Posted 4 hours ago Solution Posted 4 hours ago Why this approach is better FileExists() is executed exactly once per variable. The switch...case structure reads like a table, making the code highly readable and easy to maintain. ; Remove necessary comments for testing purposes! ; If both exist, then call Number 1 ; Local $sourceFile1 = "D:\System_Data\Scripts\BackUp\Complete\Working\1_Src\" ; Local $sourceFile2 = "D:\System_Data\Scripts\BackUp\Complete\Working\2_Dst\" ; If only $sourceFile1 exists, then call Number 2 ; Local $sourceFile1 = "D:\System_Data\Scripts\BackUp\Complete\Working\1_Src\" ; Local $sourceFile2 = "" ; If only $sourceFile2 exists, then call Number 3 ; Local $sourceFile1 = "" ; Local $sourceFile2 = "D:\System_Data\Scripts\BackUp\Complete\Working\2_Dst\" ; If neither $sourceFile1 and $sourceFile2 exist, then call Number 4 ; Local $sourceFile1 = "" ; Local $sourceFile2 = "" ; Calculate state bitmask (Source1 = bit 1, Source2 = bit 0) Local $iState = (FileExists($sourceFile1) ? 2 : 0) + (FileExists($sourceFile2) ? 1 : 0) Switch $iState Case 3 ; Both exist (File 1 & File 2) MsgBox(0, "Notice", "Both files exist...exiting!", 2) Exit Case 2 ; Only sourceFile1 exists MsgBox(0, "Notice", "$sourceFile2 does not exist...so, backup", 2) Case 1 ; Only sourceFile2 exists MsgBox(0, "Notice", "$sourceFile1 does not exist...so, restore", 2) Case 0 ; Neither exists MsgBox(0, "Notice", "$sourceFile1 and $sourceFile2 do not exist...so, cancel", 2) Exit EndSwitch mr-es335 1 I know that I know nothing
mr-es335 Posted 3 hours ago Author Posted 3 hours ago "Incroyable!" The use of the Ternary Operator in this instance has greatly simplified this script! "WOW!" and "WOW!...WOW!" Thank you very, very, ioa747!! Appreciated...as always! ioa747 1 mr-es335 Sentinel Music Studios
mr-es335 Posted 1 hour ago Author Posted 1 hour ago (edited) How is this...with "test" Function call? Spoiler expandcollapse popup; As ioa747_1b with Function call ; Remove necessary comments for testing purposes! #include <ExtMsgBox.au3> Opt("MustDeclareVars", 1) ;~ Opt("MouseCoordMode", 0) _ExtMsgBoxSet(64, 4, Default, Default, 14, "Segoe UI Semibold", 1000) Local $sMsg1 = "Both SrcData AND DstData exist...exiting... " Local $sMsg2 = "Only SrcData exists...so, backup... " Local $sMsg3 = "Only DstData exists...so, restore... " Local $sMsg4 = "Neither SrcData AND DstData exist...so, cancelling... " ; If both exist, then call Case 3 ;~ Local $sSourceData1 = "D:\System_Data\Scripts\BackUp\Complete\Working\1_Src\" ;~ Local $sSourceData2 = "D:\System_Data\Scripts\BackUp\Complete\Working\2_Dst\" ; If only $sSourceData1 exists, then call Case 2 ;~ Local $sSourceData1 = "D:\System_Data\Scripts\BackUp\Complete\Working\1_Src\" ;~ Local $sSourceData2 = "" ; If only $sSourceData2 exists, then call Case 1 ;~ Local $sSourceData1 = "" ;~ Local $sSourceData2 = "D:\System_Data\Scripts\BackUp\Complete\Working\2_Dst\" ; If neither $sSourceData1 and $sSourceData2 exist, then call Case 0 ;~ Local $sSourceData1 = "" ;~ Local $sSourceData2 = "" ; Calculate state bitmask (Source1 = bit 1, Source2 = bit 0) Local $iState = (FileExists($sSourceData1) ? 2 : 0) + (FileExists($sSourceData2) ? 1 : 0) Switch $iState Case 3 ; Both exist (File 1 & File 2) _ExtMsgBox(64, " ", " Notice!", $sMsg1, 2) Exit Case 2 ; Only sourceFile1 exists _ExtMsgBox(64, " ", " Notice!", $sMsg2, 2) BackupRestore(True) Case 1 ; Only sourceFile2 exists _ExtMsgBox(64, " ", " Notice!", $sMsg3, 2) BackupRestore(False) Case 0 ; Neither exists _ExtMsgBox(64, " ", " Notice!", $sMsg4, 2) Exit EndSwitch Func BackupRestore($sMyArg) If $sMyArg = "True" Then ConsoleWrite("Calling BackupRestore(" & $sMyArg & ")" & @CRLF) Else ConsoleWrite("Calling BackupRestore(" & $sMyArg & ")" & @CRLF) EndIf EndFunc ;==>BackupRestore Edited 1 hour ago by mr-es335 ioa747 1 mr-es335 Sentinel Music Studios
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now