Jump to content

Problem with FileExists code :-(


 Share

Recommended Posts

Hi everyone,

I've got this really frustrating problem and wondering if someone could have a look at my code to help me get my head around what i'm doing wrong.

Basically I'm trying to get the code below to check if a file exists and get some information about the file. If the file doesn't exist then it should continue past this piece of code.

Hope I make sense :-) Thanks

Code below...

if FileExists($backup_location & "/Check_Backup.txt")then

$Date = FileGetTime($backup_location & "/Check_Backup.txt" ,1, 0)

$fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])

endif

If _DateDiff('d', $fDate, _NowCalc()) > 4 Then

FileDelete($backup_location & "/Check_Backup.txt")

EndIf

So if I put a msgbox at the end of this code without having the Check_Backup.txt file then it wouldn't show the msgbox. That's the problem Edited by jbennett
Link to comment
Share on other sites

Changed the code but still have the same errors popping up :P

I think it has something to do with the fact that the file doesn't exist.

If I create the file then it works fine, but theres a good reason why I don't want to do that as this is an initial check to see if the file exists.

cheers

EDIT

====

This is the error I get if the file doesn't exist..

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\JB\Desktop\Backup\test2008.au3"

C:\Users\JB\Desktop\Backup\test2008.au3 (21) : ==> Variable used without being declared.:

If _DateDiff('d', $fDate, _NowCalc()) > 4 Then

If _DateDiff('d', ^ ERROR

>Exit code: 1 Time: 0.240

and line 21 is highlighted in bold below

if FileExists($backup_location & "/Check_Backup.txt")then

$Date = FileGetTime($backup_location & "/Check_Backup.txt" ,1, 0)

$fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])

endif

If _DateDiff('d', $fDate, _NowCalc()) > 4 Then

FileDelete($backup_location & "/Check_Backup.txt")

EndIf

The code checks if the file exists and if it does then it will delete it if it's older than 4 days.

I'm guessing that I somehow need to bypass this if the file doesn't exist but for some reason my code isn't doing that.

Edited by jbennett
Link to comment
Share on other sites

  • Moderators

FileExists($backup_location & "/Check_Backup.txt")then

put a space between )then

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Yep :-)

That's the idea

This is what i've got now (Still fals over at the bit I highlighted in bold above)

if FileExists($backup_location  & "\Check_Backup.txt") then
   $Date = FileGetTime($backup_location  & "\Check_Backup.txt" ,1, 0)
   $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])
  
 endif
    If _DateDiff('d', $fDate, _NowCalc()) > 4 Then 
        FileDelete($backup_location  & "\Check_Backup.txt")
    EndIf
Edited by jbennett
Link to comment
Share on other sites

  • Developers

I was suggesting to you to ensure those variables are initialised always or else you get this error.

Add a Global statement at the top and when the file doesn't exists make sure you set them to "" or something or else they will contain wrong data.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Something like this will probably work:

#include<date.au3>
Global $Date, $fDate
Global $backup_location= "c:\whatever\directory"
If FileExists($backup_location & "\Check_Backup.txt") Then
    $Date = FileGetTime($backup_location & "\Check_Backup.txt", 1, 0)
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])
    If _DateDiff('d', $fDate, _NowCalc()) > 4 Then
        FileDelete($backup_location & "\Check_Backup.txt")
    EndIf
EndIf
Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Yep :-)

That's the idea

This is what i've got now (Still fals over at the bit I highlighted in bold above)

if FileExists($backup_location  & "\Check_Backup.txt") then
   $Date = FileGetTime($backup_location  & "\Check_Backup.txt" ,1, 0)
   $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])
  
 endif
    If _DateDiff('d', $fDate, _NowCalc()) > 4 Then 
        FileDelete($backup_location  & "\Check_Backup.txt")
    EndIf
If the file does not exist, you don't initialize $fDate, then you go ahead and do a _DateDiff() against it anyway...?

:(

Doh! :P Jos was all over that already...

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...