Pourquoi mon script ne parviennent pas à créer des fichiers zip?
Question
#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
J'ai une liste des extensions de fichiers. Ce script doit passer par un répertoire (et les sous-répertoires), zip (fichiers zip séparés pour chaque extension) tous les fichiers avec ces extensions.
Pourquoi il ne crée pas des fichiers zip?
La solution
Dans la fonction StringTrimLeft ( "string", nombre), le nombre est le nombre de caractères à couper.
$filename = "filename.zip"
$pos = StringInStr($filename, ".") ; $pos will be equal to 9
...
$retval = StringTrimLeft($filename, $pos + 1); this will remove 10 characters = ip
Autres conseils
Deux suggestions:
- Ajouter
MsgBox(0, "Zip", "Got here")
dans votreIf ($currentExt == $extensions[$e]) Then
. Vous devriez voir que vous n'êtes jamais y arriver. - associés à ce qui précède, votre fonction
getExt
ne retourne pas la valeur correcte pour l'extension du fichier.
UPDATE
Vous êtes allé un peu trop loin avec votre édition pour getExt
.
Essayez ceci:
Func getExt($filename)
$pos = StringInStr($filename, ".")
$retval = StringTrimLeft($filename, $pos)
Return $retval
EndFunc
MISE À JOUR 2
En ce qui concerne votre problème où il ne traite pas les dossiers au-delà du niveau 2, votre problème est que vous utilisez $rootDirectory
dans votre appel récursif où vous devez utiliser $dir
.
Modifier la dernière partie de votre fonction archiveDir
à ceci:
; 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
I Tried en cours d'exécution du code tel qu'il est, et bien sûr, il a échoué. Je mets alors un
MsgBox(0, "error", @error & " " & $currentExt)
dans le « Si @error » bloc pour voir si je pouvais savoir pourquoi il a échoué. Le résultat a été le @error est revenu comme 6. En regardant dans la documentation, il est dit qu'un code d'erreur de 6 signifie que la valeur recherchée n'a pas été trouvé dans le tableau. Et puis la currentExt $ m'a dit Apprécions il a été réglé sur « .asp ».
La raison pour laquelle il n'a pu être trouvée était parce qu'il n'y a pas de périodes dans les noms d'extension du réseau. Si vous regardez de plus près la fonction getExt (), avant ajoutiez 1 à la valeur de $ position ... et maintenant vous soustrayez 1 de la valeur ... Voici une illustration de la façon dont StringTrimLeft () fonctionne ...
$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!
Ainsi, la solution consiste à ajouter « » devant toutes les extensions dans votre tableau, ou modifier votre fonction getExt ():
Func getExt($filename)
$pos = StringInStr($filename, ".")
$retval = StringTrimLeft($filename, $pos)
Return $retval
EndFunc
Il y a une autre option que vous pouvez regarder dans, et qui est d'utiliser la fonction _PathSplit () trouvée dans File.au3, mais depuis votre script est si proche de travailler à ce moment-là, je ne vous inquiétez pas, mais peut-être dans l'avenir, vous pouvez l'utiliser à la place.
Une dernière note ... Après avoir changé getExt () pour laisser tomber le "", votre script a couru beaucoup.