jbennett Posted October 21, 2008 Share Posted October 21, 2008 (edited) 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 October 21, 2008 by jbennett Link to comment Share on other sites More sharing options...
Zedna Posted October 21, 2008 Share Posted October 21, 2008 Use \ instead of / if FileExists($backup_location & "\Check_Backup.txt") then Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
jbennett Posted October 21, 2008 Author Share Posted October 21, 2008 (edited) Changed the code but still have the same errors popping up 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 October 21, 2008 by jbennett Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 21, 2008 Moderators Share Posted October 21, 2008 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 More sharing options...
Developers Jos Posted October 21, 2008 Developers Share Posted October 21, 2008 When the file doesn't exists the variables $Date and $fDate will not be initialised? 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 More sharing options...
jbennett Posted October 21, 2008 Author Share Posted October 21, 2008 (edited) 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 October 21, 2008 by jbennett Link to comment Share on other sites More sharing options...
Developers Jos Posted October 21, 2008 Developers Share Posted October 21, 2008 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 More sharing options...
jbennett Posted October 21, 2008 Author Share Posted October 21, 2008 (edited) How do I go about doing that?. Not sure Thanks edit ---- something like this..?? global $fDate = "" Edited October 21, 2008 by jbennett Link to comment Share on other sites More sharing options...
Developers Jos Posted October 21, 2008 Developers Share Posted October 21, 2008 (edited) 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 October 21, 2008 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 More sharing options...
PsaltyDS Posted October 21, 2008 Share Posted October 21, 2008 (edited) 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! Jos was all over that already... Edited October 21, 2008 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 More sharing options...
jbennett Posted October 21, 2008 Author Share Posted October 21, 2008 hehe. Thanks guys. I've got it all working now :-) 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