لماذا يفشل البرنامج النصي في إنشاء ملفات 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 UP (ملفات ZIP منفصلة لكل امتداد) جميع الملفات مع تلك الامتدادات.

لماذا لا تنشئ ملفات 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

نصائح أخرى

اقتراحان:

  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

فيما يتعلق بمشكلتك حيث لا تقوم بمعالجة المجلدات التي تتجاوز المستوى الثاني ، فإن مشكلتك هي أنك تستخدم $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

حاولت تشغيل الكود الخاص بك كما هو ، وبالتأكيد ، فشلت. ثم وضعت ملف

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

في كتلة "if error" لمعرفة ما إذا كان بإمكاني معرفة سبب فشلها. وكانت النتيجة هي أن error عاد إلى 6. النظر في الوثائق ، وتقول أن رمز الخطأ 6 يعني أن القيمة التي تم البحث عنها لم يتم العثور عليها في الصفيف. ثم أخبرني Currentext $ أنه تم تعيين قيمة على ".asp".

السبب في أنه لا يمكن العثور عليه هو أنه لا توجد فترات في أسماء التمديد في الصفيف. إذا نظرت عن كثب إلى وظيفة getext () ، قبل أن تضيف 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

هناك خيار واحد آخر يمكنك النظر فيه ، وهو يستخدم وظيفة _pathsplit () الموجودة في file.au3 ، ولكن بما أن البرنامج النصي الخاص بك قريب جدًا من العمل في هذه المرحلة ، فلن أقلق بشأن ذلك ، ولكن ربما في في المستقبل يمكنك استخدامه بدلاً من ذلك.

وملاحظة أخيرة ... بعد أن قمت بتغيير getext () لإسقاط "." ، كان السيناريو الخاص بك رائع.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top