为什么我的脚本无法创建zip文件? [英] Why does my script fail to create zip files?

查看:226
本文介绍了为什么我的脚本无法创建zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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文件).

I have a list of file extensions. This script should go through a directory (and subdirectories), zip up (separate zip files for each extension) all files with those extensions.

为什么不创建zip文件?

Why does it not create zip files?

推荐答案

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

In the function StringTrimLeft("string", count), count is the number of characters to trim.

$filename = "filename.zip"

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

所以...

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

这篇关于为什么我的脚本无法创建zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆