Link to home
Start Free TrialLog in
Avatar of parkridgelibrary
parkridgelibraryFlag for United States of America

asked on

Automatically Delete Files In A Directory

I have a shared folder on one of our servers (Windows 2003 Server Enterprise) for our patrons to temporarily store files in case they don't have their flash drive with them.  In the past with our old server (ie, before I was here) the shared folder was emptied every night around 11pm by some automatic process.  I cannot find any info on how it was done before I got here so I am throwing it out to the Experts.

The only issue is that users can create files or folders.  We want to clear everything out, but there is always one folder we want to show up (let's call it "files"for this senario).  I don't mind if the process is to clear out everything in the folder and recreate the "files" folder (there shouldn't be anything inside of it anyways).  But the process does need to run independent of user interaction though.

So I am open to a script and scheduled task, or a software application, whatever will be simple and easy to implement.  Thanks!!
ASKER CERTIFIED SOLUTION
Avatar of MSE-dwells
MSE-dwells
Flag of Yemen image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of parkridgelibrary

ASKER

Absolutely perfect!! THANK YOU SO MUCH!!!
Hey MSE-dwells,

The code you gave me above seems to be working except only parially.  Here are the two schedule tasks I made:

- C:\WINDOWS\system32\cmd.exe /c rd /s /q K:\Patron_Temp && md K:\Patron_Temp\"IMPORTANT - All files saved here are viewable by everyone and are deleted daily !!"

- C:\WINDOWS\system32\cmd.exe /c rd /s /q k:\Patron_CS_Temp && md k:\Patron_CS_Temp\"IMPORTANT - All files saved here are viewable by everyone and are deleted daily !!"

The second one works perfectly, but the first one seems to only half work.  It will clear out all of the files, but it does not create the new folder.  When I looked at the Scheduled Tasks section, it showed a Last Result of 0x20 for the first one.  When I tried to look up what that means, it look like it means either "The system cannot find the device specified" or "User limit exceeded".

Neither of these makes much sense to me.  Can you tell me what I am missing or if I didn't get the code right?  My other thought was to make a *.bat file with these commands in them so that there is only one scheduled task.  However when I tried that and ran it in the Command Window to see the result it said "The process cannot access the file because it is being used by another process."  Any ideas?
My first suggestion would  be to place the quotes differently, see below -

C:\WINDOWS\system32\cmd.exe /c rd /s /q K:\Patron_Temp && md "K:\Patron_Temp\IMPORTANT - All files saved here are viewable by everyone and are deleted daily !!"

C:\WINDOWS\system32\cmd.exe /c rd /s /q k:\Patron_CS_Temp && "md k:\Patron_CS_Temp\IMPORTANT - All files saved here are viewable by everyone and are deleted daily !!"

... as for the error, let's first see what we get when using this syntax.
MSE-dwells,

Changing the quotes definitely helped.  The only problem I am running into now, is that when it runs it seems to reset the sharing and security permissions.  Or something else is, but it never happened before I tried the script.  Any thoughts?
What directory(ies) is/are shared?  Which permissions are disappearing -- NTFS or share?
The k:\Patron_CS_Temp and k:\Patron_Temp folders are shared.  When I check it this morning though, only the k:\Patron_Temp had missing permissions.  The other one was fine.
Which permissions?  NTFS?  Are they still shared?
The permissions for like READ/WRITE/ETC.  In one case the accounts that were added in to have access were removed completely.  But yes, they are still shared as not all of the accounts were removed.
Unfortunately, you're not answering my question ... I could take a guess but I'd prefer to be certain.  Which tab are you using in Windows Explorer to determine that the permissions have either been removed or are still present?
Sorry, I don't think I fully understood what you were asking (and I am not feeling well, so my brain isn't fully functional at the moment).  I looking at both the sharing and the security tabs when I right-click the folder and choose properties.
Feeling sick has the same effect on me :0(

The issue with sharing shouldn't occur since the folder is recreated almost immediately because the sharing configuration and its permissions aren't part of the filesystem ... can I presume that the shares do indeed persist each night along with their permissions?

Now, the NTFS pemissions are an entirely different story.  I confess I hadn't considered that each folder would be using a customized ACL -- not that it's wrong, it's just less common than relying upon inheritance.  We may need to alter the solution slightly to account for this -

C:\WINDOWS\system32\cmd.exe /c pushd K:\Patron_Temp && rd /s /q . & md "IMPORTANT - All files saved here are viewable by everyone and are deleted daily !!" & popd

C:\WINDOWS\system32\cmd.exe /c pushd K:\Patron_CS_Temp && rd /s /q . & md "IMPORTANT - All files saved here are viewable by everyone and are deleted daily !!" & popd

... as always, these commands are MASSIVELY DESTRUCTIVE, please test them first to your own satisfaction (I did the same but don't take my word for it ;0)
... minor verbiage correction -

"The issue with sharing shouldn't occur since the folder is recreated almost immediately *and* because the sharing configuration and its permissions aren't part of the filesystem ..."
Yes the shares do show up continually and the inherited permissions show up just fine.  The other permissions I am adding are to allow an additional group to have access to the folder who does not have access to anything above it.  I personally would never setup a file structure in this manner, but it was in place before I was working here and I am not sure if it is worth trying to move it at this point.  I will try your new suggestions and let you know how it goes.  Thanks!!