如何查找文件夹下所有文本内(.txt)是否包含某个字符
D:\LOG 文件夹下有,1.txt、2.txt、3.txt、4.txt、.......等等文件我想找出这些文件内是否包含 错误 这个字符,如果有则提示在哪个文件内,请问该怎么弄啊 本帖最后由 dunphy 于 2014-11-8 20:49 编辑
打个比方你要找的错误字符为@,(如果是更复杂的需要正则), 将四个文本放到D盘1234文件夹下
1.txtsdfasdfsadfasdadfasdfasdfasdfs!ER#$@sdfasdfsadfasdadfasdfasdfas2.txtsdfasdfsad@fasdadfasdfasdfasdfs!ER#$@sdfasdfsadfasdadfasdfasdfas
sdfasdfsadfasdadfasdfasdfasdfs!ER#$@sdfasdfsadfasdadfasdfasdfas3.txtsdfsdfsf@w32
dsfgsg@dvsfgds
advsfgfhnjt5
Sdsdfgdgdgh
dsfw43@wrgt35y45@jmryjr4.txtadvfssfdfbg5324353@wr535t3y
ergergy43@243453
@Vadgwrgwg
VDsaf@gryjhr代码如下:
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Local $path = "d:\1234"
Local $aFileList = _FileListToArray($path, "*.txt")
If @error = 1 Then
MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
Exit
EndIf
If @error = 4 Then
MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
Exit
EndIf
; Display the results returned by _FileListToArray.
_ArrayDisplay($aFileList, "$aFileList")
Local $i, $aRetArray
For $i = 1 To UBound($aFileList) - 1 Step 1
_FileReadToArray($aFileList[$i], $aRetArray, 1)
_ArrayDisplay($aRetArray, "$aRetArray")
Local $j
For $j = 1 To UBound($aRetArray) - 1
Local $start = StringInStr($aRetArray[$j], "@") ;这里是你需要查找的错误字符
If $start <> "" Then
MsgBox(0, "提示", "在文件" & $aFileList[$i] & "中,第" & $j & "行,第" & $start & "个字符" & @CRLF & "有错误的字符@")
EndIf
Next
Next
本帖最后由 netegg 于 2014-11-8 17:42 编辑
回复 2# dunphy
不用这么麻烦吧,直接使用关键字作为分隔符,拆成数组,数组元素总数为1,就不含有 本帖最后由 netegg 于 2014-11-8 17:46 编辑
$text = fileread($file)
$aText = stringsplit($text,'@',2)
if ubound($aText)>1 then 回复 4# netegg
谢谢蛋蛋的指点,又学一种方法实现。。
页:
[1]