can't KILL files with Hidden and Read-Only attributes (1 Viewer)

ghudson

Registered User.
Local time
Today, 18:38
Joined
Jun 8, 2002
Messages
6,195
I am using Access 97.

Using the Kill function, how can I delete files from a folder that have Hidden and/or Read-Only attributes?

I am using this but it will not remove the files(s) with Hidden and/or Read-Only attributes...

Kill "C:\My Documents\DataTest\" & "*.*"

My goal is to remove (RmDir) the "DataTest" folder but I can not do that with files in it.
RmDir "C:\My Documents\DataTest"

Thanks in advance for your help!
 
Last edited:

Alexandre

Registered User.
Local time
Tomorrow, 05:38
Joined
Feb 22, 2001
Messages
794
This will remove the hidden and archive attributes of a file.

Dim fs As Variant, f As Variant
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("YourFilePath")
f.Attributes = f.Attributes - (f.Attributes And (vbReadOnly + vbArchive))
Set fs = Nothing
Set f = Nothing

However, you'll have to build around a proc that will loop through the files within your folder.

Hope this helps
 

Alexandre

Registered User.
Local time
Tomorrow, 05:38
Joined
Feb 22, 2001
Messages
794
Seems that I have found something better still... The following will remove either individual files or entire directories and is not attributes-sensitive:

Code:
Public Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Public Function DeleteDir(strDirectory As String)
Dim SHFileOp As SHFILEOPSTRUCT

    With SHFileOp
        'Delete the file
        .wFunc = FO_DELETE
        'Select the file
        .pFrom = strDirectory
        'Allow 'move to recycle bn'
        .fFlags = FOF_ALLOWUNDO
    End With
    'perform file operation
    SHFileOperation SHFileOp
    MsgBox "The file '" + strDirectory + "' has been moved to your Recycling Bin !", vbInformation + vbOKOnly
End Function
 
Last edited:

ghudson

Registered User.
Local time
Today, 18:38
Joined
Jun 8, 2002
Messages
6,195
Alexandre,

That DeleteDir() is some function.

Is it possible to not have the "Confirm Folder Delete" message? Kinda like holding down the Shift key when pressing the Delete button to delete a file or folder without a warning or sending it to the recycle bin.

I am trying to remove a folder (if it exists) and I do not want the user to see the message nor have the ability to cancel the delete folder action.

Thanks for your help!
 

Alexandre

Registered User.
Local time
Tomorrow, 05:38
Joined
Feb 22, 2001
Messages
794
Declare and use the FOF_SILENT and FOF_NOCONFIRMATION constants as follows:

Code:
Public Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
Public Const FOF_SILENT = &H4
Public Const FOF_NOCONFIRMATION = &H10
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Public Function DeleteDir(strDirectory As String)
Dim SHFileOp As SHFILEOPSTRUCT

    With SHFileOp
        'Delete the file
        .wFunc = FO_DELETE
        'Select the file
        .pFrom = strDirectory
        'Allow 'move to recycle bn' with no warning/confirmation messages and no progress meter
        .fFlags = FOF_ALLOWUNDO Or FOF_SILENT Or FOF_NOCONFIRMATION
    End With
    'perform file operation
    SHFileOperation SHFileOp
End Function
 
Last edited:

ghudson

Registered User.
Local time
Today, 18:38
Joined
Jun 8, 2002
Messages
6,195
Alexandre,

That is awesome!!! :D

Thanks for your help!
 

Users who are viewing this thread

Top Bottom