【问题标题】:Using 7zip to archive file by year使用 7zip 按年归档文件
【发布时间】:2013-01-28 16:52:47
【问题描述】:

我有一个运行 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

【问题讨论】:

  • 危险威尔罗宾逊,危险。你在那里的set /p path= 行会让Windows 忘记在哪里可以找到它的一些可执行文件,至少在它重新启动之前是这样。最好不要回收已经有其他用途的变量。

标签: file batch-file 7zip


【解决方案1】:

看起来很简单。顺便说一句,我不熟悉 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 循环。正如我写的那样,它执行目录列表,在用户输入时忽略任何与年份不匹配的内容,然后将令牌 5* 移动到 %year% 目录进行压缩。

【讨论】:

  • 这对我有用。我把代码改了很多,但我只想感谢你的 find /year 命令。
  • @user2012448 - 很酷。如果您同意这样做是合适的,请正式接受我的回答。有关说明,请参阅 meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2012-07-31
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
相关资源
最近更新 更多