How to delete the content of a directory?

How to delete the content of a directory?

emanuela_berton
Not applicable
118 Views
4 Replies
Message 1 of 5

How to delete the content of a directory?

emanuela_berton
Not applicable

[ FlexSim 18.1.2 ]

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

0 Likes
Accepted solutions (1)
119 Views
4 Replies
Replies (4)
Message 2 of 5

jeff_nordgren
Not applicable

@emanuela.berton,

Not sure if this is what your looking for or not. It deals with only single files (at a time). Would something like this work for you?

From the User Manual:

fileopen (str filename [, str opentype])

Opens the FlexSim file interfaceDescriptionOpens the FlexSim file interface. Returns 1 if successful...




0 Likes
Message 3 of 5

emanuela_berton
Not applicable

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
Message 4 of 5

philboboADSK
Autodesk
Autodesk
Accepted solution
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.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.



Phil BoBo
Sr. Manager, Software Development
Message 5 of 5

emanuela_berton
Not applicable

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

0 Likes