2009-06-24 152 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似乎是通過添加.