Jump to content

Delete file only if exists


 Share

Recommended Posts

  • Moderators

OmarPapi,

Place the FileExists condition outside the MsgBox/FileRecycle code, which then only runs if the file exists..

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 by OmarPapi
Link to comment
Share on other sites

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

 

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

×
×
  • Create New...