批处理文件列出在Windows Server 2008上具有特定文件名和特定内容的所有txt文件

时间:2014-01-31 10:43:53

标签: windows batch-file command-line

我正在寻找一个批处理文件,允许我搜索在特定时间范围内创建的文件(例如2013-01-01至2013-01-20),并使用特定的名称模式(例如* foo.xml) )包含某个字符串(例如“mySearchString”)。

我尝试了一些使用forfiles的东西,但它不允许我设置一个日期 - 它只允许我在某个日期之前或之后搜索文件。

有人帮我这个吗?

提前致谢

1 个答案:

答案 0 :(得分:2)

set "searchterm=apple"更改为您的搜索字词

将这些行更改为您需要的日期 set oldest=2006-06-01 set newest=2006-12-31

更改此行中的filespec:"c:\files\*.txt"

@echo off
:: based upon code by Todd Vargo 2007
setlocal

set "searchterm=apple"

:: Set date range below in yyyy-mm-dd format or your local format.
set oldest=2006-06-01
set newest=2006-12-31
::  =================
set tmp1="%temp%.\dir.out"

::  Set any legal filespec desired below.
dir "c:\files\*.txt" /a-d/s/b >%tmp1%
::  ==================

set vbs="%temp%.\tmp.vbs"
type nul>%vbs%
call :vbs echo >>%vbs%
cscript /nologo %vbs% >"%temp%\filelist.tmp"
del %vbs%
del %tmp1%

for /f "usebackq delims=" %%a in ("%temp%\filelist.tmp") do (
findstr /i /c:"%searchterm%" "%%a" >nul && echo found "%searchterm%" in "%%a"
)
del "%temp%\filelist.tmp" 2>nul
pause
goto :eof

:vbs
%1 d1 = #%oldest%#
%1 d2 = #%newest%#
%1 Const ForReading = 1, ForWriting = 2
%1 'Wscript.Echo DateDiff("d", d1, d2)
%1 Set fso = CreateObject("Scripting.FileSystemObject")
%1 Set f = fso.OpenTextFile(%tmp1%, ForReading)
%1 count = 0
%1 Do Until f.AtEndOfStream
%1   s = f.ReadLine
%1   Set f1 = fso.GetFile(s)
%1   If d1 ^< DateValue(f1.DateCreated) and _
%1     DateValue(f1.DateCreated) ^< d2 Then
%1     Wscript.Echo f1.Path
%1     count = count + 1
%1   End If
%1 Loop
%1 ' Wscript.Echo "     Total Files Listed:", count