Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automate weekly folder deletions in Server 2003

Status
Not open for further replies.

wlfpackr

IS-IT--Management
May 3, 2003
161
US
Recently moved from Netware to Server 2003 so I'm a bit new to the Server 2003 environment.

Anyway, I have a "Scratch" folder that everyone in the company has access to for a temporary means to share data. In the Netware environment I had it to where the contents of the folder would get deleted out every Sunday night and I need to do the same on Server 2003.

I thought I would just write a batch script to perform the deletion and setup a Scheduled Task to run the batch script at a specified time. However, I've noticed that I can't find a proper switch to delete sub-directories with the DEL command and DELTREE is no longer an option.

My batch scripting knowledge is fairly basic and usually I'm able to just find what I need on the net when I need it, however, I cannot find a way around this one.

Suggestions????




=================
There are 10 kinds of people in this world, those that understand binary and those that do not.
 
Forget batch and move on to VBScript.

Code:
Dim objFSO, ofile, oFolder, oSubFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = objFSO.GetFolder("C:\Kill")

For Each oFile In oFolder.Files
	oFile.Delete
Next
For Each oSubFolder In oFolder.SubFolders
	oSubFolder.Delete
Next

Just change the path in the script from C:\Kill to whatever directory you want and schedule this to run when you want the files and folders deleted.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
It doesn't work. I setup a test directory = C:\test.

When I run the Script I get a "Permission Denied" error in line 10, char 5. It deleted the data files in the root of "test", but did not delete any sub-directories or files in those subdirectories.


=================
There are 10 kinds of people in this world, those that understand binary and those that do not.
 
If gettign a permission denied you can use the force method to allow the deletion of read-only files.

oSubFolder.Delete True

Note that if anyone has the files open on their desktop you will however be prevented from deleting the folder.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Okay, this program worked on one directory, but not the other. It appears as though, it deleted a lot of files, but not all of them. I'm guessing someone had one open overnight and it caused the program to bomb out.

Is there a way to force deletion of a file even if someone has it open?

=================
There are 10 kinds of people in this world, those that understand binary and those that do not.
 
There will be a file/folder lock if a user has a directory or file open. There are some third party utilities (I think one is called "OwnIt") that will force letting you take ownership of a file and close any open file handles. You would need to research those alternatives.

As an option you could also force all users sessiosn to log off between the hours of 11 PM- Midnight on Sundays, that way all sessions would be closed. You could then schedule the script to run in that time window.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top