CMD forfiles Select a file to batch from a folder or tree (easy to bulk delete files before n days) _dos/bat

Source: Internet
Author: User
forfiles [/P pathname] [/M searchmask] [/S]
[/C command] [/d [+ |-] {YYYY-MM-DD | dd}]

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

Parameter list:
/P pathname represents the path to start the search. The default folder is the current working
Directory (.).

/M Searchmask searches for files based on the search mask. The default search mask is ' * '.

/S directs forfiles recursively to subdirectories. Like "dir/s".

/C command represents the commands that are executed for each file. The command string should be
Enclose in double quotes.

The default command is "cmd/c echo @file." The following variables
Can be used in a command string:
@file-Returns the file name.
@fname-Returns a filename with no extension.
@ext-returns only the extension of the file.
@path-Returns the full path to the file.
@relpath-Returns the relative path of the file.
@isdir-Returns "TRUE" if the file type is a directory;
Returns "FALSE" if it is a file.
@fsize-Returns the file size in bytes.
@fdate-Returns the date on which the file was last modified.
@ftime-Returns the time the file was last modified.

To include special characters on the command line, use the character 0xHH
The form uses hexadecimal code (for example, 0x09 is tab).
The internal CMD.exe command should begin with "CMD/C".

/d Date selection file, whose last modification date is greater than or equal to (+),
or less than or equal to (-) the date specified in the "YYYY-MM-DD" format;
Or select a file whose last modification date is greater than or equal to (+)
The current date is a "DD" day, or less than or equal to (-) the current
Date minus "DD" day. Valid "DD" days can be
0-32768 any number within the range. If not specified,
"+" is treated as the default symbol.

/? Displays a help message.

For example:
Forfiles/?
Forfiles
forfiles/p c:\windows/s/M dns*.*
FORFILES/S/M *.txt/c "cmd/c type @file | More
forfiles/p C:\/s/m *.bat
FORFILES/D -30/m *.exe
/C "cmd/c Echo @path 0x09 was changed before 30. "
FORFILES/D 2001-01-01
/C "cmd/c Echo @fname is new on January 1, 2001. "
FORFILES/D +2012-5-1/c "cmd/c Echo @fname today is new. "
forfiles/m *.exe/d +1
FORFILES/S/M *.doc/c "cmd/c echo @fsize"
forfiles/m *.txt/c "cmd/c if @isdir ==false notepad.exe @file"

Here are some examples
===============================================================================
The following is the deletion of the batch of backup files N days ago, note: Under Windows2003, such as XP because there is no forfiles.exe, no test copies of 2003 of files to the XP system32 directory
Delete files with the last modified date of the C disk backup directory 10 days ago
=================================================================================
forfiles/p "C:\Backup"/d-10/c "cmd/c echo deleting @file ... && del/f @path"
=================================================================================
The following is a batch that replicates files to another location before a day
Copy the file in the C-packing directory to the D-packing directory with the last modification date greater than or equal to July 1, 2007
=================================================================================
forfiles/p "C:\"/"2007-7-1"/C "cmd/c copy @path d:\"
Windows Server 2003 built-in command-line files, suitable for systems above XP
Example:
forfiles/m *.bkf/d -28/c "cmd/c del @file/F"
Delete the current directory 28 days before the extension is BKF file (based on current system time)
Forfiles automatically deletes file delete 7 days ago
2008-09-12 15:01
Try it with Forfiles.exe.
forfiles/p "D:\test"/s/m *.*/d-7/C "cmd/c del @path"
Make a batch file, bat file with Forfiles.exe the same directory, build a bat file shortcut in the start.

Forfiles is not in some systems, especially packaged to facilitate the use of

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.