Zetoo Posted March 9, 2018 Posted March 9, 2018 Hi newbie here, I tried to create a script to get filename and date modified from latest backup file then I will print it in txt file. By using FileFIndFirstFIle, FileFindNextFile, and FileGetTime. But I dont know why it will show me oldest file instead of latest file $filepath = "C:\" $search = FileFindFirstFile($filepath & $modified & "*.bak") $filename = FileFindNextFile ($search, 0) FileGetTime($filepath, 0) Format backup file : Backup_ABC_DEF_yyyymmdd_1234567.bak
Developers Jos Posted March 9, 2018 Developers Posted March 9, 2018 1 hour ago, Zetoo said: But I dont know why it will show me oldest file instead of latest file ... because that is likely the first one in the directory when this is unsorted? 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.
Iczer Posted March 9, 2018 Posted March 9, 2018 You can use wildcards like"*" only one time in filename: $filepath = "C:\Folder\AnotherFolder" FileChangeDir($filepath & "\") $search = FileFindFirstFile("Backup_ABC_DEF_" & $modified & "*.bak") ;$search = FileFindFirstFile("*" & $modified & "_1234567.bak") $filename = FileFindNextFile ($search, 0) If @error Then Exit $sFileTime = FileGetTime($filepath & "\" & $filename, 1) ConsoleWrite("----> FileTime = " & $sFileTime & @CRLF)
KickStarter15 Posted March 9, 2018 Posted March 9, 2018 (edited) @Zetoo, Welcome to the AutoIt forum. Question: 1 hour ago, Zetoo said: But I dont know why it will show me oldest file instead of latest file What do you want to find/get? the time or date. In your code, you can only get the highest time vs the lower time, example: you have file.log (time modified 11:35am but the date is 2018/03/08) but you also have files.log (time modified is 09:30am but the date is 2018/03/09). So basically, the function return for code is to get the higher time which is 11:35am. Or maybe I'm wrong with my catch up. How about this: $filepath = "C:\" $search = FileFindFirstFile($filepath & "*.bak") $filename = FileFindNextFile ($search, 0) $time = FileGetTime($filename & ".log", 0); 0 if for modified date If Not @error Then $dmyyyy = $time[3]& ":" & $time[4] & ":" & $time[5]&'_'&$time[2]& "/" & $time[1] & "/" & $time[0] MsgBox(0, "File", "File found: " & $filename & @CRLF & "Date Modified: " & $dmyyyy) EndIf Or maybe this one: #include <GUIConstants.au3> #Include <File.au3> #Include <Array.au3> $filepath = "C:\" $FileList=_FileListToArray($filepath, "*.bak", 1) If (Not IsArray($FileList)) and (@Error=1) Then MsgBox (0,"","No Files\Folders Found.") Exit EndIf $latest = FileGetTime($FileList[1],0,1) $Filename = "(None)" For $x = 2 to $FileList[0] if FileGetTime($FileList[$x],0,1) > $latest Then $latest = FileGetTime($FileList[$x],0,1) $Filename = $FileList[$x] EndIf Next MsgBox(64,"Newest", $Filename & " is your most recent file " & @CRLF & "Created : " & $latest) Edited March 9, 2018 by KickStarter15 Modified... Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.
Zetoo Posted March 9, 2018 Author Posted March 9, 2018 7 hours ago, Jos said: ... because that is likely the first one in the directory when this is unsorted? Jos Yes, it might be possibly to the oldest file even thought FileGetTime($filepath, 0, 0) 7 hours ago, Iczer said: You can use wildcards like"*" only one time in filename: $filepath = "C:\Folder\AnotherFolder" FileChangeDir($filepath & "\") $search = FileFindFirstFile("Backup_ABC_DEF_" & $modified & "*.bak") ;$search = FileFindFirstFile("*" & $modified & "_1234567.bak") $filename = FileFindNextFile ($search, 0) If @error Then Exit $sFileTime = FileGetTime($filepath & "\" & $filename, 1) ConsoleWrite("----> FileTime = " & $sFileTime & @CRLF) Thanks, I will try it asap 7 hours ago, KickStarter15 said: @Zetoo, Welcome to the AutoIt forum. Question: What do you want to find/get? the time or date. In your code, you can only get the highest time vs the lower time, example: you have file.log (time modified 11:35am but the date is 2018/03/08) but you also have files.log (time modified is 09:30am but the date is 2018/03/09). So basically, the function return for code is to get the higher time which is 11:35am. Or maybe I'm wrong with my catch up. How about this: $filepath = "C:\" $search = FileFindFirstFile($filepath & "*.bak") $filename = FileFindNextFile ($search, 0) $time = FileGetTime($filename & ".log", 0); 0 if for modified date If Not @error Then $dmyyyy = $time[3]& ":" & $time[4] & ":" & $time[5]&'_'&$time[2]& "/" & $time[1] & "/" & $time[0] MsgBox(0, "File", "File found: " & $filename & @CRLF & "Date Modified: " & $dmyyyy) EndIf Or maybe this one: #include <GUIConstants.au3> #Include <File.au3> #Include <Array.au3> $filepath = "C:\" $FileList=_FileListToArray($filepath, "*.bak", 1) If (Not IsArray($FileList)) and (@Error=1) Then MsgBox (0,"","No Files\Folders Found.") Exit EndIf $latest = FileGetTime($FileList[1],0,1) $Filename = "(None)" For $x = 2 to $FileList[0] if FileGetTime($FileList[$x],0,1) > $latest Then $latest = FileGetTime($FileList[$x],0,1) $Filename = $FileList[$x] EndIf Next MsgBox(64,"Newest", $Filename & " is your most recent file " & @CRLF & "Created : " & $latest) Hi, thanks for welcoming this newbie I want to get both of date and time from latest date modified. Yes, I do realize my fault for not matching the date and time since am still learning autoit and want to try to display it separately (by the end of learning, I want to combine it so it will avoid those case mentioned above) Thanks for your help, I will try it asap
Subz Posted March 9, 2018 Posted March 9, 2018 Here is a function I've written previously to get the latest file based upon files modified, created, accessed date. Alternatively if you're just going to use the filename then just use _FileListToArray and _ArraySort then get the first or last file in the array (depending on which way you sort it).
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