0
#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 ファイルが作成されないのはなぜですか?

4

3 に答える 3

2

関数 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
于 2010-05-19T20:32:03.090 に答える
1

2 つの提案:

  1. の中に追加MsgBox(0, "Zip", "Got here")しますIf ($currentExt == $extensions[$e]) Then。そこに到達することは決してないことがわかるはずです。
  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
于 2010-05-19T19:41:52.233 に答える
0

私はあなたのコードをそのまま実行しようとしましたが、確かに失敗しました。私はそれから

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

「If @error」ブロックで、失敗した理由を見つけられるかどうかを確認してください。その結果、@error は 6 として返されました。ドキュメントを調べると、エラー コード 6 は、検索された値が配列に見つからなかったことを意味すると書かれています。そして、$currentExt は、値が「.asp」に設定されていることを教えてくれました。

見つからなかった理由は、配列内の拡張子名にピリオドがないためです。getExt() 関数をよく見ると、$position 値に 1 を加算する前に...そして今は値から 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

調べることができるオプションがもう 1 つあります。それは、File.au3 にある _PathSplit() 関数を使用することですが、この時点でスクリプトはほとんど機能していないため、心配する必要はありませんが、将来、代わりに使用できます。

最後に 1 つ注意してください... getExt() を変更して "." を削除した後、スクリプトは問題なく実行されました。

于 2010-05-23T06:45:22.503 に答える