windows 用于压缩子目录的批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20779850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch file to compress subdirectories
提问by Т?мэнзул Батжаргал
I am trying to write a batch script that will run automatically compressing subdirectories using winrar or 7-zip:
我正在尝试编写一个批处理脚本,该脚本将使用 winrar 或 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.
我怎么写脚本。
采纳答案by Sunny
(1) Using WinRAR:
(1) 使用 WinRAR:
WinRAR
includes two command-line tools, rar.exe and unrar.exe
, where rar.exe compresses and unrar.exe uncompresses files.
WinRAR
包括两个命令行工具,rar.exe and unrar.exe
其中 rar.exe 压缩文件,unrar.exe 解压缩文件。
Both are located in the C:\Program Files\WinRAR
folder in the installable version.
两者都位于可C:\Program Files\WinRAR
安装版本的文件夹中。
Assuming, if there are multiple subfolders under C:\MyPictures
and you want each subfolder to get its own .rar file , in the parent folder.
假设,如果有多个子文件夹,C:\MyPictures
并且您希望每个子文件夹在父文件夹中获得自己的 .rar 文件。
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....
解释....
It'll create .rar files of all the folders/subfolders under parent folder C:\MyPictures in the same parent folder.
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
它将在同一父文件夹中创建父文件夹 C:\MyPictures 下所有文件夹/子文件夹的 .rar 文件。
然后,它将删除父文件夹 C:\MyPictures 下的所有原始文件夹/子文件夹,因此您将只剩下同一位置的档案。
“a”命令添加到存档
“-r”开关递归子文件夹
“-u”开关。与“a”命令结合使用时等效于“u”命令。添加新文件并更新存档中已有文件的旧版本
“-df”开关在文件移动到存档后删除文件
If you want to keep the original subfolders, just remove the -df switch
.
如果要保留原始子文件夹,只需删除-df switch
.
(2) Using 7-Zip:
(2) 使用 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.
7-Zip
是一个高压缩比的文件归档器。7z.exe
是 7-Zip 的命令行版本。7-Zip 不使用系统通配符解析器,也不遵循. 表示任何文件。7 拉链款待。作为匹配任何具有扩展名的文件的名称。要处理所有文件,您必须使用 * 通配符。
Using 7zip command-line options in a batch file, below works for you:
在批处理文件中使用 7zip 命令行选项,以下适用于您:
@echo off
setlocal
for /d %%x in (C:\MyPictures\*.*) do "C:\Program Files-Zipz.exe" a -tzip "%%x.zip" "%%x\"
endlocal
exit
Where
在哪里
-a archive or add
-t Type of archive
-a 存档或添加
-t 归档类型
回答by Kidus
Here's how to do it in 7-zip.
以下是如何在 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.
假设您的文件在文件夹中C:\Pictures\
。然后您可以使用以下批处理命令创建多个与您的目录同名的档案。
FOR /D %%i IN (c:\Pictures\*.*) DO "c:\Program Files-Zipz.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.
这将压缩目录中的每个文件夹Pictures
。切换c:\Pictures
到包含文件夹的目录。如果 7-zip 安装到其他目录,请更改"c:\Program Files\7-Zip\7z.exe"
为安装 7-zip 的目录。
回答by Michal Pokluda
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:
感谢您提供出色的脚本,我只有 3 个问题/更改 - 名称中有空格的目录名不起作用,我只想备份 1 级子目录,我想打包当前目录中的所有子目录(这非常适合快速分别备份所有子目录)。如果您需要类似的东西,这里是修改:
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"
回答by Alex
Another approach if you need to recursivelyzip files in all subdirs in windows is to use gitbashand the approach described here:
如果您需要在 Windows 中的所有子目录中递归压缩文件,另一种方法是使用gitbash和此处描述的方法:
- 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 {} \;
- 如果您安装了 git,请右键单击并选择“Git Bash Here”
- 类型
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
- 保留文件夹结构,将每个 zip 放在各自的子目录中
- 递归压缩目录中的所有文件(不包括空目录)
- 用目录名称命名 zip
You can also exclude specific types with:
您还可以使用以下命令排除特定类型:
find * -type d -execdir sh -c 'cd "" && 7z a "".7z -x!*/ -x!*.7z -x!*.zip && cd -' sh {} \;
回答by tukan
Non of the provided solutions worked for me. I have created a script which packs first into 7zwith maximum compression and then create rararchive for storage with recovery record (it also deletes the interim 7z archive):
没有提供的解决方案对我有用。我创建了一个脚本,它首先以最大压缩率打包到7z 中,然后创建rar存档以用于存储与恢复记录(它还删除了临时 7z 存档):
@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