• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 835
  • Last Modified:

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!!
0
parkridgelibrary
Asked:
parkridgelibrary
  • 7
  • 7
1 Solution
 
MSE-dwellsCommented:
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
 
parkridgelibraryAuthor Commented:
Absolutely perfect!! THANK YOU SO MUCH!!!
0
 
parkridgelibraryAuthor Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MSE-dwellsCommented:
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
 
parkridgelibraryAuthor Commented:
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
 
MSE-dwellsCommented:
What directory(ies) is/are shared?  Which permissions are disappearing -- NTFS or share?
0
 
parkridgelibraryAuthor Commented:
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
 
MSE-dwellsCommented:
Which permissions?  NTFS?  Are they still shared?
0
 
parkridgelibraryAuthor Commented:
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
 
MSE-dwellsCommented:
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
 
parkridgelibraryAuthor Commented:
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
 
MSE-dwellsCommented:
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
 
MSE-dwellsCommented:
... 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
 
parkridgelibraryAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now