VB6 列出并删除目录中的所有文件

发布于 2024-11-17 04:35:43 字数 429 浏览 2 评论 0原文

我需要获取目录中所有文件的名称,我当前正在使用此代码:

Dim File As String
File = Dir$(Environ("AppData") & "\*.exe")

Do While Len(File)
    MsgBox "Deleting: " & Environ("AppData") & "\" & File
Kill Environ("AppData") & "\" & File
    File = Dir$
Loop

这工作正常,但是它不显示隐藏/系统文件,或具有“正常”以外的任何属性的文件,我如何列出文件不管他们的属性如何?

我也尝试过,结果相同:

Kill Environ("AppData") & "\*.*"

I need to grab the names of all files in a directory, I am currently using this code:

Dim File As String
File = Dir$(Environ("AppData") & "\*.exe")

Do While Len(File)
    MsgBox "Deleting: " & Environ("AppData") & "\" & File
Kill Environ("AppData") & "\" & File
    File = Dir$
Loop

This works fine, however it does not display hidden/system files, or files with any attributes other than 'normal', how can I list files no matter their attributes?

I have tried this as well, which has the same outcome:

Kill Environ("AppData") & "\*.*"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-11-24 04:35:43

Dir 函数可以采用第二个属性参数:

File = Dir$(Environ("AppData") & "\*.exe", vbHidden & vbSystem)

您可以找到有关 Dir 函数的更多信息 此处

The Dir function can take a second parameter for attributes:

File = Dir$(Environ("AppData") & "\*.exe", vbHidden & vbSystem)

You can fin more about the Dir function here.

虚拟世界 2024-11-24 04:35:43

我几乎总是使用 Microsoft Scripting Runtime 从 VB6 进行文件 I/O。它只是做得更多,而且做得更好、更容易。一个微小的缺点是您的 VB 程序现在依赖于脚本运行时 DLL (scrrun.dll),您应该从 VB6 IDE 中将其添加为引用。

以下是删除文件夹中所有文件的示例。

' Note: This code is untested.

Sub Main()
    DeleteAllFilesInFolder Environ("App_Data")
End Sub

Sub DeleteAllFilesInFolder(strFolder As String)

    Dim fso As Scripting.FileSystemObject
    Dim objFolder As Scripting.Folder
    Dim objFile As Scripting.File

    Set fso = New Scripting.FileSystemObject
    Set objFolder = fso.GetFolder(strFolder)
    For Each objFile in objFolder.Files
        objFile.Delete force:=True
    Next

End Sub

I pretty much always use the Microsoft Scripting Runtime for file I/O from VB6. It just does more, and it does it better and more easily. One tiny downside is that your VB program is now dependent on the Scripting Runtime DLL (scrrun.dll), which you should add as a reference from within the VB6 IDE.

Here's an example that deletes all files from a folder.

' Note: This code is untested.

Sub Main()
    DeleteAllFilesInFolder Environ("App_Data")
End Sub

Sub DeleteAllFilesInFolder(strFolder As String)

    Dim fso As Scripting.FileSystemObject
    Dim objFolder As Scripting.Folder
    Dim objFile As Scripting.File

    Set fso = New Scripting.FileSystemObject
    Set objFolder = fso.GetFolder(strFolder)
    For Each objFile in objFolder.Files
        objFile.Delete force:=True
    Next

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文