[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to keep a fixed maximum number of iles in a directory

Posted on 2011-10-11
6
Medium Priority
?
340 Views
Last Modified: 2012-05-12
Hi,

Is there a way to make a script for Windows that will keep only a fixed number of files in a directory, no matter what is the files time and date stamps ? like 10 copies

0
Comment
Question by:fox54
  • 3
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36948544
Do you mean not even allow the files to be created, if so that's not possible.

Or do you mean a script that can delete files if more than 10 exist?  In that case, what criteria would be used to select the 10 to keep?

~bp
0
 

Author Comment

by:fox54
ID: 36948629
Just a scrit to delete if more files exists but it has to delete the oldest

So the script need to keep 10 files maximum in the directory, deleting the oldest if there's more than 10.  But if there's less than 10 files in the directory, keep them there no matter what the time and date stamps.
 
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 200 total points
ID: 36949010
Here's a simple BAT script that should to it, just change the name of the folder in the PUSHD command.

@echo off
pushd "c:\temp"
for /F "skip=10 tokens=*" %%A in ('dir /b /o-d /a-d') do del "%%~A"
popd

Open in new window

~bp
0
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 

Author Closing Comment

by:fox54
ID: 36949134
Thanks

Never heard of those command pushd and popd before
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36949605
Looks like I was too late on this one.  Bill will probably explain in a minute but:

Pushd changes to the directory you say, pretty well the same as cd /d c:\temp but will also map a drive letter if you say chose pushd \\server\share

Popd then undoes your pushd, removes any temporary drive letter and puts you back where you were before.

Steve
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36949872
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Screencast - Getting to Know the Pipeline

873 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