Jump to content
Sign in to follow this  

Finding the newest file in a directory?

Recommended Posts



In my code I'm trying to locate the name of the newest file in a directory. Below is the code I'm using, whats strange is while stepping thru the array the while loop will stop expanding the array but will still step to the next file and continues to the end of the directory but will not place the data into the array element. The array stops expanding after the 11th file. Am I not initializing the array properly? Do you have a better/shorter way to do this.

$arg argument is a directory path. It seems like the while loop stops doing the Redim $array after the 11th pass so the array stops expanding but continues to loop through the file names.

This is the code.

Func _GetNewestFile($arg)

If StringRight($arg, 1) <> '\' Then $arg &= '\' ;search path should in a \ if not add it

Local $first = FileFindFirstFile($arg & '*.*'), $array[1], $hold, $ret, $split ;locates the first file and inits some vars

While 1

$found = FileFindNextFile($first)

If @error Then ExitLoop

If StringInStr(FileGetAttrib($arg & $found), 'D') Then ContinueLoop ;skips directories

$array[uBound($array) - 1] = FileGetTime($arg & $found, 1, 1) & '|' & $arg & $found ;places file time and path in last array element

ReDim $array[uBound($array) + 1] ; expands array by one


For $items In $array

$split = StringSplit($items, '|')

If $split[1] > $hold Then

$hold = $split[1]

$ret = $split[2]

ElseIf $split[1] = $hold Then




Return $ret


Thanks for any help.

Share this post

Link to post
Share on other sites

Hi, try this func:

$NewsetFileName = _GetNewestFileName(@ScriptDir, 0)

MsgBox(64, "Results", "Newest File Name (Modified): " & $NewsetFileName)

Func _GetNewestFileName($sPath, $iMode=0) ;$iMode is the "Option" parameter for the FileGetTime() function.
    Local $sNewestFile = ""
    Local $sNextFile, $iNewsetFileSize, $iCurrentFileSize
    Local $hSearch = FileFindFirstFile($sPath & "\*.*")
    If $hSearch = -1 Then Return SetError(1, 0, -1)
    While 1
        $sNextFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If StringInStr(FileGetAttrib($sPath & "\" & $sNextFile), "D") Then ContinueLoop
        $iNewsetFileSize = FileGetTime($sPath & "\" & $sNewestFile, $iMode, 1)
        $iCurrentFileSize = FileGetTime($sPath & "\" & $sNextFile, $iMode, 1)
        If $sNewestFile = "" Or $iCurrentFileSize > $iNewsetFileSize Then $sNewestFile = $sNextFile
    Return $sNewestFile
Edited by MsCreatoR



Using OS: Win 7 Professional, Using AutoIt Ver(s): /

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...


AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *





AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post

Link to post
Share on other sites

Hi guys!

Example from MsCreatoR nice, but if folder contain very many files, then better use a DIR command.

$GetNewFile = _GetNewestFile(@ScriptDir, 0) ;second param - 0 = Modified, 1 = Created, 2 = Accessed
MsgBox(0, "Newest file is ", $GetNewFile)

Func _GetNewestFile($sPath = @ScriptDir, $sOption = 0)
    Local $iDir, $iReadOut, $ShortPath
    Switch $sOption
    Case 0
        $sOption = "w"
    Case 1
        $sOption = "c"
    Case 2
        $sOption = "a"
    $ShortPath = FileGetShortName($sPath)
    $iDir = Run(@ComSpec & " /c dir " & $ShortPath & " /a:-d /b /o:-d /t:" & $sOption, "", @SW_HIDE, 2 + 4)
    $iReadOut = StdoutRead($iDir)
    Return $sPath &"\"& $iReadOut


Share this post

Link to post
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

Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.