Forum Discussion

prasath_2k's avatar
prasath_2k
Occasional Contributor
3 years ago

Fetching file

Hi experts,

I have to select a file from a folder to open it and do some operations on it.

I need to do two things;

1. I wanted to fetch always the "latest added file" from the folder.

2. I also wanted to fetch the "second latest added file" from the folder.

 

How do carry out these operations?

Image1_ Folder and files in it

With reference to the above image; I wanted to fetch the "latest version of the installer" (ie V1.0.48 according to the given folder) and also the "one version lesser to latest"(ie V1.0.47 according to the given folder). I wanted to fetch the latest and next latest files always even if  "N" number of files are added. So can anyone guide some foolproof way to achieve this?

 

Thanks a lot!!

 

6 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    I would use aqFile or/and aqFileSystem objects provided by TestComplete to get a list of files in the folder. Then, depending on your needs/most reliable criteria, I would sort this list by either date/time, or file name, or file (installer) version and proceed.

     

  • Mr_Bro's avatar
    Mr_Bro
    Frequent Contributor

    prasath_2k 

    as AlexKaras  mentioned you can make use of aqFileSystem Object to achieve this .

    please find the sample snippet for your reference were you have a peace of code which will log the last modified details.

    def GetTimeExample():
      sPath = "C:\\Temp\\TempFile.txt"
      # Creates a file
      aqFile.Create(sPath)
      FileInf = aqFileSystem.GetFileInfo(sPath)
      # Delays the script execution
      Delay(2000)
      # Writes data to the file
      FileInf.WriteToTextFile("Hello, World!", aqFile.ctANSI);
      # Delays the script execution
      Delay(2000)
      # Reads the file's content
      FileInf.ReadWholeTextFile(aqFile.ctANSI)
      
      # Posts the file's creation time to the test log
      Log.Message(FileInf.DateCreated)
      # Posts the file's last write time to the test log
      Log.Message(FileInf.DateLastModified)
      # Posts the file's last access time to the test log
      Log.Message(FileInf.DateLastAccessed)
      
      # Deletes the file
      FileInf.Delete()

     

  • prasath_2k's avatar
    prasath_2k
    Occasional Contributor

    AlexKaras Mr_Bro 

    Thanks for your reply. Yes, I am using the same aqFileSystem related functions to handle the files. Now, is there any particular function to sort and acess a particular file of my choice?

     

     With the above code I have written, I am able to sort the installer file names based on version and get the latest one(below logs)

     

    Now I wanted to collect this sorted file path in array and operate with it? Like from array of paths I need to fetch lastest and second latest version and further work with it. How to push these sorted file paths into a array and again select a particular one path from it?

     

    Plz do suggest a method for it!

     

    Thanks once again

    • Mr_Bro's avatar
      Mr_Bro
      Frequent Contributor

      prasath_2k 

      when you are looping through the DTM files you can add each data to a JSON object or Array Object or dictionary object and once fetching the complete info you can do your required operation.

  • prasath_2k's avatar
    prasath_2k
    Occasional Contributor

    Mr_Bro AlexKaras 

    Just add on to above;

    when I try to create a array and push the DTM versions(sorted ones) into it. wrong array is getting printed. I wanted array to be arr= [V.1.0.38,V.1.0.40,V.1.0.47,V.1.0.48] but the resultant array is coming as V.1.0.48 printed 4times!

     

    can you help me in fixing this?

     

    Thanks a lot!

    Resultant array which I get!

    Pushing the DTM versions into Array

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      prasath_2k :

       

      Hi,

       

      but the resultant array is coming as V.1.0.48 printed 4times!

      The reason for this is the following:

      -- Resulting array is declared as 'var arr = []' within the loop. This means that the array is recreated with every loop iteration and the previously created array gets disposed of;

      -- Because array is declared using var (but not let) keyword array remains accessible outside of the loop;

      -- While your 'for' loop iterates through the number of found files, you are pushing the same value into array on each loop iteration, thus you are getting four same values for the latest version to be printed to the log.

       

      Considering the fact that installer file names are uniform, I would collect file names into an array, sort this array (this will sort file names in ascending or descending order - check this in debugger) and then use either first or last two array elements (depending on sort results).

      Something like this:

      let DTMFileItem;

      let DTMName;

      let arr = [];

      while (DTMFiles.HasNext()) {

        DTMFileItem = DTMFiles.Next();

        DTMName = ...

        arr.push(DTMName);

      }

      arr.sort();

      // Depending on the sort results the latest installer file name will be either in 

      // arr[0]

      // or

      // arr[arr.length - 1]

      // array element