
[已解決]批處理如何獲取指定條件的文本列表最后由 syqh 于 -3-8 04:20問題已解決,多謝apang等多位高手的無私幫助。附上代碼:
一句話找出文本中含有關鍵字,且修改時間大于TTT的文本列表。一文件夾(類似中轉站),里面的txt文件會實時更新(新的進來,舊的刪除,或修改日期變化),文本數量較多,少則幾百,多則上千。txt文件名如11.txt,12.txt......txt文件內容相似,如:第1行第2行CABC.. 第200行其中第3行格式相同,均為(:LC+2個空格+字符) 字符最少2個,最多5個(如A1、BC5、G456、、、等):LC在文本中是唯一的若直接找文本,就找:LCABC若按行查找, 就找第3行是否為:LCABC(注,空行也算一行)請問如何找出:1):LC后字符=ABC2)文本修改時間>=/3/1 8:00的文件名寫入另一文本a.txt 【1)2)同時滿足】3)a.txt與程序在同一目錄,需處理的文件夾在另一目錄a.txt內容如:11.txt12.txt..不知能否實現,希望各位能幫忙,謝謝!nclick="copycode($('code0'));">復制代碼
- ''On Error Resume Next
- Dim strDir, arrKey, strDt,str1,str2,str3,str4,str5
- strDir = D:test
- arrKey = Array(LensSpeed=1200)
- ''strDt = /3/7 7:15:00
- ''strDt = now()
- Do while true
- str1=now() ''計時
- call Main()
- Wscript.Sleep 5000
- str2=now()-str1 ''用時
- str3=Hour(formatdatetime(str2,0))''時
- str4=Minute(formatdatetime(str2,0))''分
- str5=Second(formatdatetime(str2,0))''秒
- strDt=DateAdd(h, - & str3 , now())
- strDt=DateAdd(n, - & str4 , strDt)
- strDt=DateAdd(s, - & str5 , strDt)
- Loop
- Sub Main()
- Dim fso, f, d, txt, objFile, s
- s =
- Set fso = CreateObject(scripting.FileSystemObject)
- For Each f in fso.GetFolder(strDir).Files
- If LCase(Right(f, 2)) = .l or LCase(Right(f, 2)) = .r Then
- d = f.DateLastModified
- If CDate(d) > CDate(strDt) Then
- Set objFile = fso.OpentextFile(f, 1)
- txt = objFile.ReadAll
- ''txt = Replace(txt,vbCrLf,Chr(10))
- If TestKeyWord(txt) Then s = s & f.Name & vbCrLf
- objFile.Close
- End If
- End If
- Next
- fso.OpenTextFile(a.txt, 2, true).Write s
- ''CreateObject(Wscript.Shell).Run cmd /c ch.bat,,true''調用bat
- Set fso = Nothing
- End Sub
- Function TestKeyWord(ByVal txt)
- Dim i
- TestKeyWord = True
- txt = vbCrLf & txt & vbCrLf
- For i = 0 to UBound(arrKey)
- If InStr(txt, vbCrLf & arrKey(i) & vbCrLf) = 0 Then
- TestKeyWord = False : Exit For
- End If
- Next
- End Function

