jesternaut Posted February 14, 2008 Share Posted February 14, 2008 Hello, 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. CODEFunc _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 WEnd For $items In $array $split = StringSplit($items, '|') If $split[1] > $hold Then $hold = $split[1] $ret = $split[2] ElseIf $split[1] = $hold Then ExitLoop EndIf Next Return $ret EndFunc Thanks for any help. Link to comment Share on other sites More sharing options...
MrCreatoR Posted February 14, 2008 Share Posted February 14, 2008 (edited) 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 WEnd FileClose($hSearch) Return $sNewestFile EndFunc Edited February 14, 2008 by MsCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: 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 ProgramUDFs: 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 Examples: 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 ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
rasim Posted February 14, 2008 Share Posted February 14, 2008 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" EndSwitch $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 EndFunc 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