使用7-Zip压缩目录中的文件夹,然后删除用于创建.zip文件的源文件夹 [英] Use 7-Zip to Compress folders within a directory and then delete the source folder used to create the .zip file

查看:1279
本文介绍了使用7-Zip压缩目录中的文件夹,然后删除用于创建.zip文件的源文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一个脚本来压缩目录结构中2级文件夹中的所有文件夹。我希望脚本压缩日志文件夹中的所有文件夹,然后删除原始文件夹,从而节省空间。为了说明我希望压缩的文件夹,请参见以下内容:

I need to run a script to compress all folders within a folder that is 2 levels within a directory structure. I want the script to compress all folders within the log folder and then delete the original folder, thus saving loads on space. To illustrate the folders I wish to compress, see below:

驱动器位置->计算机名->日志类型-> folders_i_want_to_compress

Drive Location-->machinename-->logtype-->folders_i_want_to_compress

在文件夹2中,有一些日期格式为yyyymmdd的文件夹,我希望将其压缩为zip文件。

Within folder 2 there are folders with dates in the format yyyymmdd and it is these that I wish to compress as zip files.

我无法解决如何创建脚本来执行此操作,但我确实在这里找到了类似的脚本: 7-Zip压缩文件夹中的文件,然后删除文件

I cannot work out how to create a script to do this but I did find a similar script here:7-Zip compress files within a folder then delete files

...如下所示:

REM Usage: ZipFilesRecursively.bat "C:\My Files"
for /R "%~f1" %%F in (*) do (
    7z a -mx9 "%%~dpnxF.7z" "%%F"
    if exist "%%~dpnxF.7z" del "%%F"
)

但这仅适用于文件。尽管我相信它的开始将用于/ D开关而不是/ R,但我无法设法更改它以使其适用于文件夹而不是文件。

But this is only for files. I cannot work how to change this so that it works for folders rather than files although I believe the start of it would be to use for with the /D switch rather than the /R.

由于文件夹的路径部分基于计算机名称,因此我需要将其输入脚本中,因此我打算使用另一个批处理文件中的调用来运行压缩/删除脚本。

As the path to the folders is based partly on the machine name, I need to feed this into the script, so I was planning on using a call from another batch file to then run the compression/deletion script.

调用脚本如下所示

Call zipdeletetest4 machinename1
Call zipdeletetest4 machinename2    
Call zipdeletetest4 machinename3
Call zipdeletetest4 machinename4

任何人都可以

推荐答案

我最终选择了

REM compress folders to zips
for /d %%x in ("C:\Logs\*.*") do start "C:\Program Files (x86)\7-Zip\7z.exe" /b /low /wait "C:\Program Files (x86)\7-Zip\7z.exe" a -tzip "%%x.zip" "%%x\"

REM delete the original folders used to create zips
for /d %%F in ("C:\Logs\*") do rd /s /q "%%F"

这篇关于使用7-Zip压缩目录中的文件夹,然后删除用于创建.zip文件的源文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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