为什么我的脚本无法创建 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 文件)具有这些扩展名的所有文件。
为什么它不创建 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
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.
Why does it not create zip files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个建议:
If ($currentExt == $extensions[$e]) then
中添加MsgBox(0, "Zip", "Got here")
。你应该看到你永远无法到达那里。getExt
函数未返回文件扩展名的正确值。更新
您对
getExt
的编辑有点太过分了。试试这个:
更新2
关于您的问题,它不处理第二级以上的文件夹,您的问题是您在递归调用中使用
$rootDirectory
,而您需要使用$dir
。将
archiveDir
函数的最后一部分更改为:Two suggestions:
MsgBox(0, "Zip", "Got here")
inside yourIf ($currentExt == $extensions[$e]) Then
. You should see that you are never getting there.getExt
function is not returning the correct value for the extension of the file.UPDATE
You went a little too far with your edit to
getExt
.Try this:
UPDATE 2
Regarding your problem where it doesn't process folders beyond the 2nd level, your issue is you are using
$rootDirectory
in your recursive call where you need to use$dir
.Change the last part of your
archiveDir
function to this:我尝试按原样运行你的代码,果然失败了。然后我在“If @error”块中添加一个,
看看是否可以找出失败的原因。结果是 @error 返回为 6。查看文档,它说错误代码 6 意味着在数组中找不到搜索的值。然后 $currentExt 告诉我它的值被设置为“.asp”。
找不到的原因是数组中的扩展名中没有句点。如果您仔细观察 getExt() 函数,在将 1 添加到 $position 值之前...现在您要从该值中减去 1...这是 StringTrimLeft() 工作原理的说明...
所以解决方案是添加“.”在数组中所有扩展名的前面,或者更改 getExt() 函数:
您还可以研究另一种选择,那就是使用 File.au3 中找到的 _PathSplit() 函数,但由于您的脚本是如此此时即将开始工作,我不会担心它,但也许将来您可以使用它。
最后一点...在我更改 getExt() 以删除“.”之后,您的脚本运行得很好。
I tried running your code as is, and sure enough, it failed. I then put a
in the "If @error" block to see if I could find out why it was failing. The result was the @error came back as 6. Looking into the documentation, it says that an error code of 6 means that the value searched for was not found in the array. And then the $currentExt told me it's value was set to ".asp".
The reason it could not be found was because there are no periods in the extension names in the array. If you look closer at the getExt() function, before you were adding 1 to the $position value... and now you are subtracting 1 from the value... Here's an illustration of how StringTrimLeft() works...
So the solution is to either add "." in front of all of the extensions in your array, or change your getExt() function:
There is one more option you could look into, and that is using the _PathSplit() function found in File.au3, but since your script is so close to working at this point, I wouldn't worry about it, but maybe in the future you could use it instead.
And one last note... After I changed getExt() to drop the ".", your script ran great.
在函数 StringTrimLeft("string", count) 中,count 是要修剪的字符数。
所以...
In the function StringTrimLeft("string", count), count is the number of characters to trim.
so...