2009-06-24 154 views
15

我正在处理一个批处理脚本,它使我可以使用forfiles删除早于设定时间段的文件。现在,我打算打印将被删除的文件。Forfiles批处理脚本(转义@字符)

我使用的forfiles调用从cmd.exe外壳完美地工作,但只要我将它嵌入到批处理脚本中,它就会使用barf。我怀疑这是由于@角色没有正确逃脱,但我不确定。

我跑的命令是:

forfiles /S /P "r:\" /m *.bak /d -10 /c "cmd /c echo @PATH" 

而且它在以下错误的结果:

ERROR: Invalid argument/option - '@PATH' 
Type "FORFILES /?" for usage. 

我GOOGLE了所有的地方,并尝试了几种不同的方案来逃避@PATH组件。一切从@@ PATH,到\“@ PATH \”没有结果。

任何帮助,将不胜感激!

编辑:我还应该注意到,我立足很多我FORFILES知识从here.

回答

33

我有同样的问题,直到我删除周围的目录路径引号,像这样:

forfiles /S /P r:\ /m *.bak /d -10 /c "cmd /c echo @PATH" 

希望有所帮助。

+0

拆除工作的报价的影响! 非常感谢! 对于我来说,把一个文件路径(一个标准过程)放在引号中会以一种非显而易见的方式将一个单独的参数打断到命令,这似乎令人难以置信地奇怪。 – Paradox 2009-06-24 16:08:12

+0

是的,它很奇怪,但我刚才也遇到了同样的问题。好的解决方案:) – jsight 2009-08-26 18:32:23

+1

谢谢,但这是愚蠢的。为什么在2012年,我必须将我的路径转换为DOS`f:\ MySQLB〜1 \`格式?大声笑。但至少现在起作用了! – cbmeeks 2012-09-18 14:21:58

0

forfiles.exe让它正常工作,否则当你使用批处理文件时它不会通过@variables。如果你在命令提示符下,Forfiles会起作用,但是当你在批处理文件中运行它时,变量不能正常工作,除非你输入:forfiles.exe

下面是一些删除txt文件超过30天 forfiles.exe /P c:\directory\ /M *.txt /C "cmd /c del @path" /d -30

17

尝试从/P路径修整后\一个例子。然后,您应该能够使用引号来封装包含空间的路径。

0

我发现有两个版本的FORFILES,一个是1998版(感谢Emmanuel Boersma),另一个是2005版(修改日期时间显示它)。

FORFILES v 1.1 - by Emmanuel Boersma - 4/98 

Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand] [-dDDMMYY] [-s] 

-pPath    Path where to start searching 
-mSearch Mask  Search files according to <Search Mask> 
-cCommand   Command to execute on each file(s) 
-d[+|-][DDMMYY|DD] Select files with date >= or <=DDMMYY (UTC) 
        or files having date >= or <= (current date - DD days) 
-s     Recurse directories 
-v     Verbose mode 

The following variables can be used in Command : 
@FILE, @PATH, @RELPATH, @ISDIR, @FSIZE, @FDATE, @FTIME 

Default : <Directory : .> <Search Mask : *.*> <Command : "CMD /C Echo @FILE"> 
Examples : 
FORFILES -pc:\ -s -m*.BAT -c"CMD /C Echo @FILE is a batch file" 
FORFILES -pc:\ -s -m*.* -c"CMD /C if @ISDIR==TRUE echo @FILE is a directory" 
FORFILES -pc:\ -s -m*.* -d-100 -c"CMD /C Echo @FILE : date >= 100 days" 
FORFILES -pc:\ -s -m*.* -d-010193 -c"CMD /C Echo @FILE is quite old!" 

每个版本都有其独特的语法。

FORFILES [/P pathname] [/M searchmask] [/S] 
     [/C command] [/D [+ | -] {MM/dd/yyyy | 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 
         "MM/dd/yyyy" 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 01/01/2001 
      /C "cmd /c echo @fname is new since Jan 1st 2001" 
    FORFILES /D +3/19/2012 /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" 

祝您有个愉快的时间让“批处理文件”更复杂。 :)

2

这是一个老问题,但我有一个不同的答案...以防万一任何人需要它。

使用'forfiles'时,路径(写在/ p之后)可以在引号之间。但是,它不能以斜线结尾。

如果你想运行“FORFILES”的驱动器的根目录:
forfiles /p "C:" /c "cmd /c echo @file"

如果你想在不同的目录中处理文件...
forfiles /p "C:\Program Files" /c "cmd /c echo @file"

换句话说,最安全的方法是:

  • 始终使用引号(因为带有空格的文件夹,如“程序文件”,仍然可以工作)
  • 总是忽略最后结尾的斜线

forfiles /p "C:\Path\Without\Trailing\Slash"

0
dir *.* > C:\path\dummy%date:~4,2%%date:~7,2%%date:~10,4%.DAT 

dir *.* > C:\path\dummy%date:~4,2%%date:~7,2%%date:~10,4%.csv 

forfiles -p C:\path\ -m *.DAT /d -50 /c "cmd /c del /Q @path" 
forfiles -p C:\path\ -m *.csv /d -50 /c "cmd /c del /Q @path" 
  1. 更换的.dat和.csv文件ü要删除的内容。

  2. -50删除旧的然后50天

  3. 这是Windows的批处理文件

0

没有工作

FORFILES /P %deletepath% /M *.%extension% /D -%days% /C "cmd /c del @PATH"

没有工作

FORFILES /P %deletepath% /M *.%extension% /D -%days% /C "cmd /c del @path"

@path in lower case works。

在我到处搜索后,我在自己的测试中发现了答案。使用服务器2012 R2上的最新版本,我尝试将@PATH更改为小写。这为我修好了。

祝你好运!

0

最佳做法是在路径(/ P)参数周围使用双引号来处理带空格的路径。

当替换变量包含尾部反斜杠时,会发生该问题。反斜杠'逃避'引用,导致FORFILES错误地解释命令行的其余部分。

按照惯例,到目录的路径不需要尾随反斜杠,这是根目录的一个例外。只指定驱动器号和冒号C:不会引用根,而是指该驱动器的“当前目录”。要引用根,必须使用尾部反斜杠C:\

我的解决方案如下:

当使用FORFILES,追加/P参数的关闭“一个.之前例如

FORFILES /P "%somePath%." /C "CMD /C ECHO @path" 

置换后,这导致了形式C:\.C:\TEMP.C:\TEMP\.的路径。所有这些都由FORFILES和DIR正确处理。

我没有测试所有可能的替代FORFILES变量,是@path似乎是通过添加.