OmarPapi Posted February 4, 2016 Share Posted February 4, 2016 Hello friends! I want to delete the file only if it exists, if not, must not appear msgbox code: $MyBox = MsgBox(1, "Cancel file", "the file records.dat exists .. want to delete it!") If $MyBox == 1 Then if FileExist then FileRecycle("records.dat") return false EndIf Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 4, 2016 Moderators Share Posted February 4, 2016 OmarPapi, Place the FileExists condition outside the MsgBox/FileRecycle code, which then only runs if the file exists.. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
OmarPapi Posted February 4, 2016 Author Share Posted February 4, 2016 (edited) 11 minutes ago, Melba23 said: M23 $MyBox = MsgBox(1, "Cancel file", "the file records.dat exists .. want to delete it!") If $MyBox == 1 Then EndIf if FileExist then FileRecycle("records.dat") return false EndIf no working Edited February 4, 2016 by OmarPapi Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 4, 2016 Moderators Share Posted February 4, 2016 OmarPapi, As you have no done what I suggested: 20 minutes ago, Melba23 said: Place the FileExists condition outside the MsgBox/FileRecycle code, which then only runs if the file exists I am not surprised. This is what I meant: #include <MsgBoxConstants.au3> $sFile = "records.dat" ; Check if file exists If FileExists($sFile) Then ; If it does then ask if it should be deleted If MsgBox($MB_OKCANCEL, "Cancel file", "the file records.dat exists .. want to delete it!") = $IDOK Then ; And delete it if required FileRecycle($sFile) ;Return False EndIf EndIf Please ask if you have any questions. M23 OmarPapi 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
OmarPapi Posted February 4, 2016 Author Share Posted February 4, 2016 (edited) 10 hours ago, Melba23 said: M23 this code is correct ? : $sFile = " records1.dat" $sFile2 = " records2.dat" $sFile3 = " records3.dat" $sFile4 = " records4.dat" If FileExists($sFile) Then If MsgBox($MB_YESNO, "Cancel file", "the file records1.dat exists .. want to delete it!") = $IDYES Then FileRecycle($sFile) EndIf EndIf If FileExists($sFile2) Then If MsgBox($MB_YESNO, "Cancel file", "the file records2.dat exists .. want to delete it!") = $IDYES Then FileRecycle($sFile2) EndIf EndIf If FileExists($sFile3) Then If MsgBox($MB_YESNO, "Cancel file", "the file records3.dat exists .. want to delete it!") = $IDYES Then FileRecycle($sFile3) EndIf EndIf If FileExists($sFile4) Then If MsgBox($MB_YESNO, "Cancel file", "the file records4.dat exists .. want to delete it!") = $IDYES Then FileRecycle($sFile4) EndIf EndIf Edited February 4, 2016 by OmarPapi Link to comment Share on other sites More sharing options...
InunoTaishou Posted February 5, 2016 Share Posted February 5, 2016 Did you try running the code and see if it works? Also, if you're going to be checking for a lot of files I'd suggest storing the file names in an array and using a for loop instead (or if your files are all named the same with an additional number you can forego the array and just use a string) ; Use an array to hold the names Local $aFiles[] = ["my file.txt", "my other file.txt", "some random file.bat", "broken script.au3", "random code.au3"] For $i = 0 to UBound($aFiles) - 1 ; Good practice to include the full path to the file, even if it's in the same directory as the script If (FileExists(@ScriptDir & "\" & $aFiles[$i])) Then If MsgBox($MB_YESNO, "Cancel file", "the file " & $aFiles[$i] & " exists .. want to delete it!") = $IDYES Then FileRecycle(@ScriptDir & "\" & $aFiles[$i]) EndIf EndIf Next ; Same concept just not using an array, works well if the files are all the same name but have sequential numbers For $i = 1 To 4 If (FileExists(@ScriptDir & "\records" & $i & ".dat")) Then If MsgBox($MB_YESNO, "Cancel file", "the file records" & $i & ".dat exists .. want to delete it!") = $IDYES Then FileRecycle(@ScriptDir & "\records" & $i & ".dat") EndIf EndIf Next OmarPapi 1 Link to comment Share on other sites More sharing options...
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