<p>Using VBA, how can I:</p>

<ol>
<li>test whether a file exists, and if so,</li>
<li>delete it?</li>
</ol>

[{'answer_id': 67853, 'author': 'Leo Moore', 'author_id': 6336, 'author_profile': 'https://Stackoverflow.com/users/6336', 'pm_score': 3, 'selected': False, 'text': '<p>In VB its normally <code>Dir</code> to find the directory of the file. If it\'s not blank then it exists and then use <code>Kill</code> to get rid of the file.</p>\n\n<pre><code>test = Dir(Filename)\nIf Not test = "" Then\n    Kill (Filename)\nEnd If\n</code></pre>\n'}, {'answer_id': 67858, 'author': 'Rich Adams', 'author_id': 10018, 'author_profile': 'https://Stackoverflow.com/users/10018', 'pm_score': 4, 'selected': False, 'text': '<p>The following can be used to test for the existence of a file, and then to delete it.</p>\n\n<pre><code>Dim aFile As String\naFile = "c:\\file_to_delete.txt"\nIf Len(Dir$(aFile)) &gt; 0 Then\n     Kill aFile\nEnd If \n</code></pre>\n'}, {'answer_id': 67860, 'author': 'Onorio Catenacci', 'author_id': 2820, 'author_profile': 'https://Stackoverflow.com/users/2820', 'pm_score': 9, 'selected': True, 'text': '<p>1.) Check <a href="http://www.tek-tips.com/faqs.cfm?fid=1936" rel="noreferrer">here</a>.  Basically do  this:</p>\n\n<pre><code>Function FileExists(ByVal FileToTest As String) As Boolean\n   FileExists = (Dir(FileToTest) &lt;&gt; "")\nEnd Function\n</code></pre>\n\n<p>I\'ll leave it to you to figure out the various error handling needed but these are among the error handling things I\'d be considering:</p>\n\n<ul>\n<li>Check for an empty string being passed.</li>\n<li>Check for a string containing characters illegal in a file name/path</li>\n</ul>\n\n<p>2.) How To Delete a File.  Look at <a href="http://word.mvps.org/faqs/macrosvba/DeleteFiles.htm" rel="noreferrer">this.</a>  Basically use the Kill command but you need to allow for the possibility of a file being read-only.  Here\'s a function for you:</p>\n\n<pre><code>Sub DeleteFile(ByVal FileToDelete As String)\n   If FileExists(FileToDelete) Then \'See above          \n      \' First remove readonly attribute, if set\n      SetAttr FileToDelete, vbNormal          \n      \' Then delete the file\n      Kill FileToDelete\n   End If\nEnd Sub\n</code></pre>\n\n<p>Again, I\'ll leave the error handling to you and again these are the things I\'d consider:</p>\n\n<ul>\n<li><p>Should this behave differently for a directory vs. a file?  Should a user have to explicitly have to indicate they want to delete a directory?</p></li>\n<li><p>Do you want the code to automatically reset the read-only attribute or should the user be given some sort of indication that the read-only attribute is set?  </p></li>\n</ul>\n\n<hr>\n\n<p>EDIT: Marking this answer as community wiki so anyone can modify it if need be.</p>\n'}, {'answer_id': 67861, 'author': 'Darrel Miller', 'author_id': 6819, 'author_profile': 'https://Stackoverflow.com/users/6819', 'pm_score': 2, 'selected': False, 'text': '<p>You can set a reference to the Scripting.Runtime library and then use the FileSystemObject.  It has a DeleteFile method and a FileExists method.</p>\n\n<p>See the MSDN article <a href="http://msdn.microsoft.com/en-us/library/aa164509(office.10).aspx" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 67956, 'author': 'JohnFx', 'author_id': 30018, 'author_profile': 'https://Stackoverflow.com/users/30018', 'pm_score': 4, 'selected': False, 'text': '<p>I\'ll probably get flamed for this, but what is the point of testing for existence if you are just going to delete it? One of my major pet peeves is an app throwing an error dialog with something like "Could not delete file, it does not exist!" </p>\n\n<pre><code>On Error Resume Next\naFile = "c:\\file_to_delete.txt"\nKill aFile\nOn Error Goto 0\nreturn Len(Dir$(aFile)) &gt; 0 \' Make sure it actually got deleted.\n</code></pre>\n\n<p>If the file doesn\'t exist in the first place, mission accomplished!</p>\n'}, {'answer_id': 67994, 'author': 'Brettski', 'author_id': 5836, 'author_profile': 'https://Stackoverflow.com/users/5836', 'pm_score': 3, 'selected': False, 'text': '<p><strong>set a reference to the Scripting.Runtime library</strong> and then use the FileSystemObject:</p>\n\n<pre><code>Dim fso as New FileSystemObject, aFile as File\n\nif (fso.FileExists("PathToFile")) then\n    aFile = fso.GetFile("PathToFile")\n    aFile.Delete\nEnd if\n</code></pre>\n'}, {'answer_id': 71236, 'author': 'Mike Woodhouse', 'author_id': 1060, 'author_profile': 'https://Stackoverflow.com/users/1060', 'pm_score': 6, 'selected': False, 'text': "<p>An alternative way to code Brettski's answer, with which I otherwise agree entirely, might be </p>\n\n<pre><code>With New FileSystemObject\n    If .FileExists(yourFilePath) Then\n        .DeleteFile yourFilepath\n    End If\nEnd With\n</code></pre>\n\n<p>Same effect but fewer (well, none at all) variable declarations.</p>\n\n<p>The FileSystemObject is a really useful tool and well worth getting friendly with. Apart from anything else, for text file writing it can actually sometimes be faster than the legacy alternative, which may surprise a few people. (In my experience at least, YMMV).</p>\n"}, {'answer_id': 26165910, 'author': 'Nigel Heffernan', 'author_id': 362712, 'author_profile': 'https://Stackoverflow.com/users/362712', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s a tip: are you re-using the file name, or planning to do something that requires the deletion immediately?</p>\n\n<p>No? </p>\n\n<p>You can get VBA to fire the command DEL "C:\\TEMP\\scratchpad.txt" /F  from the command prompt <em>asynchronously</em> using VBA.Shell:</p>\n\n<p>\n    Shell "DEL " &amp; chr(34) &amp; strPath &amp; chr(34) &amp; " /F ", vbHide \n</p>\n\n<p>Note the double-quotes (ASCII character 34) around the filename: I\'m assuming that you\'ve got a network path, or a long file name containing spaces.</p>\n\n<p>If it\'s a big file, or it\'s on a slow network connection, fire-and-forget is the way to go.\n Of course, you never get to see if this worked or not; but you resume your VBA immediately, and there are times when this is better than waiting for the network.</p>\n'}, {'answer_id': 69582730, 'author': 'Claudio', 'author_id': 9457690, 'author_profile': 'https://Stackoverflow.com/users/9457690', 'pm_score': 0, 'selected': False, 'text': "<p>A shorter version of the first solution that worked for me:</p>\n<pre><code>Sub DeleteFile(ByVal FileToDelete As String)\n   If (Dir(FileToDelete) &lt;&gt; &quot;&quot;) Then\n      ' First remove readonly attribute, if set\n      SetAttr FileToDelete, vbNormal\n      ' Then delete the file\n      Kill FileToDelete\n   End If\nEnd Sub\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/67835', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10439/']