OmarPapi

Delete file only if exists

6 posts in this topic

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

 

Share this post


Link to post
Share on other sites



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

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by OmarPapi

Share this post


Link to post
Share on other sites

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

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by OmarPapi

Share this post


Link to post
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

 

1 person likes this

Share this post


Link to post
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

  • Similar Content

    • bouzzi
      By bouzzi
      I'm having troubles with FileExist function.
      My autoit version is 3.3.14.2.
      I'm working with C:\windows\System32 folder.
      On windows 7
      I use this script copied and modified from FileExist.au3 from the Autoit Help chm
      #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> Example() Func Example() ; Create a constant variable in Local scope of the filepath that will be read/written to. Local $sFilePath = "C:\windows\System32\PrintBrmUi.exe" ; Then try with : C:\windows\System32\printui.exe Local $sFileVersion = FileGetVersion($sFilePath, $FV_PRODUCTNAME) ;Local $iFileExists = FileExists($sFilePath) ; Display a message of whether the file exists or not. If FileExists($sFilePath) Then MsgBox($MB_SYSTEMMODAL, "", "The file " & $sFilePath & " exists. " & @CRLF & $sFileVersion) Else MsgBox($MB_SYSTEMMODAL, "", "The file " & $sFilePath & " doesn't exist." & @CRLF & $sFileVersion) EndIf EndFunc ;==>Example if the productname of  the file  doesn't have key words like Windows or Microsoft
      it say that the file do not exist...
       
      Try it it!
       
      Send me your feedbacks
       
      _________________________________________
      Bouzzi
       
    • gh0stid
      By gh0stid
      Hello, again ...
       
      Ive been working a lot on a script lately .. unfortunately I just.. cannot figure to get it to work ( im no coder.. )
      I start a new thread , cause the old one isoutdated, the code, have changed a lot since its creation
       
      so far..
      1 - I need to search for every files with specific extensions on C:\ G:\ H:\ drive and get their filesize. ---- Solved Using UDF _DirGetSizeEx
      2- Output everything to log --- Solved too
      3- Proceed to copy IF for drive G: ( another function ) @username match file ownership --- Not yet figured out.. 
      Tried that.. no succes so far... and im sure its pretty obvious why but once again, cant figure it out . . ( it is only a TEST' not the actual code.. ) 
      4- If ownership match, proceed to copy, to $storage, If upon copy FileExist, rename to $sFilename_1."extension of the file" --- having trouble here too ...
      heres the code.. 
       
      Thank you ... 
      btw. its really important for me, it for a Notes Migration of 1740 users... I dont want to do it by hand.. not, at all!