在 VBA 中删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/67835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting a file in VBA
提问by inglesp
Using VBA, how can I:
使用 VBA,我如何:
- test whether a file exists, and if so,
- delete it?
- 测试文件是否存在,如果存在,
- 删除它?
回答by Onorio Catenacci
1.) Check here. Basically do this:
1.)在这里检查。基本上这样做:
Function FileExists(ByVal FileToTest As String) As Boolean
FileExists = (Dir(FileToTest) <> "")
End Function
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:
我会让你去弄清楚所需的各种错误处理,但这些是我正在考虑的错误处理事项:
- Check for an empty string being passed.
- Check for a string containing characters illegal in a file name/path
- 检查传递的空字符串。
- 检查包含文件名/路径中非法字符的字符串
2.) How To Delete a File. Look at this.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:
2.) 如何删除文件。看看这个。基本上使用 Kill 命令,但您需要考虑文件为只读的可能性。这里有一个功能给你:
Sub DeleteFile(ByVal FileToDelete As String)
If FileExists(FileToDelete) Then 'See above
' First remove readonly attribute, if set
SetAttr FileToDelete, vbNormal
' Then delete the file
Kill FileToDelete
End If
End Sub
Again, I'll leave the error handling to you and again these are the things I'd consider:
同样,我将把错误处理留给你,这些是我会考虑的事情:
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?
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?
这对于目录和文件的行为是否应该有所不同?用户是否必须明确表明他们要删除目录?
您希望代码自动重置只读属性,还是应该向用户提供某种已设置只读属性的指示?
EDIT: Marking this answer as community wiki so anyone can modify it if need be.
编辑:将此答案标记为社区维基,以便任何人都可以在需要时对其进行修改。
回答by Mike Woodhouse
An alternative way to code Brettski's answer, with which I otherwise agree entirely, might be
对 Brettski 的答案进行编码的另一种方法,否则我完全同意,可能是
With New FileSystemObject
If .FileExists(yourFilePath) Then
.DeleteFile yourFilepath
End If
End With
Same effect but fewer (well, none at all) variable declarations.
相同的效果但更少(好吧,根本没有)变量声明。
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).
FileSystemObject 是一个非常有用的工具,非常值得使用。除此之外,对于文本文件写入,它实际上有时比传统替代方案更快,这可能会让一些人感到惊讶。(至少以我的经验,YMMV)。
回答by JohnFx
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!"
我可能会因此而受到抨击,但是如果您只是要删除它,那么测试其存在的意义何在?我最讨厌的一个问题是一个应用程序抛出一个错误对话框,内容类似于“无法删除文件,它不存在!”
On Error Resume Next
aFile = "c:\file_to_delete.txt"
Kill aFile
On Error Goto 0
return Len(Dir$(aFile)) > 0 ' Make sure it actually got deleted.
If the file doesn't exist in the first place, mission accomplished!
如果该文件首先不存在,则任务完成!
回答by Rich Adams
The following can be used to test for the existence of a file, and then to delete it.
下面可以用来测试一个文件是否存在,然后删除它。
Dim aFile As String
aFile = "c:\file_to_delete.txt"
If Len(Dir$(aFile)) > 0 Then
Kill aFile
End If
回答by Leo Moore
In VB its normally Dirto find the directory of the file. If it's not blank then it exists and then use Killto get rid of the file.
在VB中,它通常Dir会找到文件的目录。如果它不是空白,则它存在,然后用于Kill删除文件。
test = Dir(Filename)
If Not test = "" Then
Kill (Filename)
End If
回答by Brettski
set a reference to the Scripting.Runtime libraryand then use the FileSystemObject:
设置对 Scripting.Runtime 库的引用,然后使用 FileSystemObject:
Dim fso as New FileSystemObject, aFile as File
if (fso.FileExists("PathToFile")) then
aFile = fso.GetFile("PathToFile")
aFile.Delete
End if
回答by Nigel Heffernan
Here's a tip: are you re-using the file name, or planning to do something that requires the deletion immediately?
这里有一个提示:您是重新使用文件名,还是打算做一些需要立即删除的事情?
No?
不?
You can get VBA to fire the command DEL "C:\TEMP\scratchpad.txt" /F from the command prompt asynchronouslyusing VBA.Shell:
您可以让 VBA使用 VBA.Shell从命令提示符异步触发命令 DEL "C:\TEMP\scratchpad.txt" /F :
Shell "DEL " & chr(34) & strPath & chr(34) & " /F ", vbHide
Shell "DEL" & chr(34) & strPath & chr(34) & " /F ", vbHide
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.
请注意文件名周围的双引号(ASCII 字符 34):我假设您有一个网络路径或包含空格的长文件名。
If it's a big file, or it's on a slow network connection, fire-and-forget is the way to go. 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.
如果它是一个大文件,或者它的网络连接速度很慢,那么“即发即忘”是一种可行的方法。当然,你永远不会看到这是否有效;但是您会立即恢复您的 VBA,有时这比等待网络更好。

