当前位置: 动力学知识库 > 问答 > 编程问答 >

excel - How to delete files and folders in my documents folder without use of FSO command?

问题描述:

I want to delete all files and folders in the My Documents folder. I tried FSO create object command to delete files but not folders in the My Documents folder. Is there any other way to delete files and folders in the My Document folder?

This is my code.

Sub GetSpecialFolderPath()

Dim objSFolders As Object

Dim MyPath As String

Set objSFolders = CreateObject("WScript.Shell").SpecialFolders

MyPath = objSFolders("mydocuments")

Dim FSO As Object

Set FSO = CreateObject("scripting.filesystemobject")

MyPath = .Range("B2").Value

If Right(MyPath, 1) = "\" Then

MyPath = Left(MyPath, Len(MyPath) - 1)

End If

If FSO.FolderExists(MyPath) = False Then

MsgBox MyPath & " doesn't exist"

Exit Sub

End If

end sub

网友答案:

The code you propose doesn't work. You need to have two distinct variables for the path you get with "MyPath = objSFolders("mydocuments")" and the path you get with "MyPath = .Range("B2").Value"

Example: use MyDocumentsPath for the first path and MyTestPath for the second path and then write "If FSO.FolderExists(MyDocumentsPath & "\" & MyTestPath ) = False Then" for the test of the directory

分享给朋友:
您可能感兴趣的文章:
随机阅读: