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:
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-Folderand05-OtherFolder, the RAR archive file05.rarcontains 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:
That batch file creates the RAR archive files
05-Folder.rarand05-OtherFolder.rarwith the folder names05-Folderand05-OtherFoldernot included in the appropriate RAR archive file because of the backslash in"%%I\". The folder names05-Folderand05-OtherFolderwould be included in the archive files on using just"%%I".Please double click on file
C:\Program Files\WinRAR\Rar.txtto open this text file and read it from top to bottom. It is the manual of console versionRar.exe. The switch-ibckis not in this manual because that is an option of the GUI versionWinRAR.exeto run in background which means minimized to system tray. TheRar.execommand line switch-idqis 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.