当前位置:网站首页 > 自然语言处理(NLP) > 正文

删除文件夹批处理命令(批处理删除指定文件命令)



在整理手上几台SQL SERVER 2000的数据库备份时,一方面为了方便快速还原数据库,另外一方面为了备份冗余、备份方式统一(先备份到本地,然后收上磁带),将以前通过Symantec Backup Exec直接备份上带的作业改成了如下方式:

Step 1: 通过数据库维护计划将备份生成在本地磁盘M,完整备份保留2天,事务日志备份保留3天

Step 2: 备份完成后通过Symantec Backup Exec将备份文件收上磁带。

但是发现即使SQL SERVER 2000的数据库维护计划设置了删除几天前的备份文件,但是发现根本没有删除过期备份。于是只好使用dos命令来处理。刚开始想用forfiles命令,结果我搜索的时候,发现Windows 2000下没有forfiles命令,后来通过从第三方复制过来,发现Windows 2000下也可以使用forfiles(如果不借助于forfiles命令,直接用批处理命令完成这个,那简直痛苦死了)

关于forfiles命令的语法如下所示

C:>forfiles /?
FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {yyyy-MM-dd | dd}]
Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.
Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).
    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .
    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".
    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped in double
                        quotes.
                        The default command is "cmd /c echo @file".
                        The following variables can be used in the
                        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.
                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".
    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "yyyy-MM-dd" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.
    /?                  Displays this help message.

Examples:

    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 30 days ago"
    FORFILES /D 2001-01-01
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +2014-12-15 /C "cmd /c echo @fname is new today"
    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"

对应的中文提示信息如下所示:

语法
forfiles [/p Path ] [/m SearchMask ] [/s ] [/c Command ] [/d [{+ | - }] [{MM / DD / YYYY | DD }]]
参数
/p Path
指定Path ,表明要从哪里开始搜索。默认的文件夹是当前工作目录,该目录通过键入句号(.) 指定。
/m SearchMask
按照SearchMask 搜索文件。默认的SearchMask 是*.* 。
/s
指示forfiles 在子目录中搜索。
/c Command
在每个文件上运行指定的Command 。带有空格的命令字符串必须用引号括起来。默认的Command 是"cmd /c echo @file" 。
/d [{+ | - }] [{MM / DD / YYYY | DD }]
选择日期大于或等于(+ )(或者小于或等于(- ))指定日期的文件,其中MM / DD / YYYY 是指定的日期,DD 是当前日期减去DD 天。如果未指定+ 或- ,则使用+ 。DD 的有效范围是0 - 32768。
/?
在命令提示符下显示帮助。
如下所示,由于Windows Server 2000下拷贝过来的forfiles命令的版本是V 1.1,使用参数必须为-p、-c、-m 而且参数后面不能有空格。

clipboard

如下所示,delete_old_backup.bat 删除2天前的完整备份、事务日志备份、以及维护计划生成的日志文件。

脚本编写、测试成功后,然后设置Task Schedule,大体步骤如下步骤所示:

Step 1:在控制面板找到任务计划,执行任务计划向导:

clipboard[7]

Step 2: 点击“浏览”按钮,选择M:DB_BACKUPdelete_old_backup.bat文件

clipboard[8]

Step 3:输入任务的名称,以及执行任务的Schedule

clipboard[9]

Step 4:设置任务执行的时间以及频率

clipboard[10]

Step 5:输入知晓计划任务的账号以及密码

clipboard[11]

Step 6:完成任务计划设置。

clipboard[12]

上面的bat主要是支持参数与日志的现实,确实不错,其实核心代码就是这样的

测试前最好备份一下数据

循环删除3天前的文件

Forfiles /S /P e:logweblog /M *.* /D -3 /C "cmd /c del /S /Q @file"

批处理文件是一种用于自动化执行一系列命令或操作的脚本文件。它可以在Windows操作系统中使用,通过命令行界面执行。

删除超过3天的文件和文件夹可以通过批处理文件来实现。下面是一个示例的批处理文件内容:

到此这篇关于使用forfiles命令批量删除N天前文件的文章就介绍到这了,更多相关forfiles 批量删除N天前文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

到此这篇删除文件夹批处理命令(批处理删除指定文件命令)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 用批处理删除文件(批处理删除文件名指定字符)2025-01-05 21:27:06
  • 批处理删除指定文件夹(批处理删除除了某个文件外的所有文件)2025-01-05 21:27:06
  • 批处理删除文件名指定字符(批处理文件删除指定文件)2025-01-05 21:27:06
  • 批处理删除文件名相同字符(批量删除文件名相同字符)2025-01-05 21:27:06
  • 批处理命令读取文件内容(批处理获取文件路径)2025-01-05 21:27:06
  • 批处理命令批量修改文件名(批处理命令修改文件内容)2025-01-05 21:27:06
  • 批处理修改文件夹名称(批处理文件修改文件名)2025-01-05 21:27:06
  • 批处理重命名文件名(批处理 重命名)2025-01-05 21:27:06
  • 删除文件批处理命令模板(删除文件夹bat命令)2025-01-05 21:27:06
  • cips日间处理时间(cips的日间业务处理时间)2025-01-05 21:27:06
  • 全屏图片