Jump to content

Get date and currentuser with an inf


shiner

Recommended Posts

I did a search, but didn't come up with anything useful quickly, so:

Is there an environment variable or method to grab the date and the currentuser name from a system which can be used in an inf based installer?

The inf, after getting the date and currentuser name, will then add this information to the registry of the target system.

Link to comment
Share on other sites

Here's what I do with batch commands.

REM :: This sets the environment variable TimeStamp to the format YYYYMMDD_HHMM.  Any leading zeros are dropped.
REM :: Adapted from: http://blogs.technet.com/b/pfe-ireland/archive/2008/05/08/batch-files-date-stamp-in-a-filename.aspx
SET "TimeStamp="
FOR /f "tokens=1-4 delims=/ " %%i in ("%date%") do SET "datestr=%%l%%j%%k"
FOR /f "tokens=1-4 delims=.: " %%i in ("%time%") do SET "timestr=%%i%%j"
SET "TimeStamp=%datestr%_%timestr%"
REM :: Strips any spaces from the variable and replaces them with underscores.
SET "TimeStamp=%TimeStamp: =_%"

Link to comment
Share on other sites

Date can be VERY tricky if you want to make sure that it works correctly worldwide. I've used this reliably:

:: #############################################################################
:: :f_GetDate _TodayDate _Year _Month _DayOfMonth
::
:: Load the ISO format date for today and local date components into variables.
::
:: Usage: CALL :f_GetDate _TodayVariable _YearVariable _MonthVariable _DayOfMonthVariable
::
:: This function should get the correct local system date components copied
:: into the arguments, passed by reference, regardless of where in the world
:: this is run, independent of "International" settings. The arguments are:
::
:: %1 - Variable to receive the date in ISO format (by ref)
:: %2 - Variable to receive year, 4 digits (by ref)
:: %3 - Variable to receive month, 2 digits, 01 to 12 (by ref)
:: %4 - Variable to receive day of month, 2 digits, 01 to 31 (by ref)
::
:: The date in ISO format is equivalent to: %_Year%-%_Month%-%_DayOfMonth%
::
:: Dependencies: START, REGEDIT [Utilizes a temp file %TEMP%.\_Temp.reg]
::
:: Originally writen by Rob van der Woude - http://www.robvanderwoude.com as
:: SortDate, Version 3.10 for Windows NT4/2000/XP. Adapted for Windows XP with
:: help from Kailash Chanduka. Modified slightly by bphlpt.
::
:: Upon exit, ERRORLEVEL is set to 0.
:: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
:f_GetDate _TodayDate _Year _Month _DayOfMonth
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: Export registry settings to temp file, read exported data
START "GET iDATE and sDATE" /WAIT REGEDIT /E "%TEMP%.\_Temp.reg" "HKEY_CURRENT_USER\Control Panel\International"
FOR /F "tokens=1* delims==" %%G IN ('TYPE "%TEMP%.\_Temp.reg" ^| FIND /I "iDate"') DO (SET _iDate=%%H)
FOR /F "tokens=1* delims==" %%G IN ('TYPE "%TEMP%.\_Temp.reg" ^| FIND /I "sDate"') DO (SET _sDate=%%H)
:: Delete quotes and delete temp file
(SET _iDate=%_iDate:"=%)
(SET _sDate=%_sDate:"=%)
del "%TEMP%.\_Temp.reg" >nul 2>&1
:: Parse today's date depending on registry's date format settings
IF %_iDate%==0 (FOR /F "tokens=1-4* delims=%_sDate%" %%G IN ('DATE/T') DO (SET "_Year=%%I"&SET "_Month=%%G"&SET "_Day=%%H")) ELSE (
IF %_iDate%==1 (FOR /F "tokens=1-4* delims=%_sDate%" %%G IN ('DATE/T') DO (SET "_Year=%%I"&SET "_Month=%%H"&SET "_Day=%%G")) ELSE (
IF %_iDate%==2 (FOR /F "tokens=1-4* delims=%_sDate%" %%G IN ('DATE/T') DO (SET "_Year=%%G"&SET "_Month=%%H"&SET "_Day=%%I"))))
:: Remove the day of week if applicable
(FOR %%G IN (%_Year%) DO (SET "_Year=%%G"))&(FOR %%G IN (%_Month%) DO (SET "_Month=%%G"))&(FOR %%G IN (%_Day%) DO (SET "_Day=%%G"))
:: Return date and components
ENDLOCAL&(SET "%2=%_Year%"&SET "%3=%_Month%"&SET "%4=%_Day%"&SET "%1=!%2!-!%3!-!%4!")&EXIT /B 0

And this for time:

:: #############################################################################
:: :f_GetTime _Time
::
:: Load time in a variable, zero padded using ONLY "standard" delimiters (:.)
::
:: Usage: CALL :f_GetTime _Time
::
:: %_Time% will be set to HH:MM:SS.MSC - NOTE: that MS is now THREE digits
::
:: Although the default time delimiter, in Windows XP and above is either . or :
:: users can change the delimiter to just about any character they like. And you
:: know theres always that one guy, the one who writes everything in green ink,
:: who will do this! This script always returns HH:MM:SS.MSC, note that MS
:: is now 3 digits, no matter which time delimiter has been set in the control
:: panel. Based on a discussion at ss64.com, with input from avery_larry and
:: bluesxman, and tweaks by bphlpt using examples by Frank Westlake.
::
:: Dependencies: None
::
:: Upon exit, ERRORLEVEL is set to 0.
:: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
:f_GetTime _Time
SETLOCAL ENABLEEXTENSIONS
FOR /F "tokens=1-3 delims=1234567890 " %%G IN ("%TIME%") DO (SET "_Delims=%%G%%H%%I")
FOR /F "tokens=1-4 delims=%_Delims% " %%G IN ("%TIME%") DO (SET "_hh=00%%G"&SET "_min=00%%H"&SET "_ss=00%%I"&SET "_ms=00%%J0")
ENDLOCAL&(SET %1=%_hh:~-2%:%_min:~-2%:%_ss:~-2%.%_ms:~-3%)&EXIT /B 0

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

2 IceBlackIce, Mr_Smartepants, bphlpt

THANKS.

I have made copies of Mr_Smartepants' and bphlpt's scripts and will keep for future use.

However, I believe I can use the environment variables provided by IceBlackIce (which I should have known) in an INF under a PostSetupCommand directive.

I simply need to get my quotes usage in INFs down pat.

This should be sufficient for my current need.

Link to comment
Share on other sites

  • 4 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...