本文介绍了如何用存档文件名中的已存档文件夹的日期创建RAR存档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的云服务Dropbox无法保留上传文件夹的原始时间戳.我必须使用 WinRAR 将文件夹压缩以备份到云中,以保留原始文件夹的日期/时间,然后再将其上传到Dropbox.但是我找不到一种简便的方法来创建存档文件名,该文件夹的当前上次修改日期的格式为存档文件的文件名中的YYYYMMDD格式.

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.

命令行

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

在命令提示符窗口中执行以下输出:

executed in a command prompt window outputs:

2014-06-01 22:12

因此, FOR 的与地区相关的日期格式为YYYY-MM-DD hh:mm.

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

当前用于创建RAR存档的命令行为:

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

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

参考:如何使用存档文件名中的当前日期创建RAR存档?

如何将存档文件夹d:\doc\aatmp的日期获取到存档文件名kk.rar中而不是当前日期/时间?

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

推荐答案

可以使用以下命令行在批处理文件中使用区域设置在Windows计算机上完成此任务:

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

分配给环境变量FolderTimeStamp的字符串例如:

The string assigned to environment variable FolderTimeStamp is for example:

2014-06-01 22:12

该字符串应修改为:

20140601

这是通过使用字符串替换完成的,如在命令提示符窗口set /?中运行时在命令 SET 中输出的帮助以及

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?

字符索引计数从0开始.:~之后的第一个数字始终是字符索引(从左到右或从右到负),第二个数字是此处始终是数字字符.

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.

年份的第一个字符在字符索引0,并且年份有四个字符.因此,第一个字符串替换为:~0,4,以使字符从索引0到索引3.

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.

字符索引4处的连字符不应位于文件名中,因此将被忽略.

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

文件夹时间戳字符串中的后两个字符是字符索引56的月份,这是使用:~5,2的原因.

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.

字符索引7处的连字符再次被忽略,因为文件名不需要.

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

文件夹时间戳字符串中的后两个字符是字符索引89的月份中的一天,这是使用:~8,2的原因.

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.

从字符索引10开始的最后六个字符与文件名无关.因此,这六个字符也将被忽略.

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

WinRAR 在确定要备份的文件夹的最后修改日期并重新格式化适合存档文件名的时间戳后创建,以在存档文件名中使用该字符串创建RAR存档,并指定了以下附加选项用开关:

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 ...压缩后清除 archive 属性,以了解哪些文件包含在上次存档中,哪些文件自上次备份以来已被修改.
  • -cfg- ...忽略默认配置文件和环境变量.
  • -dh ...打开共享文件.
  • -ep1 ...从名称中排除基本文件夹,这意味着D:\doc不包含在存档中,但包含所有属性,时间戳和权限的文件夹aatmp包含在存档中.请参阅仅使用WinRAR命令行批量压缩1个文件夹?的答案,以获取有关在命令行"%FolderToBackup%"上指定的区别的详细信息末尾没有反斜杠,或者"%FolderToBackup\"末尾没有反斜杠.
  • -ibck ...在后台运行 WinRAR ,这意味着将其最小化到系统托盘.
  • -m4 ...使用压缩.
  • -oh ...将硬链接保存为链接而不是文件.
  • -ol ...将符号链接保存为链接而不是文件.
  • -os ...保存NTFS流.
  • -ow ...正在处理文件安全性信息.
  • -r ...递归添加所有子文件夹和文件.
  • -ts ...保存所有文件时间(修改,创建,访问).
  • -y ...对所有查询均假定为.
  • -- ...不再有开关.
  • -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.

启动 WinRAR ,在最后一个主菜单帮助中单击,在第一个菜单项帮助主题中,单击选项卡目录 >在列表项命令行模式上,单击列表项开关,并阅读所用开关的相应帮助页面以获取详细信息.

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.

创建的RAR归档文件是真正的备份,因为它不仅包括具有最后修改日期和属性的文件和子文件夹,而且还包括创建和最后访问日期,如果任何文件具有一个,硬和一个文件,则备用数据流也将包含这些文件和子文件夹.符号链接和NTFS安全权限,以便能够通过适当的开关并使用支持甚至恢复NTFS安全权限的帐户来提取RAR归档文件时,真正还原所有内容.

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 /?
  • echo /?
  • endlocal /?
  • for /?
  • rem /?
  • set /?
  • setlocal /?

这篇关于如何用存档文件名中的已存档文件夹的日期创建RAR存档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:06