Solved

Automatically Delete Files In A Directory

Posted on 2007-11-19
14
821 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

So you have two Windows Servers and you have a directory/folder/files on one that you'd like to mirror to the other?  You don't really want to deal with DFS or a 3rd party solution like Doubletake. You can use Robocopy from the Windows Server 200…
Numerous times I have been asked this questions that what is it that makes my machine log on so slow, there have been cases where computers took 23 minute exactly after taking password and getting to the desktop. Interesting thing was the fact th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now