如何将所有文件(文件夹中每个子文件夹中的最新文件除外)压缩到每个子文件夹一个ZIP文件中? [英] How to compress all files with exception of newest file in each subfolder of a folder into one ZIP file per subfolder?

查看:84
本文介绍了如何将所有文件(文件夹中每个子文件夹中的最新文件除外)压缩到每个子文件夹一个ZIP文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个批处理脚本,该脚本将压缩每个子目录中除最新(或最近几个)之外的所有内容.我目前正在Windows中使用7-Zip进行尝试,但是从技术上讲,该目录位于Linux服务器上,因此欢迎针对Linux命令的任何建议.

I'm trying to create a batch script that will zip all the contents in each subdirectory except the latest (or latest few). I'm currently attempting in Windows with 7-Zip but the directory is technically on a Linux server so any suggestions geared towards a Linux command is welcome.

目录结构如下:

Directory-Parent
 ---Sub-Directory-1
 --------File1
 --------File2
 --------File3
 ---Sub-Directory-2
 --------File1
 --------File2

我想在 Directory-Parent 级别运行一个批处理,该批处理将在所有文件的每个子目录中创建一个zip文件,但最新文件除外(如果可能的话,则为几个).我还想在zip文件名的末尾添加年份.

I would like to run a batch at the Directory-Parent level that will create a zip in each sub-directory of all the files except the latest 1 (or few if possible). I also want to add the year to the end of the zip file name.

因此结果将是:

Directory-Parent
 -Sub-Directory-1
 --------File1
 --------Sub-Directory-12019.zip
 ---Sub-Directory-2
 --------File1
 --------Sub-Directory-22019.zip

我尝试了嵌套的 for 循环,但似乎无法理解.我已经尝试过在集合(IN)中使用skip和 dir for 命令,但无法使其正常工作.

I've tried a nested for loop but can't seem to get it. I've tried the for command with skip and dir in the set (IN), but can't get it to work.

我目前有以下脚本.

SET theYear=2019
For /D %%d in (*.*) do 7z a "%%d\%%d%theYear%.zip" ".\%%d\*"

除了我不知道如何排除每个文件夹中的最新文件(根据上次修改时间的最新文件)之外,这一切都完成了.

This accomplishes it all except I don't know how to exclude the latest file (newest file according to last modification time) in each folder.

推荐答案

此批处理文件可用于此任务:

This batch file can be used for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FilesToIgnore=1"
set "theYear=%DATE:~-4%"
set "ListFile=%Temp%\%~n0.lst"
del "%ListFile%" 2>nul
for /D %%I in (*) do call :CompressFiles "%%I"
goto EndBatch

:CompressFiles
pushd %1
set "ZipFile=%~nx1%theYear%.zip"
for /F "skip=%FilesToIgnore% eol=| delims=" %%J in ('dir /A-D /B /O-D /TW 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /L /V /X /C:"%ZipFile%"') do >>"%ListFile%" echo %%J
if exist "%ListFile%" (
    echo Compressing files in directory %1 ...
    7z.exe a -bd -bso0 -i"@%ListFile%" -mx9 -scsDOS -- "%ZipFile%"
    del "%ListFile%"
)
popd
goto :EOF

:EndBatch
endlocal

批处理文件从动态环境变量 DATE 的区域相关日期字符串动态设置环境变量 theyear .请在命令提示符窗口中执行 echo%DATE:〜-4%,并验证输出是否为当前年份,因为 echo%DATE%输出当前的本地日期和最后四个日期字符为年份.

The batch file sets environment variable theYear dynamically from region dependent date string of dynamic environment variable DATE. Please execute in a command prompt window echo %DATE:~-4% and verify if output is the current year because of echo %DATE% outputs current local date with last four characters being the year.

批处理文件将忽略每个目录中的 FilesToIgnore 最新文件.如果该批处理文件的先前执行中已经存在该ZIP文件,则也将被忽略.ZIP文件从不包含在 FilesToIgnore 的数量中,因为 findstr 已将其过滤掉,该文件过滤了命令 dir 的输出,该命令输出的文件名不包含当前目录中的路径,按最后修改时间排序,最新文件输出优先,最旧文件输出最后.

The batch file ignores the FilesToIgnore newest files in each directory. Ignored is also the ZIP file if already existing from a previous execution of the batch file. The ZIP file is never included in number of FilesToIgnore because of filtered out already by findstr which filters output of command dir which outputs the file names without path in current directory ordered by last modification time with newest files output first and oldest files output last.

有关所使用的开关和参数,请阅读 7-Zip 的帮助.

Please read help of 7-Zip for the used switches and parameters.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del/?
  • dir/?
  • echo/?
  • endlocal/?
  • findstr/?
  • 用于/?
  • 转到/?
  • 如果/?
  • popd/?
  • pushd/?
  • 设置/?
  • setlocal/?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • set /?
  • setlocal /?

阅读有关使用命令重定向运算符来解释 2> nul | .重定向操作符> | 必须在 FOR 命令行上使用脱字符号 ^ 进行转义,以解释为Windows命令解释器在执行命令 FOR 之前处理此命令行时的原义字符,该命令在后台启动的单独命令进程中执行嵌入式命令行.

Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul and |. The redirection operators > and | must be escaped with caret character ^ on FOR command line to be interpreted as literal characters when Windows command interpreter processes this command line before executing command FOR which executes the embedded command line in a separate command process started in background.

更新: 7-Zip 版本19.00.0.0输出警告,并使用如下所示的命令行在每个子目录中创建一个空的ZIP文件,该命令行最初用于批处理文件.我首先认为这是 7-Zip 版本19.00.0.0的错误,因为根据该版本的帮助和 7-Zip 版本16.04.0.0可以在命令行上使用:

Update: 7-Zip version 19.00.0.0 outputs a warning and creates an empty ZIP file in each subdirectory on using the command line as written below and used initially in batch file. I first thought this is a bug of 7-Zip version 19.00.0.0 because of this version should support also -- according to its help and 7-Zip version 16.04.0.0 works on using the command line:

7z.exe a -bd -bso0 -mx9 -scsDOS -- "%ZipFile%" "@%ListFile%"

有必要从此命令行中删除-,以使其与 7-Zip 19.00.0.0版一起使用.

It was necessary to remove -- from this command line to get it working with 7-Zip version 19.00.0.0.

所以我报告了此问题,《 7-Zip》的作者迅速回答解释为什么使用-会导致从 7-Zip 19.00.0.0版本开始以文件名搜索以 @ 开头的文件:/p>

So I reported this issue and the author of 7-Zip quickly replied explaining why the usage of -- results in searching for a file starting with @ in file name since 7-Zip version 19.00.0.0:

我们需要某种方法从命令行添加 @file -file 名称.
因此,-停止 @ -解析,并使用7-Zip搜索确切名称.
改为使用 -i @ listfile 更为安全.

We need some way to add @file and -file names from command line.
So -- stops @ and - parsing, and 7-Zip searches exact names.
It's more safe to use -i@listfile instead.

因此,我更新了批处理文件代码,并使用选项 -i 指定列表文件,这是指定列表文件的最安全方法.

So I updated the batch file code and specify the list file with option -i which is the most safe method to specify a list file.

这篇关于如何将所有文件(文件夹中每个子文件夹中的最新文件除外)压缩到每个子文件夹一个ZIP文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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