Pickle
Pickle

Reputation: 1124

Naming a file as the current date in batch

I'm trying to export a registry key with the current date as the name of the file using:

reg export "HKEY_CURRENT_USER\Network" "\\10.52.32.150\TimeMachine\PRETEND\%username%\%date%.reg"

But I receive "Error: The system was unable to find the specified registry key or value"

Why is this not working?

Thanks

Upvotes: 1

Views: 149

Answers (1)

Joey
Joey

Reputation: 354864

%date% gives you the date in the locale you set. For me this is ISO 8601, i.e. YYYY-MM-DD, but in many other cases it's probably something insane, such as MM/DD/YY. Especially the latter case will make problems as the slash is likely not allowed there (or interpreted as hierarchy separator).

You can get the current date in a usable (and portable) form with WMI via

for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined mydate set mydate=%%x
set mydate=%mydate:~0,8%

You then have the current date in YYYYMMDD form in %mydate%.

Upvotes: 1

Related Questions