注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

jasonyang9的博客

随便写写

 
 
 

日志

 
 

(转)Delete files more than X Days old  

2016-04-07 11:52:24|  分类: software |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
(转)Delete files more than X Days old - jasonyang9 - jasonyang9的博客

On one of our servers we have a batch processing program. It is old and clunky, but it gets the job done.

An irritating bug it has is that it leaves temp files around in the c:\windows\temp folder.

One day we noticed the server was getting slow – finally realized that the temp folder had over 100,000 files!

We don’t have the code for this program, so we can’t fix the bug. An easy fix would be to setup a scheduled task to trash that folder every night.

Oscar would like to have all those files every night, but the problem with this solution is it can cause problems with apps that actually are *using* those temp files.

What we needed was a way to delete files that were over 1 day old. This was enough to make sure the temp file was no longer used by any of our processing apps.

After searching around, I found a command line program that Microsoft included with some versions of windows. It is called “forfiles”

ForFiles will process a command against files and folders that match your criteria. It allows you to set wildcards, pick only folders, or only files…and more importantly it allows you to specify something like “I only want files that are more than 1 day old”

First, let me show you the possible command line arguments for it:

?/P Path Name - Indicates the path to start searching. The default folder is the current working directory

?/M Search Mask - Searches files according to a searchmask. The default searchmask is '*' .

?/S Sub Directories Instructs forfiles to recurse into subdirectories. Like "DIR /S".

?/C Command Indicates the command to execute for each file. Command strings should be wrapped in double quotes.

The following variables can be used in the command string:

@file - returns the name of the file.

@fname - returns the file name without extension.

@ext - returns only the extension of the file.

@path - returns the full path of the file.

@relpath - returns the relative path of the file.

@isdir - returns "TRUE" if a file type is a directory, and "FALSE" for files.

@fsize - returns the size of the file in bytes.

@fdate - returns the last modified date of the file.

@ftime - returns the last modified time of the file.

To include special characters in the command line, use the hexadecimal code for the character in 0xHH format (ex. 0x09 for tab). Internal CMD.exe commands should be preceded with "cmd /c".

?/D date Selects files with a last modified date greater than or equal to (+), or less than or equal to (-), the specified date using the "yyyy/MM/dd" format; or selects files with a last modified date greater than or equal to (+) the current date plus "dd" days, or less than or equal to (-) the current date minus "dd" days. valid "dd" number of days can be any number in the range of 0 - 32768."+" is taken as default sign if not specified.

Cool. It has everything we need.

Lets start out with the path. We want it to look in c:\windows\temp

forfiles -p "c:\windows\temp"

I want to include subfolders:

forfiles -p "c:\windows\temp" -s

Using the date option, I tell it anything over 1 day old:

forfiles -p "c:\windows\temp" -s -d -1

I only want to delete the files that end with .tmp:

forfiles -p "c:\windows\temp" -s -d -1 -m *.tmp

And finally, the command I want to execute against the files it finds is del:

forfiles -p "c:\windows\temp" -s -d -1 -m *.tmp -c "cmd /c del /f /q @path"

Note the use of the @path variable – it is one of the many variables forfiles recognizes.

So there it is – a one line command that will delete files from our temp folder over 1 day old:

forfiles -p "c:\windows\temp" -s -d -1 -m *.tmp -c "cmd /c del /f /q @path"

If your copy of windows does not have forfiles, you can download it from the Microsoft FTP Server here:

ftp://ftp.microsoft.com/ResKit/y2kfix/x86/

I am really impressed with this little utility, and the possibilities are endless. I highly suggest keeping a copy on your flash drive.

Delete files more than X Days old

  评论这张
 
阅读(24)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017