Forum Discussion

wynandb1's avatar
wynandb1
Contributor
10 years ago

Verify value in text file

Good day 

 

I need to verify a specific value in a text file. It's a date value in YY/MM/DD format. It's in line 3 and starts at column 59 and ends at column 64.

 

From what I can see in the tutorials you can only compare files byte for byte . I want to verify if the date in the file , in line 3 between , column 59 and 64 is current date + 2 days. So the date in the file must be two days from today's date . I need to verify this specifically. 

 

Can someone perhaps help ?

 

Thank you

  •  

    function ReadDateFromFile(){
      var sPath = "PATHTOFILE";
      
      // Opens the specified file for reading
      var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
      
    // Skips to the specifed position and reads the specified amount of symbols
    myFile.SkipLine(NUMBEROFLINES); myFile.Skip(NUMBEROFCHARACTERS); var sDate = myFile.ReadString(10); // Closes the file myFile.Close(); }

    The above script should work to get just the date from the file, then you can set it to a variable and compare it like any other variable.

     

  • Or use aqFileSystem.FindFiles to find a file by mask:

     

    var foundFiles = aqFileSystem.FindFiles("C:\\FolderName\\", "FileName_*_*");
    if (foundFiles != null) {
      if (foundFiles.Count == 1) {
        var sPath = foundFiles.Next().Path;
    Log.Message(sPath);

    // Read data from the file
    // ...
    } else { Log.Error("More that 1 file found."); } } else { Log.Error("File not found."); }

     

  • cunderw's avatar
    cunderw
    Community Hero

     

    function ReadDateFromFile(){
      var sPath = "PATHTOFILE";
      
      // Opens the specified file for reading
      var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
      
    // Skips to the specifed position and reads the specified amount of symbols
    myFile.SkipLine(NUMBEROFLINES); myFile.Skip(NUMBEROFCHARACTERS); var sDate = myFile.ReadString(10); // Closes the file myFile.Close(); }

    The above script should work to get just the date from the file, then you can set it to a variable and compare it like any other variable.

     

    • wynandb1's avatar
      wynandb1
      Contributor

      I see that I forgot to mention that the file name is also a variable. Its changes every time I do a export e.g C:\FolderName\Filename_CurrentDate_12345

       

      So I would love to use the script, but this is stopping me .

       

       

       

      • cunderw's avatar
        cunderw
        Community Hero

        So build the string filepath programatically as well. Example for generating it with current date:

         

         

        var sPath, currentDate, sCurrentDate; 
          
        currentDate = aqDateTime.Today();
        sCurrentDate = aqConvert.DateTimeToFormatStr(currentDate,"%m%d%Y");
        sPath = "C:\\FolderName\\Filename_" + sCurrentDate + "_12345";

         

        This will set the sPath variable to: C:\FolderName\Filename_04212015_12345

  • What we did for something similar was open the text file, grab the text, and store it in a variable.  It becomes one long string at that point and then you can use aqString to search for your date at the correct position.