Batch file to compress subdirectories

China☆狼群 提交于 2019-12-21 05:06:09

问题


I am trying to write a batch script that will run automatically compressing subdirectories using winrar or 7-zip:

Example:

 My Pictures
    Pics1 (Pics1.zip)
        File1.jpg
        File2.jpg
        File3.jpg
    Pics2 (Pics2.zip)
        File4.jpg
        File5.jpg
    Pics3 (Pics3.zip)
        File6.jpg
        File7.jpg
    ...

How do i write script.


回答1:


(1) Using WinRAR:

WinRAR includes two command-line tools, rar.exe and unrar.exe, where rar.exe compresses and unrar.exe uncompresses files.

Both are located in the C:\Program Files\WinRAR folder in the installable version.

Assuming, if there are multiple subfolders under C:\MyPictures and you want each subfolder to get its own .rar file , in the parent folder.

From a batch file, this works for you:

@echo off
setlocal
set zip="C:\Program Files\WinRAR\rar.exe" a -r -u -df
dir C:\MyPictures /ad /s /b > C:\MyPictures\folders.txt
for /f %%f in (C:\MyPictures\folders.txt) do if not exist C:\MyPictures\%%~nf.rar %zip% C:\MyPictures \%%~nf.rar %%f
endlocal
exit

Explanation....

  1. It'll create .rar files of all the folders/subfolders under parent folder C:\MyPictures in the same parent folder.

  2. Then, it'll delete all the original folders/subfolders under parent folder C:\MyPictures and thus you'll be left only with the archives at the same place.

    • “a” command adds to the archive

    • “-r” switch recurses subfolders

    • “-u” switch. Equivalent to the “u” command when combined with the “a” command. Adds new files and updates older versions of the files already in the archive

    • “-df” switch deletes files after they are moved to the archive

If you want to keep the original subfolders, just remove the -df switch.

(2) Using 7-Zip:

7-Zip is a file archiver with a high compression ratio.7z.exe is the command line version of 7-Zip. 7-Zip doesn't uses the system wildcard parser and it doesn't follow the archaic rule by which . means any file. 7-Zip treats . as matching the name of any file that has an extension. To process all files, you must use a * wildcard.

Using 7zip command-line options in a batch file, below works for you:

@echo off
setlocal
for /d %%x in (C:\MyPictures\*.*) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%x.zip" "%%x\"
endlocal
exit

Where

  • -a archive or add

  • -t Type of archive




回答2:


Here's how to do it in 7-zip.

Let's assume your files are in folder C:\Pictures\. Then you can use the following batch command to create multiple archives with the same name as your directories.

FOR /D %%i IN (c:\Pictures\*.*) DO "c:\Program Files\7-Zip\7z.exe" a "%%i.zip" "%%i\"

This will compress each folder in the directory Pictures. Change c:\Pictures to the directory containing your folders. If 7-zip is installed to a different directory, change the "c:\Program Files\7-Zip\7z.exe" to a directory where 7-zip is installed.




回答3:


Thank you for the great script, I just had 3 issues/changes - directory names with space in name were not working, I just wanted to backup level 1 subdirectories and I wanted to pack all the subdirectories in the current directory (it's great for quick backup of all subdirectories separately). Here is the modification if you need something similar:

set zip="C:\Program Files\WinRAR\rar.exe" a -r -u -df
dir /b /o:n /ad > .\folders.txt
for /F "tokens=*" %%A in (.\folders.txt) do if not exist ".\%%~nA.rar" %zip% ".\%%~nA.rar" "%%A"



回答4:


Another approach if you need to recursively zip files in all subdirs in windows is to use gitbash and the approach described here:

  • If you installed git, right click and chose "Git Bash Here"
  • Type find * -type d ! -empty -execdir sh -c 'cd "$1" && 7z a "$1".7z -x!*/ && cd -' sh {} \;

This will:

  • retain folderstructure, place each zip in respective subdir
  • recursively zip all files in dirs (excluding empty dirs)
  • name zips with directory name

You can also exclude specific types with:

find * -type d -execdir sh -c 'cd "$1" && 7z a "$1".7z -x!*/ -x!*.7z -x!*.zip && cd -' sh {} \;



回答5:


Non of the provided solutions worked for me. I have created a script which packs first into 7z with maximum compression and then create rar archive for storage with recovery record (it also deletes the interim 7z archive):

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /R f:\ZA\SQL2005\ZA\ %%A IN (*.*) DO (
    SET "file_without_suffix=%%~dpnA"

    7z.exe a -t7z -m0=lzma2 -mx=9 -mfb=256 -md=1024m -ms=on "!file_without_suffix!.7z" "%%A"
    REM -ep1 do not store path in archive
    rar.exe a -m0 -rr -ep1 "!file_without_suffix!.rar" "!file_without_suffix!.7z"
    DEL /Q /F "!file_without_suffix!.7z"
    SET "file_without_suffix="
)

SETLOCAL DISABLEDELAYEDEXPANSION


来源:https://stackoverflow.com/questions/20779850/batch-file-to-compress-subdirectories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!