|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
VBScript - Delete files/folders automatically
Hi,
At my company we use a shared dump folder which should be emptied every Friday afternoon. Does anyone know a script witch takes care of this? It should delete everything in the folder (files/folders) except for the folder itself because this is a share. Thanks in advance Ringo |
|
#2
|
||||
|
||||
|
Hi Ringo
this should do the trick Code:
<%
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("c:\test\")
for each x in fo.files
fs.DeleteFile("c:\test\" & x.Name)
next
set fo=nothing
set fs=nothing
%>
put it in a vbs file, and add the file to the windows scheduler so that it will run automatically each Friday. hope this helps
__________________
Look! Its a ShemZilla ![]() ![]()
|
|
#3
|
||||
|
||||
|
after reading your post again, I see
you want to delete the folders as well, I haven't test it, but this might work: Code:
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("c:\test\")
for each x in fo.files
fs.DeleteFile("c:\test\" & x.Name)
next
for each x in fo.subfolders
fs.DeleteFolder("c:\test\" & x.Name)
next
set fo=nothing
set fs=nothing
Last edited by nofriends : December 6th, 2006 at 02:00 AM. Reason: changed code |
|
#4
|
|||
|
|||
|
Thanks for the quick reply.
When I run the script it gives me the error: "Object required:'Server' Is there something in the script which i have to customize ? |
|
#5
|
||||
|
||||
|
just remove the Server.
it should just be CreateObject("Scripting.FileSystemObject") the above is for ASP, but that change should make it work ![]() |
|
#6
|
|||
|
|||
|
This works great !!
Thanks for your help ![]() |
|
#7
|
||||
|
||||
|
no worries, glad it worked
![]() |
|
#8
|
|||
|
|||
|
Quote:
I like this script also, but need it to be able to selectively delete files and folders older than a set number of days. Can you help me with that? Thanks. |
|
#9
|
||||
|
|
||||
|
Quote:
__________________
Click the image if at any point you don't like my decision.Scripting problems? Windows questions? Ask the Windows Guru! |
|
#10
|
|||
|
|||
|
Files read only
Quote:
I have worked for a couple of months with this script and i have noticed that when a file is "read only" the script doesn't delete the file and stops. Is it possible that the script also deletes "read only" files and is there a way to be noticed that the script has stopped working (by a log file for example). Regards Ringo |
|
#11
|
||||
|
|
||||
|
Quote:
I've also optimized the code for you. The last example was a bit redundant. Code:
Dim fs, fo, files, folders, file, folder
Set fs = CreateObject("scripting.filesystemobject")
Set fo = fs.GetFolder("C:\Test")
Set files = fo.Files
Set folders = fo.SubFolders
For Each file In files
file.Attributes = 0
file.Delete vbTrue
Next
For Each folder In folders
folder.Delete vbTrue
Next
|
|
#12
|
|||
|
|||
|
Thanks "Nilpo" this works great !
|
|
#13
|
||||
|
|