Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何根据文件夹名称压缩所有超过四个月的文件夹?_Batch File - Fatal编程技术网

Batch file 如何根据文件夹名称压缩所有超过四个月的文件夹?

Batch file 如何根据文件夹名称压缩所有超过四个月的文件夹?,batch-file,Batch File,我有很多文件夹,如下所示: 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-07,2019-06,如果今天的年份和月份是2019-11 如果今天的年份和月份是2020-01,请压缩文件夹2019-09,2019-08,等等 如何做到这一点?这可以通过以下批处理文件实现: @echo off set

我有很多文件夹,如下所示:

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-07
    2019-06
    ,如果今天的年份和月份是
    2019-11
  • 如果今天的年份和月份是
    2020-01
    ,请压缩文件夹
    2019-09
    2019-08
    ,等等

如何做到这一点?

这可以通过以下批处理文件实现:

@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
开头的行。这些注释解释了大部分代码。请阅读我在上回答的第一个命令行的解释

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

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

  • dir/?
  • echo/?
  • endlocal/?
  • 获取/?
  • 如果/?
  • rem/?
  • 设置/?
  • setlocal/?
  • wmic/?
  • wmic操作系统/?
  • wmic操作系统获取/?
  • wmic操作系统获取localdatetime/?

阅读Microsoft关于的文章,了解有关
2>nul
的解释。当Windows命令解释器在执行FOR的命令之前处理此命令行时,重定向操作符必须在上用插入符号^转义,以便命令行被解释为文字字符,该命令行以单独的方式执行嵌入的dir命令行命令进程在后台启动,添加了
%ComSpec%/c
,并在这两个
之间指定了命令行。

这可以通过以下批处理文件完成:

@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
开头的行。这些注释解释了大部分代码。请阅读我在上回答的第一个命令行的解释

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

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

  • dir/?
  • echo/?
  • endlocal/?
  • 获取/?
  • 如果/?
  • rem/?
  • 设置/?
  • setlocal/?
  • wmic/?
  • wmic操作系统/?
  • wmic操作系统获取/?
  • wmic操作系统获取localdatetime/?
阅读Microsoft关于的文章,了解有关
2>nul
的解释。当Windows命令解释器在执行FOR的命令之前处理此命令行时,重定向操作符
必须在上用插入符号^转义,以便命令行被解释为文字字符,该命令行以单独的方式执行嵌入的dir命令行命令进程在后台启动,添加了
%ComSpec%/c
,并在两个
之间指定了命令行