Jump to content

Filename and Date Modified


Recommended Posts

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

Link to comment
Share on other sites

  • Developers
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.
  :)

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

@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 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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

 

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...