We help IT Professionals succeed at work.

Registry Key Value ... needs System Date instead of hard coded value.

DatabaseMX (Joe Anderson - Microsoft Access MVP)
DatabaseMX (Joe Anderson - Microsoft Access MVP) used Ask the Experts™
on
This is part of a .REG file   ... that gets written via a .BAT file :

[HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Access\Security\Trusted Locations\Location1]
"Path"="C:\\Users\\"
"AllowSubfolders"=dword:00000001
"Description"="RSO Local User System Root"
"Date"="01-08-2020 15:00"

Instead the "Date" being hard coded, I need it to be the System Date/Time

thx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer

Commented:
Probably with a VBS script you will handle this nicely.
Take a look here :https://ss64.com/vb/regwrite.html
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
This generates a time stamp using WMI, and writes it to Date:
@echo off
setlocal enabledelayedexpansion
REM The next line sets the following DayTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set Timestamp=%DT_Month%-%DT_Day%-%DT_Year% %DT_Hour%:%DT_Minute%
echo Timestamp: %Timestamp%

REM Run your registry import here ...

REM Overwrite the date:
set key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Access\Security\Trusted Locations\Location1
>NUL reg.exe add "%key%" /f /v "Date" /t REG_SZ /d "%Timestamp%"

Open in new window

John TsioumprisSoftware & Systems Engineer

Commented:
If you need .bat
for /f "tokens=2,3,4,5,6 usebackq delims=:/ " %a in ('%date% %time%') do echo %c-%a-%b %d%e

Open in new window

Commented:
You've got some good solutions there already but please don't use the last method to get date, only because it is dependent on the user settings of the person or system running the script so will cause you odd issues.

VBScript, WMIC or Powershell would be best to get the dates even if batch file used to set the registry.

It's an old article of mine but this explains why:

https://www.experts-exchange.com/articles/1153/Using-dates-in-batch-files-scripts.html

Steve
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Well folks ... I guess this is a tad more complicated that I had hoped for, which was something like:

"Date"=<SomeSortOfDateTimeVariableHere>

But I guess that is just not how it works :-(

@Steve ...
re "only because it is dependent on the user settings of the person or system running the script "
Actually, that was kind of the intent ... to log when the 'script' was run on each user's system .. which in general would only be one time.

It's not big deal anyway ... hard coding is ok for this scenario.

And there are actually 3 places where Date would need to be dynamically set ... here is the actual Reg file (for A2013) ... and there is also one for A2010 and A2016 or greater.


Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Security]
"VBAWarnings"=dword:00000001

[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Settings]
"Use Row Level Locking"=dword:00000001

[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Security\Trusted Locations]
"AllowNetworkLocations"=dword:00000001

[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Security\Trusted Locations\Location0]
"Path"="\\\\xxxxxxx\\workgroup\\CSBU2\\"
"AllowSubfolders"=dword:00000001
"Description"="CBO Server Root"
"Date"="11-19-2018 13:45"

[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Security\Trusted Locations\Location1]
"Path"="C:\\Users\\"
"AllowSubfolders"=dword:00000001
"Description"="CBO Local User Databases Root"
"Date"="11-19-2018 13:45"

[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Access\Security\Trusted Locations\Location2]
"Path"="C:\\Program Files\\Microsoft Office\\Office15\\ACCWIZ\\"
"Description"="Access default location: Wizard Databases"
"AllowSubfolders"=dword:00000001
"Date"="11-19-2018 13:45"


And this is the .BAT file:

@echo off

set RegFile=\\xxxxx\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzzzBetaTest\A2010TrustedLocations\A2010TrustedLocCSBU2v2.reg

reg import "%RegFile%" >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
  call :ShowMsg "Access 2010 Trusted Locations were SUCCESSFULLY added to your system." "Access 2010 Trusted Locations Install (CSBU2 & Local User)"
) else (
  call :ShowMsg "An Error occurred attempting to add Access 2010 Trusted Locations. Please contact Joe Anderson (joe.anderson@xxxxx.com) immediately." "Access 2010 Trusted Locations Install (CSBU2 & Local User)"
)
exit /b

:ShowMsg [message] [title]
  echo MsgBox "%~1", vbOkOnly, "%~2" >"%TEMP%\_showmsg.vbs"
  if exist "%TEMP%\_showmsg.vbs" (
    wscript //nologo "%TEMP%\_showmsg.vbs"
    del "%TEMP%\_showmsg.vbs"
  )
  exit /b



And the bigger picture is ... I have a 'Database Loader' that all users (>400) have a desktop shortcut to.
I'm modifying the Loader to now check for a specific Reg (key) value.  If it is not present, then run .BAT file to set the Trusted Locations.  If present, Ignore.

In the past, we used to do this manually on each user's system, but this is no longer practical because there are many more users now and there are various versions of Office/Access that have been deployed by IT (don't get me started on this maddening situation, lol).

=== ===
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
What Steve was referring to is the fact that the %date% and %time% variables may have different formats, depending on the OS and user specific settings. You can't really rely upon them having any given format that you can parse as string unless you're running the script always with the same account on the same machine (and you know what you're doing).
Using WMI will always create the correct time stamp.
@echo off
setlocal enabledelayedexpansion

set RegFile=\\xxxxx\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzzzBetaTest\A2010TrustedLocations\A2010TrustedLocCSBU2v2.reg
set AccessVersion=14.0

reg.exe import "%RegFile%" >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
  call :ShowMsg "Access 2010 Trusted Locations were SUCCESSFULLY added to your system." "Access 2010 Trusted Locations Install (CSBU2 & Local User)"
) else (
  call :ShowMsg "An Error occurred attempting to add Access 2010 Trusted Locations. Please contact Joe Anderson (joe.anderson@xxxxx.com) immediately." "Access 2010 Trusted Locations Install (CSBU2 & Local User)"
)

REM The next line sets the following DayTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set Timestamp=%DT_Month%-%DT_Day%-%DT_Year% %DT_Hour%:%DT_Minute%
echo Timestamp: %Timestamp%

REM Fix the static date settings created by the reg file:
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\%AccessVersion%\Access\Security\Trusted Locations
>NUL reg.exe add "%Key%\Location0" /f /v "Date" /t REG_SZ /d "%Timestamp%"
>NUL reg.exe add "%Key%\Location1" /f /v "Date" /t REG_SZ /d "%Timestamp%"
>NUL reg.exe add "%Key%\Location2" /f /v "Date" /t REG_SZ /d "%Timestamp%"

exit /b

:ShowMsg [message] [title]
  echo MsgBox "%~1", vbOkOnly, "%~2" >"%TEMP%\_showmsg.vbs"
  if exist "%TEMP%\_showmsg.vbs" (
    wscript //nologo "%TEMP%\_showmsg.vbs"
    del "%TEMP%\_showmsg.vbs"
  )
  exit /b

Open in new window

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
@oBdA Ok Got ...
I will try your code in a bit ...
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
@oBdA  ... I tried you code and it works as prescribed.
Thank for going the extra mile with the code.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
@oBdA ... as a follow up and inspired by your code, i decided to simplify things a bit.  I added a new Value under the Security Key 'TrustedLocationsSetDate' ... and set the DateTime dynamically.
I will probably remove the other Dates now because with the new Key Value, they are somewhat redundant.
Thanks again ...
=====

REM The next line sets the following DayTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set Timestamp=%DT_Month%-%DT_Day%-%DT_Year% %DT_Hour%:%DT_Minute%
echo Timestamp: %Timestamp%

REM Fix the static date settings created by the reg file:
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\%AccessVersion%\Access
>NUL reg.exe add "%Key%\Security" /f /v "TrustedLocationsSetDate" /t REG_SZ /d "%Timestamp%"

set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\%AccessVersion%\Access\Security\Trusted Locations
>NUL reg.exe add "%Key%\Location0" /f /v "Date" /t REG_SZ /d "%Timestamp%"
>NUL reg.exe add "%Key%\Location1" /f /v "Date" /t REG_SZ /d "%Timestamp%"
>NUL reg.exe add "%Key%\Location2" /f /v "Date" /t REG_SZ /d "%Timestamp%"
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Reg
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Thank you ... not sure how I missed that