Batch delete files or list files with

Source: Internet
Author: User

Automatically delete files from a few days ago

Windows 2003 2008 Test Passes

Use the Forfiles command to find data from a few days ago.

Several parameters of Forfiles:

/p is the path to the search. In our case, we're going to find the directory in which to delete the files.
/M search for files based on the search mask. The default is *, we want to delete files from a certain time ago. We only care about time.
/d file modification time is before or after a certain time. -200 indicates a file that was 200 days ago.
/C represents the command executed for each file, here is the file to delete, so for "cmd/c del/f/s/q @file". Where the variable @file represents the file name.

In summary, the following script is drawn:

@echo off
echo Del File::::.::: £ º
echo forfiles/p d:\test\data/m */s/d -200/c "cmd/c del/f/s/q @file"
echo forfiles/p d:\test\data/d -200/c "cmd/c del @file"

Echo Done
Echo. & Pase

Under Linux, you can use the Find command to locate:
Find./-ctime 1-name "*mail"-exec rm {} \;
Delete files that were modified a day ago.

REM Delete historical data from the previous day
forfiles/m *.fc/s/d-1/C "cmd/c del @file"

REM Delete empty folders in the current directory and its subdirectories
for/f "tokens=*"%a in (' dir/b/ad/s e:\filecache^|sort/r ') do rd "%a"/q 2>nul

Change% to percent in batch process
for/f "tokens=*"%%a in (' dir/b/ad/s e:\filecache^|sort/r ') do rd "%%a"/q 2>nul

Forfiles

Windows Server 2003 built-in commands

Switch rarely, p-path, M-way, S contains subdirectories, C executes commands, D-date

Normal use may not be as good as for,dir, but the C switch is very powerful.

Command string:
@file-returns The name of the file.
@fname-returns The file name without extension.
@ext-returns only the extension of the file.
@path-returns The full path of the file.
@relpath-returns The relative path of the file.
@isdir-returns "TRUE" If a file type is a directory, and "FALSE" for files.
@fsize-returns the size of the file in bytes.
@fdate-returns the last modified date of the file.
@ftime-returns The last modified time of the file.


forfiles/p c:\windows/s/M dns*.*
List all files at the beginning of DNS in Windows and its subdirectories

FORFILES/S/M *.txt/c "cmd/c type @file | More
Lists the contents of all TXT documents in the current directory and subdirectories, and prints them as pagination

forfiles/p c \/s/m *.bat
List the bat files under Windows and its subdirectories

forfiles/d -30/m *.exe/c "cmd/c Echo @path 0x09 was changed by days ago"

List the modified EXE files within 30 days, list path + custom text 0x09 (tab) was changed day ago

FORFILES/D 2001/01/01/c "cmd/c Echo @fname is new since Jan 1st 2001"

List files after 2001, 0101 and print the document name +is new since Jan 1st 2001

FORFILES/D +2009/4/10/c "cmd/c Echo @fname is new Today"

Lists the files that were modified after 20090410 and prints

forfiles/m *.exe/d-1

List the EXE files that were modified a day ago
FORFILES/S/M *.doc/c "cmd/c echo @fsize"

List doc files and print out file size

forfiles/m *.txt/c "cmd/c if @isdir ==false notepad.exe @file"

List the TXT file, if it is not a folder, then open the file with Notepad in turn, and then turn the next file off.

forfiles/m *.log/c "cmd/c del @file"
forfiles/m *.log/c "cmd/c if @fsize geq 1000000 (del @file)"
forfiles/m *.log/c "cmd/c if @fsize geq 1000000 (del @file) Else (move @file c:\archive)"
forfiles/m *.log/c "cmd/c if @fsize geq 1000000 (echo @file is 1MB or larger) Else (Echo @file was 1MB less)"

Batch delete files or list files with

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.