1

I'm using WinRAR command line for archiving files into a predefined location with the name of the date like: 20171206.rar When I rerun the command, I want the RAR file to be replaced and not updated. I went through the list of WinRAR command line switches but found nothing that gets me the desired result.

Any and all comments are welcome.

Mofi
  • 46,139
  • 17
  • 80
  • 143
NetFlash
  • 85
  • 4
  • 12

1 Answers1

1

WinRAR has no command or switch to ignore an existing archive file on compressing files and folders into an archive like all compression tools I'm aware of.

WinRAR has the command d to delete files in an archive with deleting the archive file itself on finally not containing any file. But I think it is not advisable to use WinRAR with command d to just delete an archive file before creating a new archive file with same name.

I use for archiving source files the switch -agYYYY-MM-DD_NN making it possible for me to create more than one individual archive per day for backup purposes. WinRAR automatically creates RAR archive files with current date in format yyyy-MM-dd in file name with an incremented number because of NN, i.e. 2017-12-06_01.rar, 2017-12-06_02.rar, ..., 2017-12-07_01.rar, and so on. I delete manually the backup archives no longer needed from time to time.

I suggest the following batch file for your task:

@echo off
if not exist "20??????.rar" goto CreateDayArchive
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "FileNameDate=%%I"
del "%FileNameDate:~0,8%.rar" 2>nul
set "FileNameDate="

:CreateDayArchive
rem The RAR/WinRAR command line to create the new archive with current date.

The FOR command line to get current date in format yyyyMMdd region independent is explained in detail for example at Why does %date% produce a different result in batch file executed as scheduled task?

The usage of the environment variable DATE would be much faster than using the command WMIC. But the date format of the environment variable DATE depends on Windows region and language settings. On my computer the date value of the environment variable DATE is always in format dd.MM.yyyy which makes it possible to use this much faster batch file:

@echo off
del "%DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%.rar" 2>nul
rem The RAR/WinRAR command line to create the new archive with current date.

This code works also with / or - as separator instead of . as well as for date strings with abbreviated weekday at beginning because year, month and day are substituted from DATE string from right side. So a date like Wed, 06/12/2017 (6th December 2017) would be also processed correct by this fast batch code variant.

If the region dependent date format of DATE is MM/dd/yyyy without or with abbreviated weekday at beginning, the following batch file could be used with day and month substitution exchanged.

@echo off
del "%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%.rar" 2>nul
rem The RAR/WinRAR command line to create the new archive with current date.

See also answer on What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean? for a complete explanation on date string substitution to reformat region dependent DATE value into the wanted date format.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

Read also the Microsoft article about Using command redirection operators for an explanation of 2>nul used here to suppress the error message output by DEL to handle STDERR by redirecting it to device NUL if there is no RAR archive file with current date in file name in the current directory.

Mofi
  • 46,139
  • 17
  • 80
  • 143