Jump to content

Recommended Posts

  • Moderators
Posted

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:

  Reveal hidden contents

 

  • Moderators
Posted

OmarPapi,

As you have no done what I suggested:

  On 2/4/2016 at 1:04 PM, Melba23 said:

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

Expand  

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:

  Reveal hidden contents

 

Posted (edited)
  On 2/4/2016 at 1:26 PM, Melba23 said:

M23

Expand  

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
Posted

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

 

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
×
×
  • Create New...