jbiat Posted November 10, 2011 Share Posted November 10, 2011 (edited) I'm a newbie trying to write a script that launches an application we use on dozens of systems that creates a report on each system monthly and stores them in a folder. One report created once per month per system. The script would be installed on every system, and I want it to check if there has already been a report generated for the current month -- if there is a report already for the current month, then exit the script, but if no report yet this month it should run the report-generating app. Here's the steps I thought it should do: Find all report files in the folder Check the date modified timestamp on each file If any of the files have date modified = current month and current year, Then exit script If none of the files have date modified = current month and current year, Run the app that generates the reports The script does find each file and retrieves date modified and does compare it to current time. If there's a file with the current month, it exits just fine. But, the other part doesn't work -- if no files are found with the current month, the script fails to launch the app. I tested it on a folder that defiinitely did not contain any files with a current month timestamp, but still the script doesn't kick off the app. I also verified that the app is installed and in the right directory. After days spent reading AutoIt help and various forum posts, here's the code that I came up with: #Include <File.au3> #Include <Array.au3> Global $sReportsPath = "C:blahblahReports Folder" $aReportFileList =_FileListToArray($sReportsPath, "*.XML", 1) ;Array of report files in the Results folder. _ArrayDelete($aReportFileList, 0) ;Only want files, not count _ArrayDisplay($aReportFileList,"Report files listed") ;FOR TESTING For $sReportName In $aReportFileList Local $aModTime = FileGetTime($sReportsPath & "" & $sReportName) Local $yyyymm = $aModTime[0] & $aModTime[1] ;Get year and month of file If $yyyymm = @YEAR & @MON Then MsgBox(0, "Time compare true", "Same as this month: " & $yyyymm) ;FOR TESTING Exit ;Exit the Script because report has already been generated this month Else MsgBox(0, "Time compare false", "An earlier month: " & $yyyymm) ;FOR TESTING Run(path to the app that creates reports) EndIf Next What am I doing wrong? I looked into using the FindFileFirstFile and FindFileNextFile but don't understand it, so instead wound up using these arrays. Edited November 10, 2011 by jbiat Link to comment Share on other sites More sharing options...
Spiff59 Posted November 10, 2011 Share Posted November 10, 2011 (edited) If the _FileListToArray() finds no matches then $aReportFileList is not an array and your For/In loop will be bypassed. Maybe just modify your compare to this and let it fall through and launch your app when the loop doesn't execute: For $sReportName In $aReportFileList Local $aModTime = FileGetTime($sReportPath & "" & $sReportName) Local $yyyymm = $aModTime[0] & $aModTime[1] ;Get year and month of file If $yyyymm = @YEAR & @MON Then MsgBox(0, "Time compare true", "Same as this month: " & $yyyymm) ;FOR TESTING Exit ;Exit the Script because report has already been generated this month EndIf Next MsgBox(0, "Time compare false", "An earlier month: " & $yyyymm) ;FOR TESTING Run(path to the app that creates reports) PS - Are you in control of creating these reports? It might make life easier to plug the date into the filename when the report is created, like "2011.11.Log.xml", rather than messing with system calls to read last-modified file dates that may or may not reflect the actual date the report was created. Edit: And by the way... Welcome to the forum Edited November 10, 2011 by Spiff59 Link to comment Share on other sites More sharing options...
jbiat Posted November 10, 2011 Author Share Posted November 10, 2011 If the _FileListToArray() finds no matches then $aReportFileList is not an array and your For/In loop will be bypassed.Maybe just modify your compare to this and let it fall through and launch your app when the loop doesn't execute:PS - Are you in control of creating these reports? It might make life easier to plug the date into the filename when the report is created, like "2011.11.Log.xml", rather than messing with system calls to read last-modified file dates that may or may not reflect the actual date the report was created.Edit: And by the way... Welcome to the forum Thanks SpiffWhat's weird is that _FileListToArray() *is* finding matches. And FileGetTime is getting the times of each file found.You're right about plugging the date into the filename, in fact that's how we've got the reporting app set up. I think I could have the script check the filename, and that's actually my fallback plan. It's just that sometimes the reporting app fails to name the report file correctly, in which case the date might not be in the file name. So I was trying to come up with a more "failsafe" way of comparing dates than going by filename.I'll keep working on it. 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