Thursday, 17 January 2019

windows - add folder to 7z with current date in front


I have a folder Foo that I want to backup to Foo.2018.07.12.7z


and so far I tried this command:


C:\"Program Files"\7-Zip\7z a "Foo%DATE%".7z Foo

but the problem is that my date format has slashes so it creates nested folders like: Foo12\7\2018.7z



Answer



I prefer to get date/time in a locale/user settings independent format.


Here are two different ways to achieve this:


:: Q:\Test\2018\07\12\SU_1338640.cmd
@Echo off

:: use wmic to get date in a locale/user settings independent format
for /f "tokens=1-3 delims=.+-" %%A in (
'wmic os get LocalDateTime^|findstr ^^[0-9]'
) do Set _DT=%%A
Set "Archive=Foo.%_DT:~0,4%.%_DT:~4,2%.%_DT:~6,2%.7z"
Echo Archive=%Archive%

:: use powershell to get date in a locale/user settings independent format
for /f %%A in (
'Powershell -Nop -C "Get-Date -f yyyy.MM.dd" '
) do Set "_DT=%%A"
Set "Archive=Foo.%_DT%.7z"
Echo Archive=%Archive%

Sample output:


> Q:\Test\2018\07\12\SU_1338640.cmd
Archive=Foo.2018.07.12.7z
Archive=Foo.2018.07.12.7z

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...