Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Search string in multiple files

$
0
0
I am trying to find all the .txt or files with specific extension and load the file path to array using "_FileListToArrayEx". The result was success and pretty good. I removed first row as it contain only number. The remaining array only contain link to files.

secondly, I have got a list of strings in a different array set with following content

$array[0] = string1
$array[1] = string2
$array[2] = string3
$array[3] = string4
$array[n] = stringn

Now what i am trying to do is search each content of array through all the files. If the match is found then write the particular arry to the different .txt file. Here is the _FileListToArrayEx UDF i used for finding files with specific extension :-

; #FUNCTION# ========================================================================================================================
; Name...........: _FileListToArray
; Description ...: Lists files and\or folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax.........: _FileListToArray($sPath[, $sFilter = "*"[, $iFlag = 0]])
; Parameters ....: $sPath - Path to generate filelist for.
;				 $sFilter - Optional the filter to use, default is *. (Multiple filter groups such as "All "*.png|*.jpg|*.bmp") Search the Autoit3 helpfile for the word "WildCards" For details.
;				 $iFlag - Optional: specifies whether to return files folders or both Or Full Path (add the flags together for multiple operations):
;				 |$iFlag = 0 (Default) Return both files and folders
;				 |$iFlag = 1 Return files only
;				 |$iFlag = 2 Return Folders only
;				 |$iFlag = 4 Search subdirectory
;				 |$iFlag = 8 Return Full Path
; Return values .: @Error - 1 = Path not found or invalid
;				 |2 = Invalid $sFilter
;				 |3 = Invalid $iFlag
;				 |4 = No File(s) Found
; Author ........: SolidSnake <MetalGX91 at GMail dot com>
; Modified.......:
; Remarks .......: The array returned is one-dimensional and is made up as follows:
;							 $array[0] = Number of Files\Folders returned
;							 $array[1] = 1st File\Folder
;							 $array[2] = 2nd File\Folder
;							 $array[3] = 3rd File\Folder
;							 $array[n] = nth File\Folder
; Related .......:
; Link ..........:
; Example .......: Yes
; Note ..........: Special Thanks to Helge and Layer for help with the $iFlag update speed optimization by code65536, pdaughe
;				 Update By DXRW4E
; ===================================================================================================================================
Func _FileListToArrayEx($sPath, $sFilter = "*", $iFlag = 8)
	Local $hSearch, $sFile, $sFileList, $iFlags = StringReplace(BitAND($iFlag, 1) + BitAND($iFlag, 2), "3", "0"), $sSDir = BitAND($iFlag, 4), $FPath = "", $sDelim = "|", $sSDirFTMP = $sFilter
	$sPath = StringRegExpReplace($sPath, "[\\/]+\z", "") & "\" ; ensure single trailing backslash
	If Not FileExists($sPath) Then Return SetError(1, 1, "")
	If BitAND($iFlag, 8) Then $FPath = $sPath
	If StringRegExp($sFilter, "[\\/:><]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
	If Not ($iFlags = 0 Or $iFlags = 1 Or $iFlags = 2 Or $sSDir = 4 Or $FPath <> "") Then Return SetError(3, 3, "")
	$hSearch = FileFindFirstFile($sPath & "*")
	If @error Then Return SetError(4, 4, "")
	Local $hWSearch = $hSearch, $hWSTMP = $hSearch, $SearchWD, $sSDirF[3] = [0, StringReplace($sSDirFTMP, "*", ""), "(?i)(" & StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace("|" & $sSDirFTMP & "|", '\|\h*\|[\|\h]*', "\|"), '[\^\$\(\)\+\[\]\{\}\,\.\]', "\\$0"), "\|([^\*])", "\|^$1"), "([^\*])\|", "$1\$\|"), '\*', ".*"), '^\||\|$', "") & ")"]
	While 1
		$sFile = FileFindNextFile($hWSearch)
		If @error Then
			If $hWSearch = $hSearch Then ExitLoop
			FileClose($hWSearch)
			$hWSearch -= 1
			$SearchWD = StringLeft($SearchWD, StringInStr(StringTrimRight($SearchWD, 1), "\", 1, -1))
		ElseIf $sSDir Then
			$sSDirF[0] = @extended
			If ($iFlags + $sSDirF[0] <> 2) Then
				If $sSDirF[1] Then
					If StringRegExp($sFile, $sSDirF[2]) Then $sFileList &= $sDelim & $FPath & $SearchWD & $sFile
				Else
					$sFileList &= $sDelim & $FPath & $SearchWD & $sFile
				EndIf
			EndIf
			If Not $sSDirF[0] Then ContinueLoop
			$hWSTMP = FileFindFirstFile($sPath & $SearchWD & $sFile & "\*")
			If $hWSTMP = -1 Then ContinueLoop
			$hWSearch = $hWSTMP
			$SearchWD &= $sFile & "\"
		Else
			If ($iFlags + @extended = 2) Or StringRegExp($sFile, $sSDirF[2]) = 0 Then ContinueLoop
			$sFileList &= $sDelim & $FPath & $sFile
		EndIf
	WEnd
	FileClose($hSearch)
	If Not $sFileList Then Return SetError(4, 4, "")
	Return StringSplit(StringTrimLeft($sFileList, 1), "|")
MsgBox(4096,"",$sFile)	
EndFunc

and here is the code i used for deleting first row :-

For $n = UBound($array) - 1 to 0 step -1
	If IsInt ($array[$n]) Then ;= "2" Then
	 _ArrayDelete($array, $n)
	EndIf
Next

I am struck hereater. Any help is appriciated.

Meanwhile i used _FindinFile UDF written by guinness, but it did not give me any output. Attached screenshot shows the result of _FinfinFile. Even this UDF solution will also solve half of my problem.

Attached Thumbnails

  • findinfile.png

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>