您好我正在尝试编写一个批处理文件来搜索某些目录并输出一个文本文件报告,其中包含在特定文件大小之间保存的文件列表。
文件大小部分没问题 - 但是我如何解析日期并在今天的日期检查它,并将其添加到'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 :(得分:1)
您可以使用GNU find for Windows:
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 :(得分: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
...
答案 2 :(得分:0)
为什么不直接使用find
实用程序?您可以在一次调用中对文件进行过滤,并将其包装在某些文本中。一个例子:
find Documents/ -daystart -mtime "1" -size +1k
有关详细信息,请参阅手册页,互联网上还有数百万个示例。