スクリプトがzipファイルを作成できないのはなぜですか?

StackOverflow https://stackoverflow.com/questions/2867689

  •  01-10-2019
  •  | 
  •  

質問

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

役に立ちましたか?

解決

関数StringTrimLeft( "String"、count)では、カウントはトリミングする文字の数です。

$filename = "filename.zip"

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

それで...

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

他のヒント

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

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

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()関数を使用していますが、スクリプトはこの時点で動作しているため、心配することはありませんが、多分将来、代わりに使用できます。

そして最後のメモ... getext()を変更して「。」をドロップした後、スクリプトは素晴らしかったです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top