简体   繁体   English

Powershell脚本删除未使用的文件

[英]Powershell script to remove files that are not in use

I'm trying to write a script that removes all files from a given directory, however I don't want to remove files that are currently in use (being viewed, edited, etc.). 我正在尝试编写一个脚本来删除给定目录中的所有文件,但是我不想删除当前正在使用的文件(正在查看,编辑等)。 What I'm finding is that for some file types (.docx, .xlsx, etc.), this works just fine and the .ps1 script fails as expected and moves on. 我发现的是,对于某些文件类型(.docx,.xlsx等),这工作得很好,.ps1脚本按预期失败并继续运行。 However, some files (.bmp, .txt) can be open and get deleted as well. 但是,某些文件(.bmp,.txt)也可以打开并被删除。 It looks like certain files are not locked when in-use. 看起来某些文件在使用中没有锁定。 I guess my question is a few smaller questions: 我想我的问题是几个较小的问题:

  1. Is there a way to tell if files are in-use (other than seeing if it's locked)? 有没有办法判断文件是否正在使用(除了查看文件是否已锁定)?
  2. Is there any definitive way to tell which file types are locked when in-use? 有什么确定的方法可以告诉使用中哪些文件类型被锁定?
  3. Is there a better cmdlet than Remove-Item to use for what I am trying to achieve? 是否有比Remove-Item更好的cmdlet可用于我要实现的目标?

Thanks in advance! 提前致谢!

Some applications like Word locks the file wile reading them to avoid it being modified while it's open (usualy in case you want to modify it in ex. Word ). 像一些应用程序Word锁定该文件诡计阅读它们,以避免它被修改,而它的开放(usualy如果你想修改它的前, Word )。 Other applications, like notepad , doesn't. 其他应用程序(例如notepad )则没有。

This is not specific to a filetype (which is just a GUI attribute that has nothing to do with the data inside). 这不是特定于文件类型的(文件类型只是一个GUI属性,与内部数据无关)。 It's the application that decides if it wants to lock a file or not. 决定是否要锁定文件的是应用程序。 Ex: 例如:

  • Open a docx file in Word: Access denied (file in use) 在Word中打开docx文件:访问被拒绝(文件正在使用中)
  • Open a docx file in Wordpad: Success (no lock) 在写字板中打开docx文件:成功(无锁)

AFAIK it's impossible to detect 100% files being in use by ex. AFAIK无法检测到ex正在使用的100%文件。 notepad which doesn't lock files. 记事本不会锁定文件。

There are many ways to delete files, but Remove-Item is as good as any of them. 删除文件的方法有很多,但是Remove-Item与其中的任何一种一样好。

Adding to Frode's point, nothing wrong with remove-item . 弗罗德的观点是, remove-item没错。 Normal directory doesn't offer you much. 普通目录不能为您提供太多服务。 Files hosted via windows file server provides session information might help you identify if a file is being used. 通过Windows文件服务器托管的文件可提供会话信息,这些信息可能有助于您确定是否正在使用文件。 If possible, you may implement a smart logic that excludes the files are in use. 如果可能,您可以实现一个智能逻辑,以排除正在使用的文件。

In general, I'd recommend a work around that your powershell script check file's LastAccessTime, say, delete everything that hasn't been accessed for 24 hours 通常,我建议您解决一下Powershell脚本检查文件的LastAccessTime问题,例如,删除24小时内未访问的所有内容

Get-ChildItem -path c:\ps | Where-Object {$_.LastAccessTime -lt (get-date).addDays(-1)}

The extreme way of protecting files, is using SCM and don't delete anything checked out. 保护文件的一种极端方法是使用SCM,并且不要删除任何已检出的内容。 However this is not a friendly solution to average users. 但是,这对于普通用户而言不是一个友好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM