2013-08-26 49 views
0

嗨我想寫一個批處理文件,將搜索一些目錄,並輸出一個文件文件報告與保存在一定的文件大小之間的最後一天的文件列表。批處理文件列出在過去24小時內保存的文件

文件大小部分是沒有問題的 - 但如何解析日期並根據今天的日期進行檢查,並將其添加到'if'語句中?

這是我到目前爲止有:

@echo Report will open when complete 
@echo Working 
@echo off 
setlocal 
set "SEARCH_DIR=f:" 
set "MIN_SIZE=1" 
set "MAX_SIZE=300000" 
set "REPORT=F:\Error_report.txt" 

echo **************************************************** >> %REPORT% 
echo File report %date% %time% >> %REPORT% 
echo File size %MIN_SIZE% to %MAX_SIZE% >> %REPORT% 
echo **************************************************** >> %REPORT% 

echo File list: >> %REPORT% 

for /R "%SEARCH_DIR%" %%F in (*) do (
    if exist "%%F" if %%~zF LSS %MAX_SIZE% if %%~zF GEQ %MIN_SIZE% echo %%F >> %REPORT% 
) 

@echo Done 

START %REPORT% 

我嘗試添加if forfiles /d +1 if語句 - 但是,這並不工作!

任何幫助,將不勝感激。

回答

0

爲什麼不直接使用find工具?您可以在一次調用中對文件進行過濾,並用一些文本進行包裝。一個例子:

find Documents/ -daystart -mtime "1" -size +1k 

請參閱手冊頁的細節,也有數以百萬計的例子在互聯網上。

+0

我懷疑OP不在(li)(u)nix盒子上... – rene

+0

對不起,實際上我現在意識到這看起來確實像MS-Windoze(發現了'F:\''符號)。那麼確實沒有這些工具,對不起。有趣的是,我甚至沒有想到,幾乎每個人都在這裏使用Linux ... – arkascha

+0

LOL ...夢想..​​. – rene

1

您可以使用​​此:

find * -size +1k -mtime -1 
 
-size n[c] 
    The primary shall evaluate as true if the file size in bytes, divided by 512 and rounded up to the next integer, is n. If n is followed by the character 'c', the size shall be in bytes. 
-atime n 
    The primary shall evaluate as true if the file access time subtracted from the initialization time, divided by 86400 (with any remainder discarded), is n. 
-ctime n 
    The primary shall evaluate as true if the time of last change of file status information subtracted from the initialization time, divided by 86400 (with any remainder discarded), is n. 
-mtime n 
    The primary shall evaluate as true if the file modification time subtracted from the initialization time, divided by 86400 (with any remainder discarded), is n. 

OPERANDS 

    The following operands shall be supported: 

    The path operand is a pathname of a starting point in the directory hierarchy. 

    The first argument that starts with a '-', or is a '!' or a '(', and all subsequent arguments shall be interpreted as an expression made up of the following primaries and operators. In the descriptions, wherever n is used as a primary argument, it shall be interpreted as a decimal integer optionally preceded by a plus ('+') or minus ('-') sign, as follows: 

    +n 
     More than n. 
    n 
     Exactly n. 
    -n 
     Less than n. 


1

我覺得PowerShell的應該是更爲合適的位置:

function Get-ErrorReport { 
    param(
    [string]$Path = 'F:\', 
    [long]$MinSize = 1, 
    [long]$MaxSize = 300000, 
    [string]$OutputPath = 'F:\Error_report.txt' 
) 

    Get-ChildItem -Recurse $Path | 
    Where-Object { 
     $_.Length -ge $MinSize -and 
     $_.Length -le $MaxSize -and 
     $_.LastWriteTime -gt (Get-Date).AddDays(-1) 
    } | 
    Select-Object -ExpandProperty FullName | 
    Out-File $OutputPath 

    Invoke-Item $OutputPath 
} 

以各種方式被稱爲

Get-ErrorReport 
Get-ErrorReport -MinSize 1KB -MaxSize 10MB 
Get-ErrorReport -Path X:\ -OutputPath X:\report.txt 
... 
相關問題