首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据文件夹名称压缩所有四个月以上的文件夹?

如何根据文件夹名称压缩所有四个月以上的文件夹?
EN

Stack Overflow用户
提问于 2019-11-25 02:07:27
回答 1查看 208关注 0票数 1

我有很多文件夹如下:

代码语言:javascript
运行
复制
D:\forZip\2019-11
D:\forZip\2019-10
D:\forZip\2019-09
D:\forZip\2019-08
D:\forZip\2019-07
D:\forZip\2019-06

我想:

  • 压缩文件夹2019-072019-06等,如果今天的年份和月份是2019-11
  • 压缩文件夹2019-092019-08等,如果今天的年份和月份是2020-01

这是怎么做到的呢?

EN

回答 1

Stack Overflow用户

发布于 2019-11-25 07:22:15

这可以通过这样的批处理文件来完成:

代码语言:javascript
运行
复制
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ParentFolder=D:\forZip"

rem Get current date/time as string in format yyyyMMddHHmmss region independent.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "CurrentDate=%%I"

rem Year of folders to ZIP compress is by default current year.
set "FolderYear=%CurrentDate:~0,4%"

rem Month of folders to ZIP compress is by default current month minus four
rem months. A number with a leading 0 would be interpreted as octal number
rem and 08 and 09 are invalid octal numbers being replaced on evaluation
rem of arithmetic expression by value 0 which is not wanted here. For that
rem reason character 1 is prepended to string of current month resulting in
rem the values 101 to 112 on evaluation of arithmetic expression subtracting
rem 104 to subtract in real four from current month value with leading 0 for
rem the months 1 to 9.

set /A FolderMonth=1%CurrentDate:~4,2% - 104

rem If the month value is less than one, the folder year must be decremented
rem by one and the folder month must be incremented by twelve to get correct
rem date in previous year.

if %FolderMonth% LSS 1 (
    set /A FolderYear-=1
    set /A FolderMonth+=12
)

rem Prepend a 0 to string value of folder month and next get the last
rem two characters from folder month to have finally the folder month
rem string always with two digits, i.e. 01, 02, 03, ..., 10, 11, 12.

set "FolderMonth=0%FolderMonth%"
set "FolderMonth=%FolderMonth:~-2%"

rem Search with DIR executed by a background command process started by FOR
rem for directores matching the pattern ????-?? in configured parent folder
rem and process this list of folder names without path. Each folder name is
rem split up into two parts - the four digit year assigned to loop variable J
rem and the two digit month assigned to loop variable K. If the year string
rem (not number) from folder name is less than the folder year string, this
rem folder is moved into a ZIP file. If the year string from folder name is
rem equal the folder year string and the month string from folder name is
rem less or equal the month string, the folder is also moved into a ZIP file.

echo Processing all folders up to %FolderYear%-%FolderMonth%.
for /F "eol=| delims=" %%I in ('dir "%ParentFolder%\????-??" /AD /B /O-N 2^>nul') do (
    for /F "tokens=1,2 delims=-" %%J in ("%%I") do (
        if "%%J" LSS "%FolderYear%" (
            echo Compressing folder %%I ...
            "%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -cfg- -ep1 -ibck -inul -m5 -r "%ParentFolder%\%%I.zip" "%ParentFolder%\%%I"
        ) else if "%%J" == "%FolderYear%" if "%%K" LEQ "%FolderMonth%" (
            echo Compressing folder %%I ...
            "%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -cfg- -ep1 -ibck -inul -m5 -r "%ParentFolder%\%%I.zip" "%ParentFolder%\%%I"
        )
    )
)

endlocal

批处理文件代码包含注释,这些注释是以rem开头的行。这些注释解释了大部分代码。请阅读第一个 for 命令行的说明--我在为什么%date%在按计划任务执行的批处理文件中产生不同的结果?上的答案

我使用了20多年的WinRAR作为压缩工具。因此,编写批处理文件是为了使用共享软件WinRAR.exe将文件夹压缩到ZIP存档文件中。当然,使用WinRAR.exe的两个命令行可以被任何其他命令行替换,这些命令行调用可执行文件或脚本,将当前文件夹压缩为一个ZIP文件,并在成功压缩后删除该文件夹。

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

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

阅读微软关于使用命令重定向运算符的文章,了解对2>nul的解释。当>命令解释器在执行命令FOR之前处理此命令行时,必须在FOR命令行上使用插入字符^对重定向操作符进行转义,该命令行使用在%ComSpec% /c背景下启动的单独的命令进程以及两个'之间指定的命令行来执行嵌入式dir命令行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59024342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档