fail to delete files using the delete command in R2018a
Afficher commentaires plus anciens
Hi, everyone,
The current dir is F:\Test
I tested to delete files using the following commands:
TestName = 'F:\Test\Test\Test.xlsx'
delete TestName
But it failed to work:

When I set the current dir to F:\Test\Test and using the following commands:
TestName = 'Test.xlsx'
delete TestName
It also failed to work:

But when I using the delete 'Test.xlsx' command, it succeeded:

Does anyone have the same problem or who can tell me why?
Thank you very much in advance.
1 commentaire
Stephen23
le 21 Mai 2018
"Does anyone have the same problem or who can tell me why?"
Because you are using command syntax, which interprets all trailing inputs as literal strings. So when you enter this:
Delete TestName
it is exactly equivalent to this:
Delete('TestName')
Thus none of your examples (except the last one) are trying to delete that file. If you want to use a variable as an input argument to a function then you will need to use function syntax, as Walter Roberson showed. Note also that none of the examples in the MATLAB help show command syntax being used with an input argument.
Réponse acceptée
Plus de réponses (1)
Deleting a file may fail even if that file has not being used. I tried to use Java io functions to delete files, but it still can't guarantee success.
My wordaround:
Write a delete function contains a while loop to check if the file has already been deleted. Use an expiration time as input.
It can't guarantee 100% success, but greatly increase your success rate.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!