Forum Discussion

Tris's avatar
Tris
Contributor
5 years ago

Delete File methods not working

Hi, 

 

I've been attempting to pass in a string to either the aqFileSystem.DeleteFile and/or aqFile.Delete methods in order to delete a single file based on the string, passed in. I am having an issue where the test runs and successfully finishes, however the file is not deleted.

 

If I manually take the string passed in and hard code the value for deletion, the file is deleted, I cannot see why this works with the hard coded string, but not the passed in value, see below example.

 

// At this point a file has been identified for deletion and we are simply creating the expected string value to pass in using the file path.
function OldLogsDelete(aFile){
  var DateTime = aqDateTime.Now();
  var DateTimeMinusXDays = aqDateTime.AddDays(DateTime, -14);
  if (aFile.DateCreated < DateTimeMinusXDays)
    {
      FilePathString = aqConvert.VarToStr(aFile.Path)
      var FilePathString = aqString.Replace(FilePathString, "\\", "\\\\");
      aFile = '"' + FilePathString + '"'
      aqFile.Delete("aFile");
      Log.Message(aFile + "deleted" );
    }

When executed, we get the below output - note this matched the expected value as per example in https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfile/delete.html:

Really struggling to see what is wrong here as if I just copy the string and paste it in the method instead of the aFile value, then it works fine.

 

Thanks in advance for any help/suggestion,

Tris

  • In the example you provided, the path to the file you want to delete is badly created dynamically.

     

    See my attached file.

     

    So if you specify the path manually it works properly and if you specify it by the .Path method it doesn't work because you're formatting it badly.

     

     

  • santi's avatar
    santi
    Contributor

    1. Is that folder or file accessible 

    2. Do you have admin permission while executing script

    3. what if Log.Message(.....) not there inside function ? is it still throws error ?

    4. Why dont you just return a bool at end of function and see the file status.

    • Tris's avatar
      Tris
      Contributor

      Hi Santi,

       

      1. The file/folder is accessable
      2. I am running TestComplete as admin (and have full admin rights on the PC it is running on)
      3. I attempted to run with or without the message, not no error is thrown, I took the screenshot during execution to show string passed in
      4. I don't think the affects the method's function

      Thanks,

      Tris

      • Wamboo's avatar
        Wamboo
        Community Hero

        In the example you provided, the path to the file you want to delete is badly created dynamically.

         

        See my attached file.

         

        So if you specify the path manually it works properly and if you specify it by the .Path method it doesn't work because you're formatting it badly.