Backup log filename with date for Windows

When I run Duplicacy CLI in Windows 11, I can’t seem to grasp appending a date to the log file, along the lines of what happens with the prune command. For example, prune-log-20221129-1243571

Borrowing from posts here I started with batch commands…

Set Log="%Temp%\backup-log.txt"
"C:\Program Files\Duplicacy\duplicacy.exe" -log backup -stats -dry-run > %Log%

This drops an expected log file “backup-log.txt”. So far… so good. Modifying the set command to:

Set Log="%Temp%\backup-log.%DATE%.txt"
"C:\Program Files\Duplicacy\duplicacy.exe" -log backup -stats -dry-run > %Log%

Borrowed from this post

Yields… The system cannot find the path specified.

Thanks for your help!

I think the issue was %DATE% may produce a string like Tue 11/29/2022 in which / is interpreted as a path separator by Windows. So it was looking for a subdirectory that ends with Tue 11.

1 Like

Hi,
Your set statement has an extra unwanted character at the end!

Also like gchen says the %date% might have to be cleaned up.

Thanks, but that was a markdown posting artifact. [Fixed]

Do you have a suggestion? That’s how I copied it from your batch file (see my link).

Did I take that out of context? Thanks!

Yes, sorry, it all depends on regional settings on the specific computer: On my computer %DATE% produces 30.11.2022, so was no need to modify it. I’d better add a comment!
On another computer, like in @gchen’s example, %DATE% will show Wed 11/30/2022
In that case I would remove ‘/’ and perhaps space (or quote the resulting path if you want the space).

For example

REM Remove shash from Date:
set D=%DATE:/=%
REM Remove space:
set D=%D: =%
Set Log="%Temp%\backup-log.%D%.txt"

Please show us your output of echo %DATE% if this did not help!

BTW, if you schedule a task, the account running the task might have a different regional setting too!

1 Like

The above example doesn’t remove the day name (Wed), but it makes a valid file name!
Check Set /? for more string manipulating options, for instance substrings by using the ~ character!

set D=%DATE:~4%
set D=%D:/=%
Echo %D%

Result: 11302022 - not very useful but you could make it IEE date format (yyyymmdd) with a few more lines of set and also add time if you want!

To remove colon from time:

%TIME::=%

Again, regional settings will affect the result of %DATE% and %TIME%…
If you need the code to work regardless of regional settings, maybe use powershell:

get-date -format “yyyyMMdd-hhmmss”

Thanks for linking to the post, people looking at it will see this clarification!

1 Like

Thanks for all that. That should keep me busy tomorrow.

FWIW… I have a dated log output on a Debian unit that is quite simple:

> /home/me/.duplicacy/logs/server2_home_bkup_$(date +\%Y\%m\%d-\%H\%M\%S).log 2>&1

Why is Windows so convoluted? What might work with Powershell, does not necessarily work in the cmd.exe, which Task Scheduler uses, if I understand correctly. :sweat_smile:

2 Likes

I got close with string manipulation… and then ran across parsing, which is adaptable across regional date/time formats. For me…

echo %date%
Wed 11/30/2022
echo %time%
18:33:23.62

Playing around with variable parsing I landed on…

echo %date:~10,4%%date:~4,2%%date:~7,2%
20221130
echo %time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
18332362

Which leads to…

set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set tm=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
Set Log="%Temp%\backup_%dt%-%tm%.log"
cd /D <repo path>
duplicacy -log backup -stats -dry-run

And the log file is backup_20221130-18332362.log

Thanks so much. I never realized how I could manipulate the environmental variables. The advantage of introducing new variables also means that the system variables remain unchanged. A win.

1 Like

A small tweak to handle single-digit hours.

In the preceding script Wed 11/30/2022yields a file name of `backup_20221130- 8332362.log, so we must replace the “blank” character with a zero “0”.

set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set tm=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
REM replace empty hour space with a 0 character
set tm=%tm: =0%
Set Log="%Temp%\backup_%dt%-%tm%.log"
cd /D <repo path>
duplicacy -log backup -stats -dry-run