Deleting All Files In A Folder Containing Specific Text


ace5150

Recommended Posts

my sequence reads and then deletes a txt file from a folder on our network. If the .txt file is written again, before DF has a chance to delete it, a second file is created in the folder.

 

Ex.

test.txt

test0.txt

test1.txt

......

............

test85.txt.

 

Is it possible to delete all "instances" of this file from a sequence in DaqFactory?

 

something along the lines of

File.Delete("C:\Users\Scott\Documents\test*.txt")

 

or would I have to do each individually:

File.Delete("C:\Users\Scott\Documents\test.txt")

File.Delete("C:\Users\Scott\Documents\test0.txt")

..................

................................................

File.Delete("C:\Users\Scott\Documents\test85.txt")

Link to comment
Share on other sites

Use the getfilepathList() function to get a list of matching files, then cycle through and delete them.  Basically:

 

private string list = file.getFilePathList("c:\Users\Scott\Documents\test*.txt")

for (private i = 0, i < numrows(list), i++)

   file.delete(list)

endfor

 

I'm not sure delete() will take wildcards, but you can try it.  GetFilePathList() certainly does.

Link to comment
Share on other sites

Sorry, there was a typo in my script.  I do these snippets off the cuff, partially in hopes of teaching our customers how to do basic debugging.  Anyhow, list needs to be declared as a string.  I've edited my original post and tested it to verify it is correct.  

 

To debug that, I just added a ? list just before the delete() function call to see why I was getting the F0002, file not found error.  That printed NaN, which tells me that it tried to convert to a number and immediately told me my problem.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.