Windows periodically deletes a batch script for a file a day ago

Source: Internet
Author: User

Note:
1. If there is no forfiles.exe under XP, copy the Forfiles.exe file under Windows2003 to the System32 directory of XP to use the Forfiles command.

2. This document applies to Windows client operating system XP Win7/8/10,windows server-side operating system server 2003/2008/2012/2016.
3. If there are mistakes, trouble points out, thank you!


Forfiles.exe and Help

Describe:

Select a file (or a set of files) and on that file executes a command. This helps with batch jobs.

Grammar:

forfiles/p < target directory name >/d < days >/C < commands >


1. See what the three parameters listed are useful: (Simple parsing)

/p Specifies the directory in which to look for files, the default is the current working directory.

/d Specifies a date or number of days to compare whether the last modified date of the file meets the criteria.

/ C executes the command for each found file.


2. See what the three parameters listed are useful: (Detailed analysis)

/P <Path>: Specifies the location at which to start searching for files, or defaults to the current directory if not specified.

/M <SearchMask>: Wildcard characters used in file lookups, such as "*.log" in code, are all log files, and of course you can specify all log files that start with manmee such as "Manmee_*.log". If you do not specify this parameter, the default is "*. *".

/d [{+|-}][{<date>|<days>}]: Specifies the last modification time of the file you want to select, with "/d-7" above for all files that are based on the day, 7 days old.

Of course, you can also specify a specific time, for example: "/d-08/18/2009" so all files older than August 18, 2009. Note the specified time must be in the format "mm/dd/yyyy".

/C for all files to execute the specified command, the command body must be in double quotation marks ("), the default is" cmd/c echo @file ". The "cmd/c del/f @path" is used above to delete the specified file. (Here the @file and @path are variables, which are explained later in this article.)


==================== example shows ==========================


Example 1. To copy a file in the C packing directory with the last modified date greater than or equal to July 1, 2007 to the D packing directory :

forfiles/p "c \"/d "2007-7-1"//"cmd/c copy @path d:\"


Example 2.1 Delete the file that was last modified on the C-disk backup directory 10 days ago :

forfiles/p "C:\Backup"/d-10/c "cmd/c echo deleting @file ... && del/f @path"


Example 2.2 automatically delete D-disk test catalog last modified on 7 days ago file

forfiles/p "D:\test"/s/m * */d-7/C "cmd/c del @path"


Example 2.3 Delete 28 days before the current directory with a BKF file extension (based on the current system time)

forfiles/m *.bkf/d -28/c "cmd/c del @file/F"


Syntax parsing of deleted files
forefiles/p/m-d/C

FORFILES/P contains the full path of the file to be deleted (for example: F:\Logfiles)/M *.log-d -7/c "cmd/c del/f @path"


Explain the relevant parameters and commands

@PATH: Represents the full path of the file.

@File: Represents the file name.


========== Case Action ============


1. Delete all empty directories (to remove the D:\test directory as an example)

dir/ad/b/s d:\test\ |sort/r >d:\kill.txt

for/f "tokens=*"%%i in (d:\kill.txt) do rd "%%i"

del d:\kill.txt


2. Automatically delete files and directories 7 days ago

{The shortcut for building a bat file is started,d:\test is replaced with the directory path you want. Date refers to the date of the modification. }

principle: First delete the expired file, then delete all the empty directory, this is a complete deletion process  

@echo off

forfiles/p "D:\test"/s/m * */d-7/C "cmd/c del @path"

dir/ad/b/s d:\test\ |sort/r >d:\kill.txt

for/f "tokens=*"%%i in (d:\kill.txt) do rd "%%i"

Del D:\kill.txt


Windows periodically deletes a batch script for a file before a day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.