2

The cloud service Dropbox used by me does not preserve the original time stamp of uploaded folders. I have to use the WinRAR to compress the folder to backup in the cloud to keep the original folder date/time before I upload it to Dropbox. But I could not find an easy way to create the archive file name with the current last modification date of the folder in format YYYYMMDD in file name of archive file.

The command line

for %I in ("d:\doc\aatmp") do @echo %~tI

executed in a command prompt window outputs:

2014-06-01 22:12

So the region dependent date format of FOR is YYYY-MM-DD hh:mm.

The currently used command line to create the RAR archive is:

winrar a -r -agYYYYMMDD-HHMM kk "d:\doc\aatmp\"

Reference: How to create a RAR archive with current date in archive file name?

How to get date of archived folder d:\doc\aatmp into the archive file name kk.rar instead of the current date/time?

Mofi
  • 38,783
  • 14
  • 62
  • 115
Tangent Lin
  • 35
  • 1
  • 5

1 Answers1

1

This task could be done on your Windows computer with your region settings using a batch file with following command lines:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FolderToBackup=D:\doc\aatmp"

rem Get last modification date/time of the folder to backup
rem in region dependent format which is YYYY-MM-DD hh:mm.
for %%I in ("%FolderToBackup%") do set "FolderTimeStamp=%%~tI"

rem Get from this date/time string just the year, month
rem and day of month from the date without the hyphens.
set "FolderTimeStamp=%FolderTimeStamp:~0,4%%FolderTimeStamp:~5,2%%FolderTimeStamp:~8,2%"

rem Compress the folder to backup into a RAR archive file with
rem last modification date of folder used in archive file name.
"%ProgramFiles%\WinRAR\WinRAR.exe" a -ac -cfg- -dh -ep1 -ibck -m4 -oh -ol -os -ow -r -ts -y -- kk_%FolderTimeStamp%.rar "%FolderToBackup%"

rem Restore the environment as set before usage of command SETLOCAL at top.
endlocal

The string assigned to environment variable FolderTimeStamp is for example:

2014-06-01 22:12

This string should be modified to just:

20140601

This is done using string substitution as explained by help of command SET output on running in a command prompt window set /? and also by the answer on What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?

The character index counting starts with 0. The first number after :~ is always the character index (from left on positive or from right on negative) and the second number is here always the number of characters.

The first character of the year is at character index 0 and the year has four characters. For that reason the first string substitution is :~0,4 to get the characters from index 0 to index 3.

The hyphen at character index 4 should not be in file name and is ignored for that reason.

The next two characters in folder time stamp string are the month at character indexes 5 and 6 which is the reason for using :~5,2.

The hyphen at character index 7 is again ignored as not needed for file name.

The next two characters in folder time stamp string are the day of month at character indexes 8 and 9 which is the reason for using :~8,2.

The last six characters starting from character index 10 are of no interest for file name. Therefore those six characters are ignored, too.

WinRAR is executed after determining last modification date of folder to backup and reformat the time stamp suitable for the archive file name to create a RAR archive with that string in archive file name with following additional options specified with the switches:

  • -ac ... clear archive attribute after compression to know which files are included in last archive and which are modified since last backup.
  • -cfg- ... ignore default profile and environment variable.
  • -dh ... open shared files.
  • -ep1 ... exclude base folder from names which means D:\doc is not included in archive, but the folder aatmp with all its attributes, time stamps and permissions is included in archive. See answer on Simply compress 1 folder in batch with WinRAR command line? for details about difference on specifying on command line "%FolderToBackup%" without a backslash at end or "%FolderToBackup\" with a backslash at end.
  • -ibck ... run WinRAR in background which means minimized to system tray.
  • -m4 ... use good compression.
  • -oh ... save hard links as the link instead of the file.
  • -ol ... save symbolic links as the link instead of the file.
  • -os ... save NTFS streams.
  • -ow ... process file security information.
  • -r ... recursively add all subfolders and files.
  • -ts ... save all file times (modification, creation, access).
  • -y ... assume Yes on all queries.
  • -- ... no more switch.

Start WinRAR, click in last main menu Help on first menu item Help topics, click on tab Contents on list item Command line mode, click on list item Switches and read the appropriate help page for the used switches for details.

The created RAR archive is a real backup as it includes not just the files and subfolders with their last modification dates and attributes, but also with creation and last access dates, alternate data streams in case of any file has one, hard and symbolic links, and NTFS security permissions to be able to really restore everything on extracting the RAR archive with the appropriate switches and using an account which supports restoring even the NTFS security permissions.

For understanding the other used commands not explained in detail above 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.

  • echo /?
  • endlocal /?
  • for /?
  • rem /?
  • set /?
  • setlocal /?
Mofi
  • 38,783
  • 14
  • 62
  • 115