[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 【已解决】目录下有多个文件夹,批处理压缩的问题

比如:一个目录下有3个文件夹,我想先压缩第一个文件夹,当第一个文件夹压缩完后,接着压缩第二个文件夹... ...,(把目录下三个文件夹都压缩成三个文件包)这种实现起来难不难?
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

本帖最后由 jyswjjgdwtdtj 于 2023-6-19 09:21 编辑

回复 1# rivenven


    不难
↓只是看起来有点好看 不建议采纳 赶快趋同winzip
  1. '
  2. ' Copyright (c) 2012-2013 WangYe. All rights reserved.
  3. '
  4. ' Author: WangYe
  5. ' Site: http://wangye.org
  6. ' This code is distributed under the BSD license
  7. '
  8. ' For more information please visit
  9. '   http://wangye.org/blog/archives/767/
  10. '
  11. ' References:
  12. ' http://www.techcoil.com/blog/handy-vbscript-functions-for-dealing-with-zip-files-and-folders/
  13. ' http://stackoverflow.com/questions/30211/can-windows-built-in-zip-compression-be-scripted
  14. '
  15. Class ZipCompressor
  16.     Private objFileSystemObject
  17.     Private objShellApplication
  18.     Private objWScriptShell
  19.     Private objScriptingDictionary
  20.     Private objWMIService
  21.     Private COPY_OPTIONS
  22.     Private Sub Class_Initialize()
  23.         Set objFileSystemObject = WSH.CreateObject("Scripting.FileSystemObject")
  24.         Set objShellApplication = WSH.CreateObject("Shell.Application")
  25.         Set objWScriptShell     = WSH.CreateObject("WScript.Shell")
  26.         Set objScriptingDictionary = WSH.CreateObject("Scripting.Dictionary")
  27.         Dim strComputer
  28.         strComputer = "."
  29.         Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  30.         ' COPY_OPTIONS
  31.         '    4   Do not display a progress dialog box.
  32.         '   16   Respond with "Yes to All" for
  33.         '         any dialog box that is displayed.
  34.         '  512   Do not confirm the creation of a new
  35.         '         directory if the operation requires one to be created.
  36.         ' 1024   Do not display a user interface if an error occurs.
  37.         
  38.         COPY_OPTIONS =  4 + 16 + 512 + 1024
  39.     End Sub
  40.     Private Sub Class_Terminate()
  41.         Set objWMIService = Nothing
  42.         objScriptingDictionary.RemoveAll
  43.         Set objScriptingDictionary = Nothing
  44.         Set objWScriptShell     = Nothing
  45.         Set objShellApplication = Nothing
  46.         Set objFileSystemObject = Nothing
  47.     End Sub
  48.     Private Sub makeEmptyZipFile(pathToZipFile)
  49.         Dim file
  50.         Set file = objFileSystemObject.CreateTextFile(pathToZipFile)
  51.         file.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
  52.         file.Close
  53.     End Sub
  54.     Private Function pathToAbsolute(fileName)
  55.         Dim i, file, files
  56.         files = Split(fileName, ";")
  57.         ReDim tmpFiles(UBound(files))
  58.         i = 0
  59.         For Each file in files
  60.             If file<>"" Then
  61.                 file = objWScriptShell.ExpandEnvironmentStrings(file)
  62.                 file = objFileSystemObject.GetAbsolutePathName(file)
  63.                 tmpFiles(i) = file
  64.                 i = i+1
  65.             End If
  66.         Next
  67.         If i-1 > 0 And i-1 < UBound(files) Then ReDim Preserve tmpFiles(i-1)
  68.         pathToAbsolute = Join(tmpFiles, ";")
  69.         Erase tmpFiles
  70.     End Function
  71.     Private Function pathCombine(fileName, nextFileName)
  72.         Dim files, lastIndex
  73.         files = Split(fileName, "\")
  74.         lastIndex = UBound(files)
  75.         If files(lastIndex)<>"" Then
  76.             lastIndex = lastIndex + 1
  77.             ReDim Preserve files(lastIndex)
  78.         End If
  79.         files(lastIndex) = nextFileName
  80.         pathCombine = Join(files, "\")
  81.         Erase files
  82.     End Function
  83.     Private Function pathSplit(fileName)
  84.         Dim fileSplitted(2)
  85.         fileSplitted(0) = objFileSystemObject.GetDriveName(fileName)
  86.         fileSplitted(2) = objFileSystemObject.GetFileName(fileName)
  87.         fileSplitted(1) = Mid(fileName, Len(fileSplitted(0))+1, _
  88.             Len(fileName) - Len(fileSplitted(0)) - Len(fileSplitted(2)))
  89.         pathSplit = fileSplitted
  90.     End Function
  91.     Private Function pathSplitForQuery(fileName)
  92.         Dim fileSplitted
  93.         fileSplitted = pathSplit(fileName)
  94.         fileSplitted(1) = Replace(fileSplitted(1), "\", "\\")
  95.         If Right(fileSplitted(1), 2) <> "\\" Then
  96.             fileSplitted(1) = fileSplitted(1) & "\\"
  97.         End If
  98.         ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa392263(v=vs.85).aspx
  99.         fileSplitted(2) = Replace(fileSplitted(2), "_", "[_]")
  100.         fileSplitted(2) = Replace(fileSplitted(2), "*", "%")
  101.         fileSplitted(2) = Replace(fileSplitted(2), "?", "_")
  102.         pathSplitForQuery = fileSplitted
  103.     End Function
  104.     Private Function buildQuerySQL(fileName)
  105.         Dim fileSplitted, file, ext
  106.         fileSplitted = pathSplitForQuery(fileName)
  107.         Dim lastDotIndex
  108.         file = "%" : ext  = "%"
  109.         If fileSplitted(2)<>"" Then
  110.             lastDotIndex = InStrRev(fileSplitted(2), ".")
  111.             file = fileSplitted(2)
  112.         End If
  113.         If lastDotIndex>0 Then
  114.             ext = Mid(fileSplitted(2), _
  115.                 lastDotIndex+1, Len(fileSplitted(2)) - lastDotIndex)
  116.             file = Left(fileSplitted(2), Len(fileSplitted(2)) - Len(ext) - 1)
  117.         End If
  118.         ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa387236(v=vs.85).aspx
  119.         buildQuerySQL = "SELECT * FROM CIM_DataFile" & _
  120.                         " WHERE Drive='" & fileSplitted(0) & "' AND" & _
  121.                         " (FileName LIKE '" & file & "') AND" & _
  122.                         " (Extension LIKE '" & ext & "') AND" & _
  123.                         " (Path='" & fileSplitted(1) &"')"
  124.     End Function
  125.     Private Function deleteFile(fileName)
  126.         deleteFile = False
  127.         If objFileSystemObject.FileExists(fileName) Then
  128.             objFileSystemObject.DeleteFile fileName
  129.             deleteFile = True
  130.         End If
  131.     End Function
  132.     Private Sub compress_(ByVal fileName, ByRef zipFile)
  133.         Dim objFile, srcFile, srcFiles
  134.         srcFiles = Split(fileName, ";")
  135.         Dim colFiles
  136.         ' http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx
  137.         For Each srcFile In srcFiles
  138.             If objFileSystemObject.FolderExists(srcFile) Then
  139.                 Set objFile = objShellApplication.NameSpace(srcFile)
  140.                 If Not (objFile Is Nothing) Then
  141.                     zipFile.CopyHere objFile.Items, COPY_OPTIONS
  142.                     Do Until objFile.Items.Count <= zipFile.Items.Count
  143.                         WScript.Sleep(200)
  144.                     Loop
  145.                 End If
  146.                 Set objFile = Nothing
  147.             ElseIf objFileSystemObject.FileExists(srcFile) Then
  148.                 zipFile.CopyHere srcFile, COPY_OPTIONS
  149.                 WScript.Sleep(200)
  150.             Else
  151.                 Set colFiles = objWMIService.ExecQuery(buildQuerySQL(srcFile))
  152.                 For Each objFile in colFiles
  153.                     srcFile = objFile.Name
  154.                     zipFile.CopyHere srcFile, COPY_OPTIONS
  155.                     WScript.Sleep(200)
  156.                 Next
  157.                 Set colFiles = Nothing
  158.             End If
  159.         Next
  160.     End Sub
  161.     Public Sub add(fileName)
  162.         objScriptingDictionary.Add pathToAbsolute(fileName), ""
  163.     End Sub
  164.     ' Private Function makeTempDir()
  165.     '    Dim tmpFolder, tmpName
  166.     '    tmpFolder = objFileSystemObject.GetSpecialFolder(2)
  167.     '    tmpName   = objFileSystemObject.GetTempName()
  168.     '    makeTempDir = pathCombine(tmpFolder, tmpName)
  169.     '    objFileSystemObject.CreateFolder makeTempDir
  170.     ' End Function
  171.    
  172.     Public Function compress(srcFileName, desFileName)
  173.         Dim srcAbsFileName, desAbsFileName
  174.         srcAbsFileName = ""
  175.         If srcFileName<>"" Then
  176.             srcAbsFileName = pathToAbsolute(srcFileName)
  177.         End If
  178.         desAbsFileName = pathToAbsolute(desFileName)
  179.         If objFileSystemObject.FolderExists(desAbsFileName) Then
  180.             compress = -1
  181.             Exit Function
  182.         End If
  183.         ' That zip file already exists - deleting it.
  184.         deleteFile desAbsFileName
  185.         makeEmptyZipFile desAbsFileName
  186.         Dim zipFile
  187.         Set zipFile = objShellApplication.NameSpace(desAbsFileName)
  188.         If srcAbsFileName<>"" Then
  189.             compress_ srcAbsFileName, zipFile
  190.         End If
  191.         compress = zipFile.Items.Count
  192.         Dim objKeys, i
  193.         objKeys = objScriptingDictionary.Keys
  194.         For i = 0 To objScriptingDictionary.Count -1
  195.             compress_ objKeys(i), zipFile
  196.         Next
  197.         compress = compress + i
  198.         Set zipFile = Nothing
  199.     End Function
  200.     Public Function decompress(srcFileName, desFileName)
  201.         Dim srcAbsFileName, desAbsFileName
  202.         srcAbsFileName = pathToAbsolute(srcFileName)
  203.         desAbsFileName = pathToAbsolute(desFileName)
  204.         If Not objFileSystemObject.FileExists(srcAbsFileName) Then
  205.             decompress = -1
  206.             Exit Function
  207.         End If
  208.         If Not objFileSystemObject.FolderExists(desAbsFileName) Then
  209.             decompress = -1
  210.             Exit Function
  211.         End If
  212.         Dim zipFile, objFile
  213.         Set zipFile = objShellApplication.NameSpace(srcAbsFileName)
  214.             Set objFile = objShellApplication.NameSpace(desAbsFileName)
  215.                 objFile.CopyHere zipFile.Items, COPY_OPTIONS
  216.                 Do Until zipFile.Items.Count <= objFile.Items.Count
  217.                     WScript.Sleep(200)
  218.                 Loop
  219.                 decompress = objFile.Items.Count
  220.             Set objFile = Nothing
  221.         Set zipFile = Nothing
  222.     End Function
  223. End Class
  224. Set zip = New ZipCompressor
  225. zip.compress "abc\1","1.zip"
  226. zip.compress "abc\2","2.zip"
  227. zip.compress "abc\3","3.zip"
复制代码

TOP

  1. for /d %%i in (*)do "C:\Program Files\7-zip\7z.exe" a -r "%%~fi.7z" "%%~fi\*"
复制代码
1

评分人数

TOP

如果有隐藏文件夹
  1. for /f "delims=" %%i in ('dir/b/ad *')do "C:\Program Files\7-zip\7z.exe" a -r "%%~fi.7z" "%%~fi\*"
复制代码

TOP

回复 2# jyswjjgdwtdtj

你这个还是BAT批处理嘛,怎么像是写代码程序了

TOP

回复 5# rivenven


    不是啊

TOP

返回列表