Что нового

[решено] Копирование файла если он найден в Массиве

Tosyk

Новичок
Сообщения
206
Репутация
0
Пишу скрипт, который ищет айтемы из одного массива (небольшого) в другом (большом).

айтем в небольшом массиве - это неполный путь к файлу (в аттаче civ_female_act3.model_materials.txt)
айтем в большом массиве - это полный путь к файлу (в аттаче TextureList.lst)

если айтем из небольшого массива найден в большом, то файл по полному пути копируется в папку. Сейчас я просто ищу $find. Сам скрипт:

Код:
#include <Array.au3>
#include <File.au3>

Global $aArrayInput, $aArrayOutput, $matArray
Global $sDir = @ScriptDir
Global $sFileName = '*'
Global $sFileExtension = 'texture.dds' ; or '*' for all files or 'pdf' for just PDFs
Global $sListPath = @ScriptDir & '\TextureList.lst'
Global $find = 'textures\cperrella\fabric\cloth_knits\wrinkles_01_n.texture' & '.dds'
Global $matInput = @ScriptDir & '\civ_female_act3.model_materials.txt'
Global $sExclude

$aArrayOutput = _FileListToArrayRec( $sDir, $sFileName & '.' & $sFileExtension, 1, 1, 0, 2 )
If IsArray($aArrayOutput) Then
   If FileExists($sListPath) Then
      _FileSearchInTXT()
   Else
      _FileWriteFromArray($sListPath, $aArrayOutput)
      _FileSearchInTXT()
;~       _ArrayDisplay( $aArrayOutput )
   EndIf
EndIf

_FileReadToArray($matInput, $matArray, $FRTA_COUNT)
_ArrayDelete($matArray, $matArray[1])

$sExclude = "===*|---*|shader*|material*| *"

$sExclude = StringReplace($sExclude, ".", "\.")
$sExclude = StringReplace($sExclude, "?", ".")
$sExclude = StringReplace($sExclude, "*", ".*?")

$iIndex = 0
For $i = 0 To UBound($matArray) - 1
    If NOT StringRegExp($matArray[$i], $sExclude) Then
        $matArray[$iIndex] = $matArray[$i]
        $iIndex += 1
    EndIf
Next
Redim $matArray[$iIndex]


For $i = UBound($matArray) - 1 To 0 Step -1
    If $matArray[$i] = "" Then
        _ArrayDelete($matArray, $i)
    EndIf
Next

_ArrayTrim($matArray, 17, 0)
_ArrayDisplay($matArray, "2D array - no count (trimmed)", Default, 8)


;~ ----------------------------------------------------------------------------------------------
Func _FileSearchInTXT()
If $sListPath <> '' And $find <> '' Then
    _FileReadToArray($sListPath, $aArrayInput)
    For $i = 1 To UBound($aArrayInput) - 1
        If StringInStr($aArrayInput[$i], $find) Then
           ConsoleWrite("Found file: " & $aArrayInput[$i] & @CRLF)
        EndIf
     Next
Else
    MsgBox(48, 'Error', 'A file was not picked or what to find was cancelled/empty!')
EndIf
EndFunc    ;==>_FileSearchInTXT
подскажите пожалуйста как это сделать.
 

Вложения

  • TextureList.zip
    1.4 КБ · Просмотры: 0

IMStrelcov

CTPEJIbLLOB
Сообщения
253
Репутация
64
Код:
Global $aArraySmall[3] = ['1.txt', '3.txt', '5.txt']
Global $aArrayBig[5] = ['c:\2.txt', 'd:\4.txt', 'e:\5.txt', 'f:\6.txt', 'g:\7.txt']

For $iA = 0 To UBound($aArraySmall) -1
   For $iB = 0 To UBound($aArrayBig) -1
      $sFile = StringRegExp($aArrayBig[$iB], '([^\\]+)?\z', 2)[0]
      If $aArraySmall[$iA] = $sFile Then
         ;FileCopy $aArrayBig[$iB]
         ConsoleWrite($aArraySmall[$iA] & ' = ' & $aArrayBig[$iB] & @LF)
      EndIf
   Next
Next
 
Автор
Tosyk

Tosyk

Новичок
Сообщения
206
Репутация
0
Код:
Global $aArraySmall[3] = ['1.txt', '3.txt', '5.txt']
Global $aArrayBig[5] = ['c:\2.txt', 'd:\4.txt', 'e:\5.txt', 'f:\6.txt', 'g:\7.txt']

For $iA = 0 To UBound($aArraySmall) -1
   For $iB = 0 To UBound($aArrayBig) -1
      $sFile = StringRegExp($aArrayBig[$iB], '([^\\]+)?\z', 2)[0]
      If $aArraySmall[$iA] = $sFile Then
         ;FileCopy $aArrayBig[$iB]
         ConsoleWrite($aArraySmall[$iA] & ' = ' & $aArrayBig[$iB] & @LF)
      EndIf
   Next
Next
отлично, вроде всё понятно даже мне! :smile:
только можете объяснить вот это: '([^\\]+)?\z' ?
до меня очень туго доходят регулярные выражения
Сообщение автоматически объединено:

кстати, я попробовал, но сейчас получается, что в моих списках, например:
строчка в маленком списке: 'textures\default_pink_shader\default_pink_shader_n.texture'
строчка в большом списке: 'D:\_r\to_con\_gm\textures\cperrella\fabric\cloth_knits\wrinkles_01_n.texture.dds'

а в вашем скрипте получается, что эти же данные сравниваются как:
строчка в маленком списке: 'textures\default_pink_shader\default_pink_shader_n.texture'
строчка в большом списке: 'default_pink_shader_n.texture.dds'

и оно не работает
 
Последнее редактирование:

IMStrelcov

CTPEJIbLLOB
Сообщения
253
Репутация
64
и оно не работает
Код:
StringRegExp($aArrayBig[$iB], '([^\\]+)?\z', 2)[0]

берет только имя файла, без всяких частей пути, поэтому и не совпадение папка\файл с файл

Вот так будет работать как вы хотите
Код:
Global $aArraySmall[3] = ['1.txt', '3.txt', '5.txt']
Global $aArrayBig[5] = ['c:\2.txt', 'd:\4.txt', 'e:\5.txt', 'f:\6.txt', 'g:\7.txt']

For $iA = 0 To UBound($aArraySmall) -1
   For $iB = 0 To UBound($aArrayBig) -1
      If StringInStr($aArrayBig[$iB], $aArraySmall[$iA]) Then
         ;FileCopy $aArrayBig[$iB]
         ConsoleWrite($aArraySmall[$iA] & ' = ' & $aArrayBig[$iB] & @LF)
         ;ExitLoop ;прервать поиск для текущего файла из маленького списка и начать поиск другого
      EndIf
   Next
Next

Но учтите, что если в большем файле у вас будет:
D:\folder_1\folder_3\file
D:\folder_2\folder_3\file
а искать будете:
folder_3\file или file
то может быть беда, зависит от того как вы построите скрипт.
 
Автор
Tosyk

Tosyk

Новичок
Сообщения
206
Репутация
0
у меня получилось закончить скрипт почти без регулярных выражений. спасибо! прикрепил аттачем потому что форум не позволяет писать сообщения больше 20000 символов. самая сложная часть была вот в этом месте:
Код:
[URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Func[/URL] _Settings()
    [URL='https://www.autoitscript.com/autoit3/docs/libfunctions/_FileReadToArray.htm']_FileReadToArray[/URL]($sListPath, $aArrayOutput)
    [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']If[/URL] [URL='https://www.autoitscript.com/autoit3/docs/functions/IsArray.htm']IsArray[/URL]($aArrayOutput) [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Then[/URL]
        [URL='https://www.autoitscript.com/autoit3/docs/libfunctions/_FileReadToArray.htm']_FileReadToArray[/URL]($matInput, $aMatArray)

        ;~----------------------------------------------------------------------------------------------
        ;~ CLEARING matArray START
        ;~----------------------------------------------------------------------------------------------

        ; Exlude mask
        $sExclude = "(?i)^===*|(?i)^---*|(?i)^shader*|(?i)^material*|(?i)^template*|^\s|^ *"
        $sExclude = [URL='https://www.autoitscript.com/autoit3/docs/functions/StringReplace.htm']StringReplace[/URL]($sExclude, ".", "\.")
        $sExclude = [URL='https://www.autoitscript.com/autoit3/docs/functions/StringReplace.htm']StringReplace[/URL]($sExclude, "*", ".*?")

        ; Remove redundant lines by mask
        [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']For[/URL] $i = 1 [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']To[/URL] $aMatArray[0]
            [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']If[/URL] [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']NOT[/URL] [URL='https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm']StringRegExp[/URL]($aMatArray[$i], $sExclude) [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']And[/URL] $aMatArray[$i] <> "" [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Then[/URL]
                $aMatArray[$iIndex] = $aMatArray[$i]
                $aMatArray[$i] = "" ; add this?
                $iIndex += 1
            [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Else[/URL]
                [URL='https://www.autoitscript.com/autoit3/docs/functions/ConsoleWrite.htm']ConsoleWrite[/URL]("Ignored lines: " & $i & " | " & $iIndex & " | " & $aMatArray[$i] & [URL='https://www.autoitscript.com/autoit3/docs/macros.htm#%40CRLF']@CRLF[/URL])
                [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']EndIf[/URL]
        [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Next[/URL]

        [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']ReDim[/URL] $aMatArray[$iIndex]
        $aMatArray[0] = $iIndex - 1

        ; Remove first 17 characters of hash
        [URL='https://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayTrim.htm']_ArrayTrim[/URL]($aMatArray, 17, 0)

;~      Replace '/' with '\'
        [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']For[/URL] $i = 0 [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']To[/URL] [URL='https://www.autoitscript.com/autoit3/docs/functions/UBound.htm']Ubound[/URL]($aMatArray) - 1
         $aMatArray[$i] = [URL='https://www.autoitscript.com/autoit3/docs/functions/StringReplace.htm']StringReplace[/URL]($aMatArray[$i], "/", "\")
        [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Next[/URL]

;~      Remove duplicates
        $aMatArrayUnique = [URL='https://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayUnique.htm']_ArrayUnique[/URL]($aMatArray)

        ;~----------------------------------------------------------------------------------------------
        ;~ CLEARING matArray END
        ;~----------------------------------------------------------------------------------------------

;       Copy files to Material folder
        [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']For[/URL] $iA = 0 [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']To[/URL] [URL='https://www.autoitscript.com/autoit3/docs/functions/UBound.htm']UBound[/URL]($aMatArrayUnique) -1
         [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']For[/URL] $iB = 0 [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']To[/URL] [URL='https://www.autoitscript.com/autoit3/docs/functions/UBound.htm']UBound[/URL]($aArrayOutput) -1
            [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']If[/URL] [URL='https://www.autoitscript.com/autoit3/docs/functions/StringInStr.htm']StringInStr[/URL]($aArrayOutput[$iB], $aMatArrayUnique[$iA]) [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Then[/URL]
                [URL='https://www.autoitscript.com/autoit3/docs/functions/FileCopy.htm']FileCopy[/URL]($aArrayOutput[$iB], $sDirOutput, $FC_OVERWRITE + $FC_CREATEPATH)
                [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']ExitLoop[/URL]
            [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']EndIf[/URL]
         [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Next[/URL]
        [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']Next[/URL]
    [URL='https://www.autoitscript.com/autoit3/docs/keywords.htm']EndIf
EndFunc[/URL]   ;==>_Settings
 

Вложения

  • TexCollector.au3
    7.9 КБ · Просмотры: 0
Верх