Batch file script to zip files

后端 未结 7 1429
夕颜
夕颜 2020-12-09 03:22

i have a folder structure in this pattern. I\'ve just shown two sub directories and 2 files in each but generally I have n number of subdirectories at a single level and sin

相关标签:
7条回答
  • 2020-12-09 04:04
    for /d %%a in (*) do (ECHO zip -r -p "%%~na.zip" ".\%%a\*")
    

    should work from within a batch.

    Note that I've included an ECHO to simply SHOW the command that is proposed. You'd need to remove the ECHO keywor to EXECUTE the commands.

    0 讨论(0)
  • 2020-12-09 04:05

    No external dependency on 7zip or ZIP - create a vbs script and execute:

        @ECHO Zipping
        mkdir %TEMPDIR%
        xcopy /y /s %FILETOZIP% %TEMPDIR%
        echo Set objArgs = WScript.Arguments > _zipIt.vbs
        echo InputFolder = objArgs(0) >> _zipIt.vbs
        echo ZipFile = objArgs(1) >> _zipIt.vbs
        echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
        echo Set objShell = CreateObject("Shell.Application") >> _zipIt.vbs
        echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
        echo objShell.NameSpace(ZipFile).CopyHere(source) >> _zipIt.vbs
        @ECHO *******************************************
        @ECHO Zipping, please wait..
        echo wScript.Sleep 12000 >> _zipIt.vbs
        CScript  _zipIt.vbs  %TEMPDIR%  %OUTPUTZIP%
        del _zipIt.vbs
        rmdir /s /q  %TEMPDIR%
    
        @ECHO *******************************************
        @ECHO      ZIP Completed
    
    0 讨论(0)
  • 2020-12-09 04:16

    You're near :)

    First, the batch (%%variable) and Windows CMD (%variable) uses different variable naming. Second, i dont figure out how do you use zip from CMD. This is from Linux users i think. Use built-in zip manipulation is not like easy on Win, and even harder with batch scripting.

    But you're lucky anyway. I got (extracted to target folder) zip.exe and cygwin1.dll from the cygwin package (3mb filesize both together) and start play with it right now.

    Of course, i use CMD for better/faster testing instead batch. Only remember modify the %varname to %%varname before blame me :P

    for /d %d in (*) do zip -r %d %d

    Explanation: for /d ... that matches any folder inside. Only folder ignoring files. (use for /f to filesmatch) for /d %d in ... the %d tells cmd wich name do you wanna assign to your variable. I put d to match widh d (directory meaning). for /d %d in (*) ... Very important. That suposses that I CD to desired folder, or run from. (*) this will mean all on THIS dir, because we use /d the files are not processed so no need to set a pattern, even if you can get only some folders if you need. You can use absolute paths. Not sure about issues with relatives from batch. for /d %d in (*) do zip -r ... Do ZIP is obvious. (exec zip itself and see the help display to use your custom rules). r- is for recursive, so anyting will be added. for /d %d in (*) do zip -r %d %d The first %d is the zip name. You can try with myzip.zip, but if will fail because if you have 2 or more folders the second cannot gave the name of the first and will not try to overwrite without more params. So, we pass %d to both, wich is the current for iteration folder name zipped into a file with the folder name. Is not neccesary to append ".zip" to name.

    Is pretty short than i expected when start to play with.

    0 讨论(0)
  • 2020-12-09 04:23

    I know its too late but if you still wanna try

    for /d %%X in (*) do (for /d %%a in (%%X) do ( "C:\Program Files\7-Zip\7z.exe" a -tzip "%%X.zip" ".\%%a\" ))
    

    here * is the current folder. for more options try this link

    0 讨论(0)
  • 2020-12-09 04:24

    I like PodTech.io's answer to achieve this without additional tools. For me, it did not run out of the box, so I had to slightly change it. I am not sure if the command wScript.Sleep 12000 (12 sec delay) in the original script is required or not, so I kept it.

    Here's the modified script Zip.cmd based on his answer, which works fine on my end:

    @echo off   
    if "%1"=="" goto end
    
    setlocal
    set TEMPDIR=%TEMP%\ZIP
    set FILETOZIP=%1
    set OUTPUTZIP=%2.zip
    if "%2"=="" set OUTPUTZIP=%1.zip
    
    :: preparing VBS script
    echo Set objArgs = WScript.Arguments > _zipIt.vbs
    echo InputFolder = objArgs(0) >> _zipIt.vbs
    echo ZipFile = objArgs(1) >> _zipIt.vbs
    echo Set fso = WScript.CreateObject("Scripting.FileSystemObject") >> _zipIt.vbs
    echo Set objZipFile = fso.CreateTextFile(ZipFile, True) >> _zipIt.vbs
    echo objZipFile.Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
    echo objZipFile.Close >> _zipIt.vbs
    echo Set objShell = WScript.CreateObject("Shell.Application") >> _zipIt.vbs
    echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
    echo Set objZip = objShell.NameSpace(fso.GetAbsolutePathName(ZipFile)) >> _zipIt.vbs
    echo if not (objZip is nothing) then  >> _zipIt.vbs
    echo    objZip.CopyHere(source) >> _zipIt.vbs
    echo    wScript.Sleep 12000 >> _zipIt.vbs
    echo end if >> _zipIt.vbs
    
    @ECHO Zipping, please wait...
    mkdir %TEMPDIR%
    xcopy /y /s %FILETOZIP% %TEMPDIR%
    WScript _zipIt.vbs  %TEMPDIR%  %OUTPUTZIP%
    del _zipIt.vbs
    rmdir /s /q  %TEMPDIR%
    
    @ECHO ZIP Completed.
    :end
    

    Usage:

    • One parameter (no wildcards allowed here):

      Zip FileToZip.txt

      will create FileToZip.txt.zip in the same folder containing the zipped file FileToZip.txt.

    • Two parameters (optionally with wildcards for the first parameter), e.g.

      Zip *.cmd Scripts

      creates Scripts.zip in the same folder containing all matching *.cmd files.


    Note: If you want to debug the VBS script, check out this hint, it describes how to activate the debugger to go through it step by step.

    0 讨论(0)
  • 2020-12-09 04:25

    This is the correct syntax for archiving individual; folders in a batch as individual zipped files...

    for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\*"
    
    0 讨论(0)
提交回复
热议问题