【问题标题】:Why does my script fail to create zip files?为什么我的脚本无法创建 zip 文件?
【发布时间】:2011-02-21 12:00:22
【问题描述】:
#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

我有一个文件扩展名列表。这个脚本应该通过一个目录(和子目录),压缩(为每个扩展名单独的压缩文件)所有文件这些扩展名。

为什么它不创建 zip 文件?

【问题讨论】:

    标签: zip autoit


    【解决方案1】:

    两个建议:

    1. 在您的If ($currentExt == $extensions[$e]) Then 中添加MsgBox(0, "Zip", "Got here")。你应该看到你永远不会到达那里。
    2. 与上述相关,您的 getExt 函数未返回文件扩展名的正确值。

    更新

    你对getExt的编辑有点过火了。

    试试这个:

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


    更新 2

    关于您的问题,它不处理超过 2 级的文件夹,您的问题是您在递归调用中使用 $rootDirectory,而您需要使用 $dir。

    将archiveDir 函数的最后一部分更改为:

      ; 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
    

    【讨论】:

    • 我现在看到了,但是我不确定为什么该方法没有返回正确的值...
    【解决方案2】:

    我尝试按原样运行您的代码,果然,它失败了。然后我放了一个

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

    在“If @error”块中,看看我是否能找出它失败的原因。结果是@error 返回为 6。查看文档,它说错误代码 6 表示在数组中找不到搜索的值。然后 $currentExt 告诉我它的值设置为“.asp”。

    找不到的原因是数组中的扩展名中没有句点。如果您仔细查看 getExt() 函数,在您将 1 加到 $position 值之前...现在您从值中减去 1...这是 StringTrimLeft() 工作原理的说明...

    $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!
    

    所以解决方案是添加“。”在数组中的所有扩展之前,或更改您的 getExt() 函数:

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

    您还可以考虑另一种选择,即使用 File.au3 中的 _PathSplit() 函数,但由于您的脚本此时已接近工作,我不会担心,但也许将来你可以使用它。

    最后一点...在我更改 getExt() 以删除“.”后,您的脚本运行良好。

    【讨论】:

    • 它在 3 级子目录中运行良好吗?这就是我遇到问题的地方......
    • 在进行了 aphoria 在他的第二次更新中提到的更改后,它按预期工作。
    【解决方案3】:

    在函数StringTrimLeft("string", count)中,count是要修剪的字符数。

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

    所以...

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

    【讨论】:

    • If ($goFiles == True) Then $emax = UBound($extensions) $fmax = UBound($allFiles) For $e = 0 to $emax - 1 For $f = 0 to $ fmax - 1 $currentExt = getExt($allFiles[$f]) MsgBox(0, "$currentExt 的值", $currentExt) If ($currentExt == $extensions[$e]) Then MsgBox(0, "Zip" , "Got here") $zip = _Zip_Create($dir & "\" & $currentExt & "_zip.zip") _Zip_AddFile($zip, $allFiles[$f]) EndIf Next Next EndIf 为什么输出的是".txt 。文本文件”。我修复了方法,但这已成为输出。这就像在欺骗扩展程序
    • 我要出门了,所以我没有时间看这个......但我想告诉你一件事,你可以取消你的第二个 For 循环通过使用 _ArraySearch()。 $position = _ArraySearch($extensions, $currentExt) 如果它不在 Error 中,那么你知道你的数组中有一个扩展。
    • 编辑您的问题并向我们展示您所做的代码更改。
    猜你喜欢
    • 1970-01-01
    • 2021-01-21
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多