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
310 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
Comment Utility
^^
That last line was meant to read: "ain't gonna work too well"

Dan
0
 

Expert Comment

by:dustybryn
Comment Utility
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
Comment Utility
Forgot to say. You'll need to replace the folder name "Brynstest" with whatever folder you want to start the delete from
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
You restrict users rights but you allow scripting?
0
 
LVL 1

Author Comment

by:danielnet
Comment Utility
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
Comment Utility
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
Comment Utility
Thank-you very much, works a treat!
points to you!

Dan
0
 

Expert Comment

by:makiwa1
Comment Utility
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
Comment Utility
Why don't you try

DEL <your path>\*.* /S

in a batch file.
0
 

Expert Comment

by:makiwa1
Comment Utility
DEL /S does not delete the directories.

But have found deltree and it does the job.

Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now