使用7zip按年归档文件

时间:2013-01-28 16:52:47

标签: 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

1 个答案:

答案 0 :(得分: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%目录以进行压缩。