Domanda

#include <File.au3>
#include <Zip.au3>
#include <Array.au3>

; bad file extensions
Local $extData = "ade|adp|app|asa|ashx|asp|bas|bat|cdx|cer|chm|class|cmd|com|cpl|crt|csh|der|exe|fxp|gadget|hlp|hta|htr|htw|ida|idc|idq|ins|isp|its|jse|ksh|lnk|mad|maf|mag|mam|maq|mar|mas|mat|mau|mav|maw|mda|mdb|mde|mdt|mdw|mdz|msc|msh|msh1|msh1xml|msh2|msh2xml|mshxml|msi|msp|mst|ops|pcd|pif|prf|prg|printer|pst|reg|rem|scf|scr|sct|shb|shs|shtm|shtml|soap|stm|url|vb|vbe|vbs|ws|wsc|wsf|wsh"
Local $extensions = StringSplit($extData, "|")

; What is the root directory?
$rootDirectory = InputBox("Root Directory", "Please enter the root directory...")

archiveDir($rootDirectory)

Func archiveDir($dir)

    $goDirs = True
    $goFiles = True
    ; Get all the files under the current dir
    $allOfDir = _FileListToArray($dir)
    $tmax = UBound($allOfDir)

    For $t = 0 To $tmax - 1
    Next

    Local $countDirs = 0
    Local $countFiles = 0

    $imax = UBound($allOfDir)
    For $i = 0 To $imax - 1
        If StringInStr(FileGetAttrib($dir & "\" & $allOfDir[$i]), "D") Then
            $countDirs = $countDirs + 1
        ElseIf StringInStr(($allOfDir[$i]), ".") Then
            $countFiles = $countFiles + 1
        EndIf
    Next

    If ($countDirs > 0) Then
        Local $allDirs[$countDirs]
        $goDirs = True
    Else
        $goDirs = False
    EndIf

    If ($countFiles > 0) Then
        Local $allFiles[$countFiles]
        $goFiles = True
    Else
        $goFiles = False
    EndIf

    $dirCount = 0
    $fileCount = 0

    For $i = 0 To $imax - 1
        If (StringInStr(FileGetAttrib($dir & "\" & $allOfDir[$i]), "D")) And ($goDirs == True) Then
            $allDirs[$dirCount] = $allOfDir[$i]
            $dirCount = $dirCount + 1
        ElseIf (StringInStr(($allOfDir[$i]), ".")) And ($goFiles == True) Then
            $allFiles[$fileCount] = $allOfDir[$i]
            $fileCount = $fileCount + 1
        EndIf
    Next

    ; Zip them if need be in current spot using 'ext_zip.zip' as file name, loop through each file ext.
    If ($goFiles == True) Then
        $fmax = UBound($allFiles)
        For $f = 0 To $fmax - 1
            $currentExt = getExt($allFiles[$f])
            $position = _ArraySearch($extensions, $currentExt)
            If @error Then
                MsgBox(0, "Not Found", "Not Found")
            Else
                $zip = _Zip_Create($dir & "\" & $currentExt & "_zip.zip")
                _Zip_AddFile($zip, $dir & "\" & $allFiles[$f])
            EndIf
        Next
    EndIf

    ; Get all dirs under current DirCopy
    ; For each dir, recursive call from step 2
    If ($goDirs == True) Then
        $dmax = UBound($allDirs)
        $rootDirectory = $rootDirectory & "\"
        For $d = 0 To $dmax - 1
            archiveDir($rootDirectory & $allDirs[$d])
        Next
    EndIf

EndFunc

Func getExt($filename)

    $pos = StringInStr($filename, ".")
    $retval = StringTrimLeft($filename, $pos - 1)
    Return $retval

EndFunc

Ho un elenco di estensioni di file. Questo script dovrebbe passare attraverso una directory (e sottodirectory), comprimere (file zip separati per ogni estensione) di tutti i file con tali estensioni.

Perché non creare zip file?

È stato utile?

Soluzione

Nella funzione StringTrimLeft ( "stringa", count), il conteggio è il numero di caratteri da tagliare.

$filename = "filename.zip"

$pos = StringInStr($filename, ".") ; $pos will be equal to 9

così ...

$retval = StringTrimLeft($filename, $pos + 1); this will remove 10 characters = ip

Altri suggerimenti

Due suggerimenti:

  1. Add MsgBox(0, "Zip", "Got here") all'interno del vostro If ($currentExt == $extensions[$e]) Then. Dovreste vedere che non siete mai arrivarci.
  2. In relazione alla precedente, la funzione getExt non restituisce il valore corretto per l'estensione del file.

UPDATE

È andato un po 'troppo lontano con la tua modifica getExt.

Prova questo:

Func getExt($filename)
  $pos = StringInStr($filename, ".")
  $retval = StringTrimLeft($filename, $pos)
  Return $retval
EndFunc  

UPDATE 2

Per quanto riguarda il problema dove non elabora cartelle oltre il 2 ° livello, il problema è che si sta utilizzando $rootDirectory nella chiamata ricorsiva in cui è necessario l'uso $dir.

Cambia l'ultima parte della funzione archiveDir a questo:

  ; For each dir, recursive call from step 2
  If ($goDirs == True) Then
    $dmax = UBound($allDirs)
    $dir = $dir & "\"
    For $d = 0 to $dmax - 1
      archiveDir($dir & $allDirs[$d])
    Next
  EndIf

Ho provato a correre il codice come è, e abbastanza sicuro, non è riuscito. Ho poi messo a

MsgBox(0, "error", @error & " " & $currentExt)

nella sezione "Se @Error" Blocco per vedere se riuscivo a trovare il motivo per cui si stava fallendo. Il risultato è stato il @Error è tornato come 6. Guardando la documentazione, si dice che un codice di errore di 6 significa che il valore cercato non è stato trovato nella matrice. E poi il $ currentExt mi ha detto che di valore è stato impostato su "asp".

Il motivo per cui non è stato trovato era perché non ci sono periodi nei nomi di estensione della matrice. Se si guarda più da vicino la funzione getExt (), prima che tu fossi aggiungendo 1 al valore di posizione $ ... e ora si sta sottraendo 1 al valore di ... Ecco un esempio di come funziona ...

$filename = "filename.txt"

$pos = StringInStr($filename, ".") ; $pos will be equal to 9

$retval = StringTrimLeft($filename, $pos + 1); this will remove 10 characters = xt, that's too much.
$retval = StringTrimLeft($filename, $pos - 1); this will remove 8 characters = .txt, that's not enough.
$retval = StringTrimLeft($filename, $pos); this will remove 9 characters = txt, that's juuuuuuust right!

Quindi la soluzione è quella di uno add "" di fronte a tutte le estensioni nel vostro array o modificare la funzione getExt ():

Func getExt($filename)
    $pos = StringInStr($filename, ".")
    $retval = StringTrimLeft($filename, $pos)
    Return $retval
EndFunc

C'è un'opzione più si potrebbe guardare in, e che sta utilizzando la funzione _PathSplit () in File.au3, ma dal momento che lo script è così vicino a lavorare, a questo punto, non mi preoccuperei a questo proposito, ma magari in futuro si potrebbe utilizzare invece.

E un ultima nota ... Dopo ho cambiato getExt () per far cadere il "", lo script ha grande.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top