早些时候,我写了一个名为DropIt的程序 ,当文件夹中出现新文件时,它会自动为您移动或复制文件。例如,如果您的本地硬盘空间有限,并且想要将所有下载内容移至外部存储设备(storage device),这可能很有用。
如果你想自动删除文件,有两种方法可以在Windows中进行。第一种方法涉及下载名为AutoDelete的(AutoDelete)免费软件应用程序(freeware app),该应用程序可让您配置删除特定文件夹中文件的计划。我已经编写了两份关于使用该程序的详细指南(此处和此处),因此,如果您更喜欢免费软件程序来完成工作,请查看这些指南。
删除文件的第二种方法是创建一个批处理文件(batch file),然后安排该批处理文件(batch file)运行。您无需安装任何第三方软件即可完成所有这些操作。在本文中,我将引导您完成创建批处理文件(batch file)的步骤,然后使用任务计划程序(Task Scheduler)让脚本重复运行。
第 1 步 - 创建批处理文件
如果创建批处理文件(batch file)听起来有点吓人或太技术性,请不要担心,因为您不必知道其中的任何含义。我将解释您需要复制和粘贴的内容、可以更改的位置和选项。首先(First),打开记事本并复制(Notepad and copy)并粘贴以下文本行:
forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c del @path"
上面的行可能没有意义,这很好,我将在下面解释。基本上(Basically),它告诉 Windows 删除 C:Test文件夹和子文件夹(folder and sub-folders)中超过 5 天的所有文件。这是您的记事本文件(Notepad file)的外观。
在我们了解有关该命令的更多详细信息之前,让我们保存文件并对其进行测试运行(test run)。首先,在您的计算机(First)C 驱动器(C drive)的根目录下创建一个名为Test的文件夹。接下来,单击文件(File)-保存(Save)并将文件另存为批处理文件(batch file)。为此,请输入一个名称,后跟 .bat,然后将Save as type下拉菜单更改为All Files。
请注意,您可以将文件保存到您喜欢的硬盘驱动器上的任何位置,这并不重要。现在在Test(Test)文件夹中创建一些虚拟文件,然后双击Delete.bat文件运行它。有什么要删的吗?可能不是!
什么都没有被删除的原因是因为该命令有/D -5,这意味着文件是 5 天或更早。为了删除任何文件,无论(file regardless)它是何时创建的,您可以将-5更改为-0或者您可以完全删除/D -5部分。现在,如果您运行它,所有文件都将被删除。
要自定义命令,您可以做的第一件事是将目录更改为 C:Test 以外的目录。这就像从Windows 资源管理器(Windows Explorer)中复制所需目录的路径并将其粘贴到记事本(Notepad)中的命令中一样简单。
接下来是您在目录路径后看到的-s参数。( -s)这表明该命令还应查看所有子文件夹。如果您不想从子文件夹中删除文件,请继续删除 -s 参数。
接下来是-m后跟 *.*,这意味着该命令应该评估各种文件。如果您只想删除文件夹中的特定文件类型(file type),例如PDF文件或JPG图像,只需将 *.*更改为*.pdf或*.jpeg,它只会删除这些文件。
我们已经讨论过的/D -X部分是关于文件必须有多旧才能有资格删除。您可以将其保留为大于 1 的值,将其设置为 0,或完全删除它。这就是我们需要了解的有关命令的所有信息。
运行此命令有几点需要注意。首先,当文件被删除时,它们不会进入回收站(Recycle Bin),而是被永久删除,所以使用时要小心。其次,该命令只删除文件,而不是文件夹。
由于这是一个批处理文件(batch file),您还可以在同一个文件中添加此命令的多个版本。例如,在这里我正在创建一个批处理文件(batch file),它将删除所有超过 180 天的DOCX文件、所有超过 60 天的PDF文件和所有(PDF)TXT文件,无论这些文件有多旧。
第 2 步 - 计划批处理文件
现在您已经创建并保存了批处理文件(batch file),让我们继续安排它在重复发生的基础上运行。为此,我们必须打开Task Scheduler。
幸运的是,我已经写了一篇关于如何安排批处理文件(batch file)的文章,所以打开那个页面开始吧。向下滚动(Scroll)到PC 启动(Schedule Batch File on PC Startup)部分的计划批处理文件,然后继续操作。
您唯一需要更改的是Trigger。您可以选择每天(Daily)、每周(Weekly)、每月(Monthly)、计算机启动时(When the computer starts)、我登录时(When I log on)或记录特定事件时(When a specific event is logged)。
当您选择每周(Weekly)或每月(Monthly)之类的内容并单击下一步(Next)时,您将看到一个新屏幕,您可以在其中配置您希望脚本运行的确切时间和日期。(time and days)
希望对于大多数需要执行一些简单的自动化任务以删除其 PC 上的文件的人来说,这是一个很好的解决方案。如果您有任何问题,请随时发表评论。享受!
How to Automatically Delete Files in Windows
Earlier I wrotе about a program called DropIt that automatically moves or copies files for you when new files appear inside a folder. This can be useful, for example, if you have limited space on a local hard drive and want to move all your downloads off to an external storage device.
If you want to automatically delete files, there are two ways you can go about it in Windows. The first method involves downloading a freeware app called AutoDelete that lets you configure a schedule for deleting files in a particular folder. I’ve already written two detailed guides on using the program (here and here), so check those out if you prefer a freeware program to get the job done.
The second method for deleting files is to create a batch file and then schedule that batch file to run. You can do all of that without installing any third-party software. In this article, I’ll walk you through the steps for creating a batch file and then using Task Scheduler to have the script run on a reoccurring basis.
Step 1 – Create Batch File
If creating a batch file sounds a bit scary or too technical, don’t worry because you don’t have to know what any of that means. I’ll explain what you need to copy and paste, where and what options you can change. First, open Notepad and copy and paste the following line of text:
forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c del @path"
The line above probably makes no sense, which is perfectly fine as I’ll explain it down below. Basically, it tells Windows to delete all files in the C:\Test folder and sub-folders that are older than 5 days. Here is what your Notepad file should look like.
Before we get into more details about the command, let’s save the file and give it a test run. First, create a folder on your computer called Test at the root of the C drive. Next, click File – Save and save the file as a batch file. To do that, type in a name followed by .bat and then change the Save as type dropdown to All Files.
Note that you can save the file to whichever location on the hard drive you like, it doesn’t really matter. Now create some dummy files in the Test folder and then double click on the Delete.bat file to run it. Anything get deleted? Probably not!
The reason why nothing was deleted is because the command has /D -5, which means files that are 5 days or older. In order to delete any file regardless of when it was created, you can either change the -5 to -0 or you can remove the /D -5 part altogether. Now if you run it, all the files will be deleted.
To customize the command, the first thing you can do is change the directory to something other than C:\Test. That’s as simple as copying the path from Windows Explorer for the directory you want and pasting it into the command in Notepad.
Next is the -s parameter that you see after the directory path. This indicates that the command should look into all sub-folders also. If you do not want to delete files from subfolders, go ahead and remove the -s parameter.
Next is -m followed by *.*, which means that the command should evaluate files of every kind. If you only want to delete a specific file type in a folder, like PDF files or JPG images, just change *.* to *.pdf or *.jpeg and it will only remove those files.
The /D -X part we already talked about in terms of how old the files have to be in order to qualify for deletion. You can either keep it with a value greater than 1, set it to 0, or remove it altogether. That’s about all we need to know about the command.
There are a few things to note about running this command. Firstly, when files are deleted, they do not go to the Recycle Bin, but instead are deleted permanently, so be careful when using it. Secondly, the command only deletes files, not folders.
Since this is a batch file, you could also add multiples versions of this command in the same file. For example, here I am creating a batch file that will delete all DOCX files older than 180 days, all PDF files older than 60 days and all TXT files regardless of how old the files are.
Step 2 – Schedule Batch File
Now that you have your batch file created and saved, let’s go ahead and schedule it to run on a reoccurring basis. To do this, we have to open up Task Scheduler.
Luckily, I’ve already written an article on how to schedule a batch file, so open that page to get started. Scroll down to the Schedule Batch File on PC Startup section and follow along.
The only thing you have to change is the Trigger. You can choose from Daily, Weekly, Monthly, When the computer starts, When I log on or When a specific event is logged.
When you pick something like Weekly or Monthly and click Next, you’ll get a new screen where you can configure the exact time and days you want the script to run.
Hopefully, this is a good solution for most people who need to perform some simple automated tasks for deleting files on their PCs. If you have any questions, feel free to post a comment. Enjoy!