批处理文件对COM preSS子目录 [英] Batch file to compress subdirectories
问题描述
我试图写一个批处理脚本,将用winrar或7-ZIP自动运行的COM pressing子目录:
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
...
我怎样写的脚本。
How do i write script.
推荐答案
(1)使用WinRAR的:
WinRAR的
包括两个命令行工具, RAR.EXE和unrar.exe
,其中COM RAR.EXE presses和unrar.exe uncom presses文件。
WinRAR
includes two command-line tools, rar.exe and unrar.exe
, where rar.exe compresses and unrar.exe uncompresses files.
两者都位于 C:\\ Program Files文件\\ WinRAR的
在安装版本文件夹
Both are located in the C:\Program Files\WinRAR
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 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
解释....
-
这将创建.rar程序父下的所有文件夹/子文件夹的文件
文件夹C:\\ MyPictures在相同的父文件夹
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" command adds to the archive
- R开关递归子文件夹
"-r" switch recurses subfolders
- U开关。相当于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
是一个文件归档与高COM pression比。 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 Files\7-Zip\7z.exe" a -tzip "%%x.zip" "%%x\"
endlocal
exit
其中,
-
-a归档或添加
-a archive or add
存档-t类型
这篇关于批处理文件对COM preSS子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!