#region: - Options Opt('MustDeclareVars', 1) Opt('TrayIconDebug', 1) Opt('TrayIconHide', 0) #endregion HotKeySet('{ESC}', '_Pro_Exit') Local $sSerchInDirPath = 'd:\Обмен'; Директория, в которой будет произведен поиск файлов, включая поддиректории. Local $sFilesSerchRegMask = '.+\.bat'; Фильтр поиса файлов, заданный в виде регулярного выражения. Local $sSearchString = '789987789'; Искомый текст в тексте файла. Local $sReplaceString = '---------'; Текст, котрый будет заменять искомый текст. _ReplaceTextInFile($sSerchInDirPath, $sFilesSerchRegMask, $sSearchString, $sReplaceString) If @error Then MsgBox(48, 'Ошибка: ' & @error, 'Искомые файлы не найдены') Func _ReplaceTextInFile($sSerchInDirPath, $sFilesSerchRegMask, $sSearchString, $sReplaceString) Local $i, $aFilesList, $sFileText, $hFile $aFilesList = _GetFilesDirsList($sSerchInDirPath, $sFilesSerchRegMask) If NOT $aFilesList[0] Then Return SetError(1) $sSerchInDirPath = StringStripWS(StringRegExpReplace($sSerchInDirPath, '\\+$', ''), 1+2) For $i=1 To UBound($aFilesList)-1 $sFileText = FileRead($aFilesList[$i]) $sFileText = StringReplace($sFileText, $sSearchString, $sReplaceString) $hFile = FileOpen($aFilesList[$i], 2) FileWrite($hFile, $sFileText) FileClose($hFile) Next EndFunc Func _GetFilesDirsList($sSerchInDirPath, $sFilesSerchRegMask='.*', $sDirsSerchRegMask='.*', $iAddFiles=1, $iAddDirs=0, $fSerchInWrongDir=1, $sRootPath=1, $fAddFromRootDir=1) $sSerchInDirPath = StringRegExpReplace($sSerchInDirPath, '\\+$', '') Local $aResultList[1], $aSubResultList[1], $aSub2ResultList[1], $hSerch, $sFileName, $sFilePath, $fAddToResult, $fIsDir, $fDirsSerchRegMask, $fFilesSerchRegMask, $fSerchInWrongDirSub, $fAddFromRootDirSub, $i $aResultList[0] = 0 $hSerch = FileFindFirstFile($sSerchInDirPath & '\' & '*') If @error Then Return SetError(1, 0, $aResultList) If $sRootPath == 1 AND $iAddDirs == 1 AND $fAddFromRootDir Then $aResultList[0] += 1 ReDim $aResultList[$aResultList[0]+1] $aResultList[$aResultList[0]] = $sSerchInDirPath EndIf While 1 $sFileName = FileFindNextFile($hSerch) If @error == 1 Then ExitLoop $sFilePath = $sSerchInDirPath & '\' & $sFileName $fAddToResult = 0 $fIsDir = StringInStr(FileGetAttrib($sFilePath), 'D') $fDirsSerchRegMask = StringRegExp(StringRegExpReplace($sFilePath, '.+\\(.+)$', '\1'), $sDirsSerchRegMask) $fFilesSerchRegMask = StringRegExp(StringRegExpReplace($sFilePath, '.+\\(.+)$', '\1'), $sFilesSerchRegMask) $fAddFromRootDirSub = 1 If NOT $fDirsSerchRegMask Then $fAddFromRootDirSub = 0 If $fIsDir Then If $iAddDirs == 1 AND $fDirsSerchRegMask Then $fAddToResult = 1 Else If $iAddFiles == 1 AND $fFilesSerchRegMask AND $fAddFromRootDir Then $fAddToResult = 1 EndIf If $fAddToResult Then If $sRootPath == 1 AND NOT $fIsDir Then $aResultList[0] += 1 ReDim $aResultList[$aResultList[0]+1] $aResultList[$aResultList[0]] = $sFilePath Else $aSubResultList[0] += 1 ReDim $aSubResultList[$aSubResultList[0]+1] $aSubResultList[$aSubResultList[0]] = $sFilePath EndIf EndIf $fSerchInWrongDirSub = 1 If NOT $fSerchInWrongDir AND $fIsDir AND NOT $fDirsSerchRegMask Then $fSerchInWrongDirSub = 0 $aSub2ResultList[0] = 0 If $fIsDir Then If $fSerchInWrongDirSub Then _ $aSub2ResultList = _GetFilesDirsList($sFilePath, $sFilesSerchRegMask, $sDirsSerchRegMask, $iAddFiles, $iAddDirs, $fSerchInWrongDir, 0, $fAddFromRootDirSub) EndIf If $aSub2ResultList[0] Then For $i = 1 To $aSub2ResultList[0] $aSubResultList[0] += 1 ReDim $aSubResultList[$aSubResultList[0]+1] $aSubResultList[$aSubResultList[0]] = $aSub2ResultList[$i] Next EndIf WEnd FileClose($hSerch) For $i=1 To UBound($aSubResultList)-1 $aResultList[0] += 1 ReDim $aResultList[$aResultList[0]+1] $aResultList[$aResultList[0]] = $aSubResultList[$i] Next Return SetError(0, 0, $aResultList) EndFunc Func _Pro_Exit() Exit EndFunc |