Forum Discussion

ArmandsBruns's avatar
ArmandsBruns
Frequent Contributor
13 years ago

Find File name "which has the current date and last time"

Hi,






Maybe someone knows and can help me with code.



I have a list of created excel files:

"Result_IE_25062012_15_01_27.xlsx"

"Result_IE_26062012_17_04_03.xlsx"

"Result_IE_27062012_15_05_44.xlsx"

"Result_IE_27062012_19_06_53.xlsx"






Is there any function or
method how to FIND the last changed or last created file and OPEN it ?



Best Regards



Armands










  • Hi Armands Bruns,


    This function is something I wrote that could be used for what you are looking to do. It searches on the DateLastModified attribute but that can be changed to DateCreated or DateLastAccessed as well.



    Note that it is written in JScript.





     function findLatestFileAndLaunch()

    {

        var filePath = "C:\\";

        var fileExtension = "*.exe";



        var i = 0;

        var filesInFolder = new Array();

        var fileInformation = new Array();

        var newestFile;



        //Grab the files that match the extension supplied from the folder specified.

        filesInFolder = aqFileSystem.FindFiles(filePath, fileExtension);

        //Grab the file details for comparison.

        while (filesInFolder.HasNext())

        {

            fileInformation = filesInFolder.Next();

            i++;

        }



        //Set newestFile to the first file that was found.

        newestFile = fileInformation[0];



        for (var a = 0; a < fileInformation.length; a++)

        {

            //Compare the files and find the most recent.

            if (aqDateTime.Compare(newestFile.DateLastModified, fileInformation.DateLastModified) == 1)

                newestFile = fileInformation
    ;

        }



        //Add the latest file as a TestedApp and Run it.

        var appIdx = TestedApps.Add(newestFile.Path);

        TestedApps.Items(appIdx).Run();

    }

  • ArmandsBruns's avatar
    ArmandsBruns
    Frequent Contributor
    Hi Mike



    Thanks for your support



    I tried to convert VBScript &colon;)

    but it shows me error:

    -----------------------------------------------------

    Function findLatestFileAndLaunch()



        filePath = "D:\"

        fileExtension = "Result_IE_*.xlsx"

        i = 0

        Set filesInFolder = Array()

        Set fileInformation = Array()

        Dim newestFile



        'Grab the files that match the extension supplied from the folder specified.

        filesInFolder = aqFileSystem.FindFiles(filePath, fileExtension)



        'Grab the file details for comparison.

        while filesInFolder.HasNext

           Set fileInformation(i) = filesInFolder.Next

           i = i + 1



        'Set newestFile to the first file that was found

        newestFile = fileInformation(0)

        For a = 0 To fileInformation.length-1

        

            'Compare the files and find the most recent.

            if (aqDateTime.Compare(newestFile.DateLastModified, fileInformation(a).DateLastModified) = 1) Then

            newestFile = fileInformation(a)

            End If

        Next    



        'Add the latest file as a TestedApp and Run it.

        Set appIdx = TestedApps.Add(newestFile.Path)

        TestedApps.Items(appIdx).Run()   

    End Function

    -----------------------------------------------------



    Armands
  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Armands,


    Some time ago, I created a similar script for one of our users. Please refer to the following forum thread and check the latest code snippet:

    http://community.smartbear.com/forum/post/?mode=singleThread&thread=d44c2a9c-1426-4199-8a55-8e19404c9228


    As for your script, as far as I can see, you've missed the WEnd operation:



    while filesInFolder.HasNext

           Set fileInformation(i) = filesInFolder.Next

           i = i + 1

        wend  ' <- Right here


    I hope this helps :)