Solved

A program to delete all files and folders within a folder, on a win2000 machine with restricted user rights.

Posted on 2003-10-31
14
319 Views
Last Modified: 2010-05-01
At work(an IT learning centre) we have 40 machines. Students are supposed to save all their work onto their floppy disks, purely to keep the machines free from being clogged up with crap.
They are Windows 2000, and they are all left logged on with a standard restricted account called Student.

I have been tasked with finding a way to keep the My Documents folder clear. There are two choices here: (1) I could try to find a way to write a program which will prevent students from saving in that folder, prompting them with a custom message each time. Or (2) I could write a program which periodically empties the folder.

I tried my hand at number 2, but the only method I could get working was to delete the My Documents folder and re-create it. This being scheduled to run with scheduled tasks is not a very foolproof way of doing things, and as a restricted user
Any help on systematically deleting all files and folders within the my documents folder using any method would be appreciated.

Daniel Aldous-Critchley
PS: Remember tho, that as restricted user shelling out the commands gonna work too well
0
Comment
Question by:danielnet
  • 4
  • 4
  • 2
  • +2
14 Comments
 
LVL 1

Author Comment

by:danielnet
ID: 9656521
^^
That last line was meant to read: "ain't gonna work too well"

Dan
0
 

Expert Comment

by:dustybryn
ID: 9656620
This code will recursively step down from the folder you specify and systematically delete every file and folder undeneath it. You will need to set a reference to Microsoft scripting run time and have a form with a command button called command1.

Option Explicit
Dim objFSO As FileSystemObject
Dim objFSOFolder As Folder
Dim objFSOSubFolder As Folder
Dim objFSOFile As File


Private Function RecursiveDelete(ByVal FSOTopFold As Folder)
'Search the folders and delete everything

    For Each objFSOSubFolder In FSOTopFold.SubFolders
        Call RecursiveDelete(objFSOSubFolder)
    Next
    For Each objFSOFile In FSOTopFold.Files
       objFSOFile.Delete (True)
    Next
    FSOTopFold.Delete

End Function

Private Sub Command1_Click()
    'Set objects
    Set objFSO = New FileSystemObject
    Set objFSOFolder = objFSO.GetFolder("C:\brynstest")
   
    'Run the delete
    Call RecursiveDelete(objFSOFolder)
   
    'Clean up
    Set objFSO = Nothing
    Set objFSOFolder = Nothing
    Set objFSOSubFolder = Nothing
    Set objFSOFile = Nothing
   
   
End Sub

Hope this helps,

Dust.
0
 

Expert Comment

by:dustybryn
ID: 9656626
Forgot to say. You'll need to replace the folder name "Brynstest" with whatever folder you want to start the delete from
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9656915
If you want to stop them saving into that folder at all, why not just remove their user groups rigths on the folder? The nthey won't be able to save in there.
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9659126
Have you tried using SHFileOp API with the Silent and No Confirmation Flags set?


Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Const FO_COPY = &H2
Const FO_DELETE = &H3
Const FO_MOVE = &H1
Const FO_RENAME = &H4
Const FOF_ALLOWUNDO = &H40
Const FOF_SILENT = &H4
Const FOF_NOCONFIRMATION = &H10
Const FOF_RENAMEONCOLLISION = &H8
Const FOF_NOCONFIRMMKDIR = &H200
Const FOF_FILESONLY = &H80

Private 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

Private Sub Command1_Click()
Dim result As Long, fileop As SHFILEOPSTRUCT
With fileop
     .hwnd = Me.hwnd
     .wFunc = FO_DELETE
     .pFrom = "C:\My Documents\*.*"
     .fFlags = FOF_SILENT Or FOF_NOCONFIRMATION
End With
result = SHFileOperation(fileop)
If result <> 0 Then
     ' Operation failed
     MsgBox Err.LastDllError
End If
End Sub
0
 
LVL 1

Author Comment

by:danielnet
ID: 9670035
dustybryn thank-you your method seemed to work for me, if i was to use it on a folder like cookies though, where index.dat cannot be deleted. will it just step over it? I'd try myself it but i'm not at work at the moment

Thanx
Dan
0
 

Expert Comment

by:dustybryn
ID: 9670105
just add error handling and it will ignore any it cannot delete (if they are open or read only for instance). Obviously if a file cannot be deleted neither can it's folder so the folder would remain but with only the file that caused the problem. The RecursiveDelete function should be ammended so that it reads as follows

Private Function RecursiveDelete(ByVal FSOTopFold As Folder)
'Search the folders and delete everything
    On Error GoTo errH:
    For Each objFSOSubFolder In FSOTopFold.SubFolders
        Call RecursiveDelete(objFSOSubFolder)
    Next
    For Each objFSOFile In FSOTopFold.Files
       objFSOFile.Delete (True)
    Next
    FSOTopFold.Delete
    Exit Function
errH:
    If Err.Number = 70 Then
        Err.Clear
        Resume Next
    End If
   
End Function

Hope this helps,

Dust.

0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9670132
You restrict users rights but you allow scripting?
0
 
LVL 1

Author Comment

by:danielnet
ID: 9677490
Fantastic! just one final issue i have, it deletes the original folder too.

So for instance if my folder to do the recursive delete on is C:\temp and there are loadsa files and folders underneath it, it will delete everything i need. But it will delete the temp folder too. the only time it won't do this is if there is a file that won't delete, due to permissions, or file-open or whatever.

i'm gonna increase the points for this because u've helped me so much.

Thanx
Dan
0
 

Accepted Solution

by:
dustybryn earned 150 total points
ID: 9677692
Dan,
      Here is the full code for what you want. As it is it will now leave the original top folder intact but will delete all files and folders beneath it. I have also included a setting which enables you to leave all the files in the top folder intact if that is what you decide. Look at the first call to RecursiveDelete from the Command1_Click event. The first 'true' parameter tells the function to preserve the top folder. The next 'false' setting tells the function not to preserve the files within the top folder.

Option Explicit
Dim objFSO As FileSystemObject
Dim objFSOFolder As Folder
Dim objFSOSubFolder As Folder
Dim objFSOFile As File

Private Function RecursiveDelete(ByVal FSOTopFold As Folder, Optional ByVal KeepOriginalFolder As Boolean, Optional ByVal KeepTopFolderFiles As Boolean) As Boolean
'Search the folders and delete everything
    On Error GoTo errH:
    For Each objFSOSubFolder In FSOTopFold.SubFolders
        Call RecursiveDelete(objFSOSubFolder)
    Next
   
    If Not KeepTopFolderFiles Then
        For Each objFSOFile In FSOTopFold.Files
           objFSOFile.Delete (True)
        Next
    End If
   
    If Not KeepOriginalFolder Then FSOTopFold.Delete
    RecursiveDelete = True
    Exit Function
errH:
    If Err.Number = 70 Then
        Err.Clear
        Resume Next
    End If
    RecursiveDelete = False
End Function

Private Sub Command1_Click()
Dim blSuccess As Boolean
    Screen.MousePointer = vbHourglass
    'Set objects
    Set objFSO = New FileSystemObject
    Set objFSOFolder = objFSO.GetFolder("C:\bryn")
   
    'Run the delete
    blSuccess = RecursiveDelete(objFSOFolder, True, False) 'notice the true setting
   
    'Clean up
    Set objFSO = Nothing
    Set objFSOFolder = Nothing
    Set objFSOSubFolder = Nothing
    Set objFSOFile = Nothing
    MsgBox "Done."
    Screen.MousePointer = vbDefault
End Sub

Hope this helps,
                      Dust
 
0
 
LVL 1

Author Comment

by:danielnet
ID: 9677878
Thank-you very much, works a treat!
points to you!

Dan
0
 

Expert Comment

by:makiwa1
ID: 14018259
I know this question is closed and I am willing to open another if required.

I have tried this script as is but I get an "Unexpected end of statement in Line2 " error when I try to run it.

My scripting skills are non existant so I need all the help I can get.
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 14031928
Why don't you try

DEL <your path>\*.* /S

in a batch file.
0
 

Expert Comment

by:makiwa1
ID: 14034243
DEL /S does not delete the directories.

But have found deltree and it does the job.

Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question