I have to compress some folders every month that always start with the number of the referenced month followed by a -
.
For example:
April: folder is 04- ??????
May: folder is 05- ???????
I just know the first part of the folder name. The rest of the folder name is always different.
I´m stuck here:
@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"
I just have the information about the first name part of the folder like 04-
.
How can I specify Rar.exe
to compress the folder by only the first folder name?
I recommend to read the answers on Time is set incorrectly after midnight for understanding the first FOR command line of the batch code below to get current year and month without using 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
That batch file compresses all folders in directory of the batch file with name starting with current month and a hyphen into a RAR archive file with current month as archive file name. So if the batch file directory contains, for example, the folders 05-Folder
and 05-OtherFolder
, the RAR archive file 05.rar
contains these two folders with all its files and subfolders.
It is of course also possible to compress each folder with name starting with current month and a hyphen into a separate RAR archive file by using the following code:
@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
That batch file creates the RAR archive files 05-Folder.rar
and 05-OtherFolder.rar
with the folder names 05-Folder
and 05-OtherFolder
not included in the appropriate RAR archive file because of the backslash in "%%I\"
. The folder names 05-Folder
and 05-OtherFolder
would be included in the archive files on using just "%%I"
.
Please double click on file C:\Program Files\WinRAR\Rar.txt
to open this text file and read it from top to bottom. It is the manual of console version Rar.exe
. The switch -ibck
is not in this manual because that is an option of the GUI version WinRAR.exe
to run in background which means minimized to system tray. The Rar.exe
command line switch -idq
is used instead to create the archive files in quiet mode showing only errors.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
popd /?
pushd /?
robocopy /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of operator &
used multiple times in the two batch files above.