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

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

问题描述

我正在尝试创建一个批处理脚本,该脚本将压缩每个子目录中除最新(或最新几个)之外的所有内容.我目前正在使用 7-Zip 在 Windows 中尝试,但该目录在技术上位于 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,除了最新的 1 个(或少数几个,如果可能).我还想在 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) 中尝试了带有跳过和 dirfor 命令,但无法让它工作.

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%System32findstr.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 文件.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/?
  • for/?
  • 转到/?
  • if/?
  • popd/?
  • pushd/?
  • set/?
  • setlocal/?

阅读有关 Using Command Redirection Operators2>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:

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天全站免登陆