用于压缩子目录的批处理文件 [英] Batch file to compress subdirectories

查看:28
本文介绍了用于压缩子目录的批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个批处理脚本,该脚本将使用 winrar 或 7-zip 自动运行压缩子目录:

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

示例:

 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
    ...

我如何编写脚本.

推荐答案

(1) 使用 WinRAR:

WinRAR 包含两个命令行工具,rar.exe 和 unrar.exe,其中 rar.exe 压缩文件,unrar.exe 解压缩文件.

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

两者都位于可安装版本的 C:Program FilesWinRAR 文件夹中.

Both are located in the C:Program FilesWinRAR folder in the installable version.

假设,如果 C:MyPictures 下有多个子文件夹,并且您希望每个子文件夹在父文件夹中获得自己的 .rar 文件.

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 FilesWinRAR
ar.exe" a -r -u -df
dir C:MyPictures /ad /s /b > C:MyPicturesfolders.txt
for /f %%f in (C:MyPicturesfolders.txt) do if not exist C:MyPictures\%%~nf.rar %zip% C:MyPictures \%%~nf.rar %%f
endlocal
exit

说明....

  1. 它将创建父目录下所有文件夹/子文件夹的 .rar 文件文件夹 C:MyPictures 位于同一父文件夹中.

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

然后,它会删除父目录下的所有原始文件夹/子文件夹文件夹 C:MyPictures,因此您将只剩下档案在同一个地方.

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"命令添加到存档

  • "a" command adds to the archive

-r"开关递归子文件夹

"-r" switch recurses subfolders

-u"开关.与a"组合时等效于u"命令命令.添加新文件并更新旧版本的文件已经在存档中

"-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"开关在文件移动到存档后删除文件

"-df" switch deletes files after they are moved to the archive

如果您想保留原始子文件夹,只需删除 -df 开关.

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

(2) 使用 7-Zip:

7-Zip 是一个压缩率很高的文件归档器.7z.exe 是 7-Zip 的命令行版本.7-Zip 不使用系统通配符解析器,也不遵循 . 表示任何文件的古老规则.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.

在批处理文件中使用 7zip 命令行选项,以下对您有用:

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 Files7-Zip7z.exe" a -tzip "%%x.zip" "%%x"
endlocal
exit

哪里

  • -存档或添加

  • -a archive or add

-t 存档类型

这篇关于用于压缩子目录的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆