PowerShell脚本来删除旧文件 [英] Powershell script to delete old files

查看:295
本文介绍了PowerShell脚本来删除旧文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本将删除一个名为目录中超过14天,并写入到文件的 .TXT 的路径和文件删除(发现这个脚本上另一个forum..credit到吉文):

  DIR C:\tmp -recurse |其中{$ _ PsIsContainer -and $ _ lastWriteTime -lt(GET-日).AddDays(-14)!} |选择LastWriteTime,@ {N =路径; E = {转换路径$ _ PSPath}} |三通C:\oldFiles.txt |删除-项目-force -whatif 



我有3个问题:




  1. 什么是 -lt ,什么是 -le 什么为 -gt ?当我会用每一个

  2. 上面的脚本只是删除文件...我怎样才能删除文件夹呢?

  3. 上面的脚本是基于关闭 LastWriteTime 的。关于 CreatedDate 上次访问什么时间?


解决方案

好了,我们开始吧:




  1. -lt,-le和-gt是比较运算符。 LT意味着比,乐指小于或等于,和GT表示大于。


  2. 删除文件夹可以获得危险的,如果你没有什么获取控制里面。你可能有问题,请注意数据的丢失。您可以通过使用相同的删除 - 项目cmdlet的,只是用它玩的选项删除文件夹。检查这篇文章,但对如何实现它的伟大说明:
    HTTP: //searchwindowsserver.techtarget.com/generic/0,295582,sid68_gci1275887,00.html


  3. 通常,对于文件已经坐在那里一会儿,LastWriteTime和CreatedDate和的LastAccessTime将是相同的。在一个只读文件,就像一个DLL,的LastAccessTime可能比其他两个新。在读/写文件(如Outlook的PST文件)WriteTime和AccessTime可能是相同的。基本上,这完全取决于你。他们一样LastWriteTime不相同的方式工作。考虑一下你要删除的文件的性质,并继续前进!



The following script will delete files in a named directory that are older than 14 days and write to a .txt with the path and the files deleted (found this script on another forum..credit to shay):

dir c:\tmp -recurse | where {!$_.PsIsContainer -AND $_.lastWriteTime -lt (Get-Date).AddDays(-14) } | select LastWriteTime,@{n="Path";e={convert-path $_.PSPath}} | tee c:\oldFiles.txt | Remove-Item -force -whatif

I have 3 questions:

  1. What is -lt and what is -le and what is -gt? When would I use each one
  2. The script above only deletes file...how can I delete folders as well?
  3. The script above is based off of LastWriteTime .. what about CreatedDate or LastAccessed time?

解决方案

Ok, here we go:

  1. -lt, -le and -gt are comparison operators. lt means less than, le means less or equal than, and gt means greater than.

  2. Removing folders can get dangerous if you do not have control over what gets inside it. You might have problems and please be aware of data loss. You can delete folders by using the same Remove-Item cmdlet, just by playing with its options. Check this article, it has great instructions on how to achieve it: http://searchwindowsserver.techtarget.com/generic/0,295582,sid68_gci1275887,00.html

  3. Usually, for files that have been sitting there for a while, LastWriteTime and CreatedDate and LastAccessTime will be the same. In a read-only file, like a DLL, LastAccessTime might be newer than the other two. In a read/write file (like outlook's pst file) WriteTime and AccessTime might be the same. Basically, that's totally up to you. They work as the same way as LastWriteTime does. Consider the nature of the files you want to delete, and go ahead!

这篇关于PowerShell脚本来删除旧文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆