微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不知道文件夹全名的情况下通过命令行将文件夹压缩为存档文件?

如何解决如何在不知道文件夹全名的情况下通过命令行将文件夹压缩为存档文件?

我必须每个月压缩一些文件夹,这些文件夹总是以引用月份的编号开头,后跟 -

例如:

四月:文件夹是 04- ??????
五月:文件夹是05- ???????

我只知道文件名称的第一部分。文件名称的其余部分始终不同。

我被困在这里

@echo off
for /f "delims=" %%G In ('PowerShell -Command "&{((Get-Date).AddMonths(-1)).ToString('yyyy')}"') do set "ano=%%G" 

for /f "delims=" %%A In ('PowerShell -Command "&{((Get-Date).AddMonths(-1)).ToString('MM-')}"') do set "mes=%%A" 

set "winrar=C:\Program Files\winrar"
"%winrar%\rar.exe" a -ibck -ep1  "C:\FOLDER 1\FOLDER 2\FOLDER 3\%ano%\????????.rar"

我只有有关文件夹的名字部分的信息,例如 04-

如何指定 Rar.exe 仅按第一个文件名称压缩文件夹?

解决方法

我建议阅读 Time is set incorrectly after midnight 上的答案,以了解下面批处理代码的第一个 FOR 命令行,以便在不使用 PowerShell 的情况下获取当前年份和月份:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0"
for /F "tokens=1,2 delims=/" %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "Year=%%I" & set "Month=%%J" & goto CheckFolder
:CheckFolder
for /D %%I in (%Month%-*) do goto CompressFolders
echo INFO: There is no non-hidden folder with name: %Month%-*
goto EndBatch
:CompressFolders
set "ArchiveFolder=C:\FOLDER 1\FOLDER 2\FOLDER 3\%Year%"
md "%ArchiveFolder%" 2>nul
if not exist "%ArchiveFolder%\" echo ERROR: Failed to create folder: "%ArchiveFolder%"& goto EndBatch
"C:\Program Files\WinRAR\Rar.exe" a -cfg- -ep1 -idq -m5 -r -y "%ArchiveFolder%\%Month%.rar" "%Month%-*\*"
:EndBatch
popd
endlocal

该批处理文件将批处理文件目录中名称以当前月份和连字符开头的所有文件夹压缩为一个以当前月份为归档文件名的 RAR 归档文件。因此,例如,如果批处理文件目录包含文件夹 05-Folder05-OtherFolder,则 RAR 存档文件 05.rar 包含这两个文件夹及其所有文件和子文件夹。

当然也可以使用以下代码将名称以当前月份和连字符开头的每个文件夹压缩到一个单独的 RAR 存档文件中:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0"
for /F "tokens=1,2 delims=/" %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "Year=%%I" & set "Month=%%J" & goto CheckFolder
:CheckFolder
for /D %%I in (%Month%-*) do goto CompressFolders
echo INFO: There is no non-hidden folder with name: %Month%-*
goto EndBatch
:CompressFolders
set "ArchiveFolder=C:\FOLDER 1\FOLDER 2\FOLDER 3\%Year%"
md "%ArchiveFolder%" 2>nul
if not exist "%ArchiveFolder%\" echo ERROR: Failed to create folder: "%ArchiveFolder%"& goto EndBatch
for /D %%I in (%Month%-*) do "C:\Program Files\WinRAR\Rar.exe" a -cfg- -ep1 -idq -m5 -r -y "%ArchiveFolder%\%%I.rar" "%%I\"
:EndBatch
popd
endlocal

该批处理文件创建 RAR 存档文件 05-Folder.rar05-OtherFolder.rar,文件夹名称为 05-Folder05-OtherFolder not 包含在适当的 RAR 中由于 "%%I\" 中的反斜杠,存档文件。仅使用 05-Folder 时,文件夹名称 05-OtherFolder"%%I" 将包含在存档文件中。

请双击文件 C:\Program Files\WinRAR\Rar.txt 打开此文本文件并从上到下阅读。它是控制台版本 Rar.exe 的手册。开关 -ibck 不在本手册中,因为它是 GUI 版本 WinRAR.exe 的一个选项,可在后台运行,这意味着最小化到系统托盘。 Rar.exe 命令行开关 -idq 用于在安静模式下创建仅显示错误的存档文件。

要了解使用的命令及其工作原理,请打开 command prompt 窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • popd /?
  • pushd /?
  • robocopy /?
  • set /?
  • setlocal /?

另请参阅 single line with multiple commands using Windows batch file 以了解在上述两个批处理文件中多次使用的运算符 &

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。