Dateien suchen und verändern

  • Hallo,

    Ich stehe vor folgendem Problem.
    Ich habe einen Ordner XY in dem sind weitere unterordner Ordner_ab_1, Ordner_ef_2, Ordner_er_45, ...
    In jedem Ordner sind verschied Dateien und ein .jpg.

    Ich will jetzt das JPG aus jedem Ordner wie der Ordner nummerieren also nur 1.jpg, 2.jpg, 45.jpg, ... und danach in einen Sammelordner verschieben.

    Wer kann mir dazu hilfe anbieten? Ich habe schon einiges versuch wie z.B.:

    Wäre schön wenn mir jemand eine funktionierende methode zum finden der jpg's nennen könnte.

    Einmal editiert, zuletzt von Chickmc (30. September 2009 um 14:09)

  • Hi,

    vielleicht ist das ein Ansatz ...

    Spoiler anzeigen
    [autoit]

    $sFileJPG = ""

    [/autoit] [autoit][/autoit] [autoit]

    $sPath = "D:\ToSrv\"

    [/autoit] [autoit][/autoit] [autoit]

    $hSearch = FileFindFirstFile($sPath & "*.*")

    [/autoit] [autoit][/autoit] [autoit]

    If ($hSearch = -1) Then
    Exit
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    While (TRUE)

    [/autoit] [autoit][/autoit] [autoit]

    $sFile = FileFindNextFile($hSearch)

    [/autoit] [autoit][/autoit] [autoit]

    If @error Then ExitLoop

    [/autoit] [autoit][/autoit] [autoit]

    $sAttrib = FileGetAttrib ($sPath & $sFile)

    [/autoit] [autoit][/autoit] [autoit]

    ; Prüfen, ob sFile ein Verzeichnis ist.
    If (StringInStr ($sAttrib, 'D')) Then

    [/autoit] [autoit][/autoit] [autoit]

    ; Bilddatei im Subverzeichnis suchen.
    $sFileJPG = FindJPG ($sPath & $sFile)

    [/autoit] [autoit][/autoit] [autoit]

    MsgBox ($MB_ICONINFORMATION, "JPEG", $sFileJPG)

    [/autoit] [autoit][/autoit] [autoit]

    ; TODO: Umbenennen und verschieben

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func FindJPG ($path)

    [/autoit] [autoit][/autoit] [autoit]

    Local $hSearch
    Local $sFile

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $hSearch = FileFindFirstFile($path & "\*.JPG")

    [/autoit] [autoit][/autoit] [autoit]

    If ($hSearch = -1) Then
    Exit
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    While (TRUE)

    [/autoit] [autoit][/autoit] [autoit]

    $sFile = FileFindNextFile($hSearch)

    [/autoit] [autoit][/autoit] [autoit]

    If @error Then ExitLoop

    [/autoit] [autoit][/autoit] [autoit]

    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Return $sFile

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit]

    Ansonsten sieh dir mal die _File... Funktionen in den UDFs an, z.B. _FileListToArray.


    Gruß
    Greenhorn


  • Moin,

    so vielleicht:

    [spoiler]

    [autoit]

    $spath = "D:\ToSrv"
    $sRequest = "*.jpg"
    $sSammel = "E:\Sammel"
    local $Datei

    [/autoit][autoit][/autoit][autoit]

    ;Finde alle *.jpg in allen Unterverzeichnissen
    $aResults = _FileSearch($sPath, $sRequest, 1, 0)
    If @error Then
    MsgBox(48, "Achtung!", "Keine Dateien gefunden!")
    Else
    ;Schleife über Ergebnisse
    For $i = 1 to UBound ($aResults) - 1
    ;Splitte Absoluten Pfad
    $temp = StringSplit ($aResults [$i], "\")
    ;Splitte letztes Unterverzeichnis
    $temp1 = StringSplit ($temp [UBound ($temp) - 2], "_")
    ;Setze neuen Dateinamen zusammen
    For $k = 1 To UBound ($temp) - 2
    $Datei &= $temp [$k] & "\"
    Next
    $Datei = $Datei & $temp1 [UBound ($temp1) - 1] & ".jpg"
    ;Benenne Datei um
    FileMove ($aResults [$i], $Datei)
    ;Hier nun FileMove für Sammelverzeichnis, könnte auch mit filemove gemacht werden....
    FileCopy ($Datei, $sSammel , 8)
    FileDelete ($Datei)
    Next
    EndIf
    ;================================================================================
    ;Flag = 1 search with recurse
    ;Flag <> 1 search without recurse
    ;
    ;$iRet = 1 return parent directory of the file path
    ;$iRet <> 1 return full file path
    ;
    ;$iStatus_CtrlID - If this is an id of the label control, the function will set the current status of the search
    ;
    ;On Failure set @error as following:
    ; 1 - $sPath is not a dir or it not exists (in this case returned -1).
    ; 2 - $sPath is empty dir.
    ; 3 - No files found in $sPath dir.
    ;
    ;On Seccess return an array with found files.
    ;================================================================================
    Func _FileSearch($sPath, $sMask, $iFlag=0, $iRet=0, $iStatus_CtrlID=0)
    If Not StringInStr(FileGetAttrib($sPath), "D") Then Return SetError(1, 0, -1)

    [/autoit][autoit][/autoit][autoit]

    Local $aRetPathArr[1], $sFindNextFile, $sCurrentPath, $aSubDirFindArr
    If StringInStr($sMask, "*") Then $sMask = StringReplace($sMask, "*.", "")

    [/autoit][autoit][/autoit][autoit]

    $sPath = StringRegExpReplace($sPath, '\\+ *$', '\')

    [/autoit][autoit][/autoit][autoit]

    Local $hSearch = FileFindFirstFile($sPath & "\*.*")
    If @error = 1 Then Return SetError(2, 0, 0)
    If $hSearch = -1 Then Return SetError(3, 0, 0)

    [/autoit][autoit][/autoit][autoit]

    While 1
    $sFindNextFile = FileFindNextFile($hSearch)
    If @error = 1 Then ExitLoop

    [/autoit][autoit][/autoit][autoit]

    $sCurrentPath = $sPath & "\" & $sFindNextFile

    [/autoit][autoit][/autoit][autoit]

    If $iFlag = 1 And StringInStr(FileGetAttrib($sCurrentPath), "D") Then
    $aSubDirFindArr = _FileSearch($sCurrentPath, $sMask, $iFlag, 0, $iStatus_CtrlID)
    If @error Then ContinueLoop

    [/autoit][autoit][/autoit][autoit]

    For $i = 1 To $aSubDirFindArr[0]
    $aRetPathArr[0] += 1
    ReDim $aRetPathArr[$aRetPathArr[0]+1]

    [/autoit][autoit][/autoit][autoit]

    $aRetPathArr[$aRetPathArr[0]] = $aSubDirFindArr[$i]
    If $iRet = 1 Then _
    $aRetPathArr[$aRetPathArr[0]] = StringRegExpReplace($aRetPathArr[$aRetPathArr[0]], "\\[^\\]*$", "")
    Next
    Else
    If $sMask = "*" Or $sFindNextFile = $sMask Or StringRegExpReplace($sCurrentPath, '^.*\.', '') = $sMask Then
    $aRetPathArr[0] += 1
    ReDim $aRetPathArr[$aRetPathArr[0]+1]

    [/autoit][autoit][/autoit][autoit]

    $aRetPathArr[$aRetPathArr[0]] = $sCurrentPath
    If $iRet = 1 Then $aRetPathArr[$aRetPathArr[0]] = $sPath
    EndIf
    EndIf
    WEnd

    [/autoit][autoit][/autoit][autoit]

    FileClose($hSearch)

    [/autoit][autoit][/autoit][autoit]

    If $aRetPathArr[0] = 0 Then Return SetError(3, 0, 0)

    [/autoit][autoit][/autoit][autoit]

    Return $aRetPathArr
    EndFunc

    [/autoit]

    (/spoiler]

    ;-))
    Stefan

    • Offizieller Beitrag

    Rekursive Dateisuche ist das Stichwort. ;)

    Ich hab es mal so genmacht, dass die Namen Vornullen bekommen, dann ist der Dateiname gleich lang - sieht besser aus.
    So gehts:

    Spoiler anzeigen
    [autoit]

    Local $startPath = 'Dein Start-Pfad'
    Local $ZielPfad = 'Dein Pfad zum Abspeichern\' ; Backslash mit verwenden!
    Local $ext = 'jpg'

    [/autoit] [autoit][/autoit] [autoit]

    Local $arJPGFiles = _GetFilesFolder_Rekursiv($startPath, $ext, 0)
    Local $len = StringLen(String($arJPGFiles[0]))
    Local $nul = ''
    If $len > 1 Then
    For $i = 1 To $len -1
    $nul &= '0'
    Next
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To $arJPGFiles[0]
    FileMove($arJPGFiles[$i], $ZielPfad & StringRight($nul & $i, $len) & '.jpg')
    If @error Then
    MsgBox(262160,"ACHTUNG","Die Datei" & @CRLF & $arJPGFiles[$i] & @CRLF & _
    "konnte nicht verschoben werden, da eine Zieldatei mit dem Namen" & @CRLF & _
    $ZielPfad & StringRight($nul & $i, $len) & '.jpg' & @CRLF & "bereits existiert!")
    EndIf
    Next
    MsgBox(262176,"FERTIG","Verschieben und Umbenennen der Dateien beendet.")

    [/autoit] [autoit][/autoit] [autoit]

    ;==================================================================================================
    ; Function Name: _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
    ; Description: Recursive listing of files and/or folders
    ; Parameter(s): $sPath Basicpath of listing ('.' -current path, '..' -parent path)
    ; $sExt Extension for file selection '*' or -1 for all (Default)
    ; $iDir -1 Files+Folder(Default), 0 only Files, 1 only Folder
    ; optional: $iRetType 0 for Array, 1 for String as Return
    ; optional: $sDelim Delimiter for string return
    ; 0 -@CRLF (Default) 1 -@CR 2 -@LF 3 -';' 4 -'|'
    ; Return Value(s): Array (Default) or string with found pathes of files and/or folder
    ; Array[0] includes count of found files/folder
    ; Author(s): BugFix ([email='bugfix@autoit.de'][/email])
    ;==================================================================================================
    Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
    Case '1'
    $sDelim = @CR
    Case '2'
    $sDelim = @LF
    Case '3'
    $sDelim = ';'
    Case '4'
    $sDelim = '|'
    Case Else
    $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
    Local $aOut
    $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
    If $aOut[1] = '' Then
    ReDim $aOut[1]
    $aOut[0] = 0
    EndIf
    Return $aOut
    Else
    Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
    For $file In $Folder.Files
    If $Ext <> '*' Then
    If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
    $strFiles &= $file.Path & $Delim
    Else
    $strFiles &= $file.Path & $Delim
    EndIf
    Next
    EndIf
    For $Subfolder In $Folder.SubFolders
    If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
    _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
    EndFunc

    [/autoit]