Sign in to follow this  
Followers 0
HahlefKazumoto

Newest File

7 posts in this topic

Hi Everyone,

i've just startet to script with AutoIT and im not realy used in the syntax of it too :(

I would like to "detect" the newest file in a spezific folder, take it and rename it (filemove) with a value i discript before..

by google i found a function from another user. I think this function is exactly doing what i want.. but i dont get, how i could get the value (variable?) out of the function and use be able to use it..

Here you can see my Script:

Func EvaluateFiles($Folder)

$avFiles = _FileListToArray($Folder & "\", "*",1)
If @Error<>0 Then
Return
EndIf

$iNewestTime = 11111111111111; YYYYMMDDhhmmss
$iNewestIndex = 0; Array index of newest file
; Find the newest file
For $p = 1 To $avFiles[0]
$iFileTime2 = Number(FileGetTime($Folder & "\" & $avFiles[$p], 0, 1))
If $iFileTime2 > $iNewestTime Then
$iNewestTime = $iFileTime2
$iNewestIndex = $p
EndIf
Next


If $iNewestIndex > 0 Then
$t = FileGetTime($Folder & "\" & $avFiles[$iNewestIndex])
$iDateCalc = _DateDiff( 'd',$t[0] & "/" & $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5],_NowCalc())
If $iDateCalc > 0 Then;<---Flags files that are older than today
MsgBox(16,"ATTENTION","This files in - " & $Folder & " - have not been updated today." & @CRLF & @CRLF & $avFiles[$iNewestIndex] & " is the newest file with a modified date of" & @CRLF & @CRLF & $t[1] & "/" & $t[2] & "/" & $t[0] & " " & $t[3] & ":" & $t[4] & ":" & $t[5])
EndIf
Else
MsgBox(16, "Error", "Failed to find a newest file.")
EndIf
EndFunc

$date1 = StringRight(@YEAR, 2)
$date9 = $date1 & @MON & @MDAY & @HOUR & @MIN & @SEC

$Fileformat1 = "ENG"&$date9&"C0Q002001"
$Fileformat2 = "ENG"&$date9&"C0Q002002"


$file = ("C:\Users\sx40211\desktop\Vorhanden\"&$Folder&".xlsx")
$file2 = ("C:\Users\sx40211\desktop\Vorhanden\Neue Datei\"&$Fileformat1&".xlsx")

FileMove($file, $file2, 1)

Realy? i dont get it ^^

im sure, that i have to call the function, to get a value.. Call("EvaluateFiles") right?

But how could i get now the name of the newest file?

Please help me anyone... my head is exploding :(

Share this post


Link to post
Share on other sites

Start by searching the forum!

Use something like "newest file", select "Only search in titles" and you will get a lot of hits.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I tried as you told and i didnt get any results..

Share this post


Link to post
Share on other sites

That's what I get:

post-7903-0-52234600-1363695023_thumb.pn


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

jep jep, sry now a have some results to... thanks a lot for the time you spend..

Share this post


Link to post
Share on other sites

Glad to be of service :)

We will be happy to answer further questions you might have.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Try this:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include-once
#include <Array.au3>
#include <Date.au3>
#include <File.au3>

;===============================================================================
; Function Name:   _FileListToArrayBetweenDates($sFolder, $sdateStart, $sdateEnd)
; Description::    returns the files between $sdateStart and $sdateEnd (start and enddate are included)
;
; Parameter(s):    $sFolder = folder which is searched
;                 $sdateStart    = the oldest date of filemodification to be returned
;                 $sdateEnd     = the youngest date of filemodification to be returned
;
; Requirement(s):  AutoIt 3.3.0.0
;
; Return Value(s): 2D Array with filename(s) and modificationdate
;
; Author(s):       autoBert (www.autoit.de)
;===============================================================================
Func _FileListToArrayBetween2Days($sFolder, $sdateStart = "", $sdateEnd = "")
;    If $sdateEnd = "" Then $sdateEnd = @YEAR & @MON & @MDAY
    If $sdateEnd = "" Then $sdateEnd = '99991231'
    Dim $aRes2D[1][2]
    $aRes = _FileListToArray($sFolder,'*',1) ;All Files
    If Not IsArray($aRes) Then
        SetError(-1)
    Else
        $j = 0 ;Counter for Resultarray
        ReDim $aRes2D[$aRes[0] + 1][2]
        For $i = 1 To $aRes[0]
            $sFileDate = FileGetTime($sFolder & "\" & $aRes[$i], 0, 1)
            ;ConsoleWrite($aRes[$i] & @TAB & $sFileDate & @CRLF)
            If $sFileDate >= $sdateStart And $sFileDate <= $sdateEnd Then
                $j += 1
                $aRes2D[$j][0] = $aRes[$i]
                $aRes2D[$j][1] = $sFileDate
            EndIf
        Next
        $aRes2D[0][0] = $j
        ReDim $aRes2D[$j + 1][2]
        _ArraySort($aRes2D, 1, 1, 0, 1)
        $aRes2D[0][1] = 'last modified'
    EndIf
    Return $aRes2D
EndFunc   ;==>_FileListToArrayBetween2Days

;Example:
;#cs
Global $a2D

$a2D = _FileListToArrayBetween2Days(@ScriptDir);creates a 2D-Array from beginn to end
ConsoleWrite('The last modified file: '&$a2D[1][0]&@TAB&'modified: ' &$a2D[1][1]&@CRLF)
_ArrayDisplay($a2D)
;#ce
and modify to your needs

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  
Followers 0