Solved

Automatically Delete Files In A Directory

Posted on 2007-11-19
14
825 Views
Last Modified: 2010-04-21
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!!
0
Comment
Question by:parkridgelibrary
  • 7
  • 7
14 Comments
 
LVL 9

Accepted Solution

by:
MSE-dwells earned 500 total points
ID: 20314531
I'd suggest you schedule a task on the server holding the share that runs at the desired time, the task syntax would look something like this -

cmd /c rd /s /q <your path here> && md <your path here>\Files

... you may also need to schedule the task to run as a particular user depending upon the NTFS permissions configured on the directory.

Note - that is a very destructive command so please be careful!
0
 

Author Closing Comment

by:parkridgelibrary
ID: 31409997
Absolutely perfect!! THANK YOU SO MUCH!!!
0
 

Author Comment

by:parkridgelibrary
ID: 20350124
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?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20350175
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.
0
 

Author Comment

by:parkridgelibrary
ID: 20381293
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?
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20381336
What directory(ies) is/are shared?  Which permissions are disappearing -- NTFS or share?
0
 

Author Comment

by:parkridgelibrary
ID: 20381625
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.
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20381733
Which permissions?  NTFS?  Are they still shared?
0
 

Author Comment

by:parkridgelibrary
ID: 20381755
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.
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20381785
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?
0
 

Author Comment

by:parkridgelibrary
ID: 20382012
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.
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20382098
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)
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20382115
... 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 ..."
0
 

Author Comment

by:parkridgelibrary
ID: 20382157
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!!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This may not be a text book method to resolve VSS backup issues but it seemed to have worked on few of the Windows 2003 servers we had issues while performing a Volume Shadow Copy backup. If you have issues while performing a shadow copy backup usin…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

861 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