danielnet
asked on
A program to delete all files and folders within a folder, on a win2000 machine with restricted user rights.
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
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
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(objFSOSubF older)
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:\bryns test")
'Run the delete
Call RecursiveDelete(objFSOFold er)
'Clean up
Set objFSO = Nothing
Set objFSOFolder = Nothing
Set objFSOSubFolder = Nothing
Set objFSOFile = Nothing
End Sub
Hope this helps,
Dust.
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(objFSOSubF
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:\bryns
'Run the delete
Call RecursiveDelete(objFSOFold
'Clean up
Set objFSO = Nothing
Set objFSOFolder = Nothing
Set objFSOSubFolder = Nothing
Set objFSOFile = Nothing
End Sub
Hope this helps,
Dust.
Forgot to say. You'll need to replace the folder name "Brynstest" with whatever folder you want to start the delete from
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.
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
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
ASKER
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
Thanx
Dan
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(objFSOSubF older)
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.
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(objFSOSubF
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.
You restrict users rights but you allow scripting?
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank-you very much, works a treat!
points to you!
Dan
points to you!
Dan
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.
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.
Why don't you try
DEL <your path>\*.* /S
in a batch file.
DEL <your path>\*.* /S
in a batch file.
DEL /S does not delete the directories.
But have found deltree and it does the job.
Thanks.
But have found deltree and it does the job.
Thanks.
ASKER
That last line was meant to read: "ain't gonna work too well"
Dan