file - 使用 7zip 按年份归档文件

标签 file batch-file 7zip

我有一个运行 7zip 的批处理脚本,它允许我将所有文件压缩到一个文件夹中。我只想压缩修改日期为 2010 年或我选择的其他日期的文件。我想在将文件存档到 zip 文件夹后删除它们。这是我的逻辑。 查找 2012 年的文件,并将这些文件存档到名为 2012 的文件夹中。压缩该文件夹并删除文件。 0

这就是我到目前为止所拥有的。

@ECHO OFF

REM This sets name of the zip file
Set choice=
set /p choice=What is the year of the files?
PAUSE

REM This sets the path of the file
Set path=
set /p path=What is the path of the files on C:\'path'\?

REM Use 7Zip to Zip files in folder c:\path and place the zip archive in C:\path

ECHO.
ECHO Zipping all files in C:\%path% and moving archive to c:\%path%
ECHO.
PAUSE

C:\7z a -tzip "C:\%path%\%choice%.zip" "C:\%path%\*.*" -mx5
ECHO.
PAUSE

ECHO Would you like to Delete the files in orginal folder?
DEL "C:\%path%\*.*"
PAUSE

ECHO File is now zipped

最佳答案

看起来很简单。顺便说一句,我不熟悉 7zip 的命令行开关。我想当然地认为您已经掌握了 7z.exe 所需的语法。

@echo off

set 7z=C:\7z.exe
if not exist %7z% set /p 7z="Path to 7-zip executable? "

set /p year="What year do you wish to archive? "

set /p dir="What is the path of the files you wish to archive? "

mkdir "%year%"

rem output of dir is date time am/pm size filename
rem filename = 5th + additional tokens
for /f "tokens=5*" %%I in ('dir "%dir%" ^| find "/%year%"') do move "%dir%\%%I %%J" "%year%"

%7z% a -tzip "%dir%\%year%.zip" "%year%" -mx5

set /p I="I'm about to delete the %year% folder.  Hit Ctrl-C if this is a bad idea, or hit Enter to continue."
rmdir /q /s "%year%"

echo Done.  w00p.

我认为您缺少的是 for 循环。正如我所写的,它执行目录列表,忽略任何与用户输入的年份不匹配的内容,然后将 token 5* 移动到 %year% 目录进行压缩。

关于file - 使用 7zip 按年份归档文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567022/

相关文章:

c# - 在 C# 代码中提取 7zip

windows - 指定用于批处理的 zip 文件

C 逐字读取

c# - 为什么 File.WriteAllText 不将数据写入文件?

ruby - 内置在 ruby​​ 目录中列出目录的方法

windows - 批处理脚本替换字符串中的变量字符串

c# - 使用 7zip 压缩文件的示例 C# .net 代码

file - 去安装: add non-source files to built package

batch-file - 带参数批量启动 exe

windows - 如何在窗口 bat 中获取整个 svn 提交消息?