繁体   English   中英

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

[英]Batch file to list all txt files with a certain filename and certain content on windows server 2008

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

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

有人帮我这个吗?

提前致谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM