question

emanuela.berton avatar image
0 Likes"
emanuela.berton asked emanuela.berton commented

How to delete the content of a directory?

Good morning, I'm writing in a specific folder some txt files as log. Before start to write any file I want to delete the folder content or at least delete some files in it, but I don't find the right command to do this. Can you help me? Thanks

FlexSim 18.1.2
flexsim 18.1.2delete file
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

emanuela.berton avatar image emanuela.berton Jeff Nordgren commented ·

Thanks for your answer but it is not exactly what I need. I know how to create and write files, but my situation is this.

I have a directory, ex. C:\temp\logfile, where I write several files similar to these event1.txt, event2.txt, event3.txt, ecc. While my model is running I catch some events and for each fired event I write a specific file with a suffix (progressive counter or other) with some specific information. When I reset the model I would delete all files in the directory in order to start from zero with my data with a empty situation. I read some old posts of about 2 years ago where this is not possible using Flexsim instructions, but I hope you should find other way to do the same thing. Thanks for any suggestion.

0 Likes 0 ·

1 Answer

·
Phil BoBo avatar image
3 Likes"
Phil BoBo answered emanuela.berton commented
string directory = "C:\\temp\\logfile\\";
treenode temp = model().first.subnodes.assert("temp");
temp.subnodes.clear();
getfilesindirectory(directory, "txt", temp);
for (int r = 1; r <= temp.subnodes.length; r++) {
    string fileName = temp.subnodes[r].value;
    applicationcommand("deletefile", directory + fileName);
}

Note that deleting files is not immediate. When you programmatically delete a file in Windows, it marks it for deletion and then actually deletes it later.

So if you query whether the file is still there on the line immediately after "deletefile," then the file will probably still exist. You need to separate your file deletion from your querying or creation of those files by some amount of real time.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

emanuela.berton avatar image emanuela.berton commented ·

Thanks Phil, it is exactly what I need. I updated my model and it works as I expected.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.