2

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?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Black Mamba
  • 247
  • 1
  • 12
  • I'm going to assume that `04-` is directly related to `%mes%`, so clearly if your supplied directory uses `%ano%` it is not going to work with the string `04-` which uses a completely different format. Please try a little harder to explain the relationship between the code and the question body. – Compo May 20 '21 at 20:17
  • the `%ano%` is only to get the correct folder year. `%mes%` is only what i know the first name of the folder i want to compact. A program generate a folder named `%mes%+ uknown name`. So when turn the month, i want compact this previous folder. I have to do it every month. So creating a script to do that will facilitate my job. – Black Mamba May 20 '21 at 20:29
  • `FOR /D %%G IN ("C:\FOLDER 1\FOLDER 2\FOLDER 3\%ano%\%mes%*") do set "folder=%%G"` – Squashman May 20 '21 at 20:45
  • Surely it would be more efficient to get the directory name within a single instance of powershell.exe. e.g. `For /F "Delims=" %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile "$d = ((Get-Date).AddMonths(-1)).ToString(\"yyyy\\MM-\")+\"*\"; (Get-Item \"C:\FOLDER 1\FOLDER 2\FOLDER 3\$d\").FullName"') Do @Echo %%G` – Compo May 21 '21 at 01:03
  • Thank you a lot guys! You really helped me a lot! – Black Mamba May 24 '21 at 16:53

1 Answers1

1

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143