Unique batch file leading zeros

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dcrossmier
Posts: 4
Joined: 01 May 2012 23:31

Unique batch file leading zeros

#1 Post by dcrossmier » 01 May 2012 23:43

I am trying to use the unique batch file but am not getting the leading zeros as expected on the month and day. I have copied the batch script verbatim without modification but at this date/time: 2012-05-02 01:17AM; Unique.bat output was:

20125201173406
I believe I am parsing it correctly, using spaces below to show that 5 and 2 do not have leading zeros...
2012 5 2 01 17 3406

Running on Windows 7 Home Premium 64-bit.

Am I missing something?
I even tried setting my Region/Date info in the Control Panel to have leading zeros and it still did not work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Unique batch file leading zeros

#2 Post by foxidrive » 02 May 2012 01:24

Where is this Unique batch file?

This will get you the date with leading zeros that you can parse too.


Code: Select all

WMIC OS Get LocalDateTime|findstr \.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Unique batch file leading zeros

#3 Post by Squashman » 02 May 2012 05:58

Could we see the output of the DATE variable from the CMD shell?

Code: Select all

echo %date%

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Unique batch file leading zeros

#4 Post by abc0502 » 02 May 2012 07:19

Squashman wrote:Could we see the output of the DATE variable from the CMD shell?

Code: Select all

echo %date%

like sqashman said and u get the output to a file like that

Code: Select all

echo %date% >>"c:\date.txt"

dcrossmier
Posts: 4
Joined: 01 May 2012 23:31

Re: Unique batch file leading zeros

#5 Post by dcrossmier » 02 May 2012 21:02

Squashman wrote:Could we see the output of the DATE variable from the CMD shell?

Code: Select all

echo %date%


The batch file commands for unique were on this page of DosTips.com
http://www.dostips.com/DtTipsDateTime.php#

dcrossmier
Posts: 4
Joined: 01 May 2012 23:31

Re: Unique batch file leading zeros

#6 Post by dcrossmier » 02 May 2012 21:08

foxidrive wrote:This will get you the date with leading zeros that you can parse too.

Code: Select all

WMIC OS Get LocalDateTime|findstr \.


Very nice, never heard of that! But how do I get the output into a DOS variable in my batch file?

dcrossmier
Posts: 4
Joined: 01 May 2012 23:31

Re: Unique batch file leading zeros

#7 Post by dcrossmier » 02 May 2012 21:10

Squashman wrote:Could we see the output of the DATE variable from the CMD shell?

Code: Select all

echo %date%


I think you are asking my what I see on my computer when I echo the date. This is what I see.

Code: Select all

C:\test>echo %date%
Wed 05-02-2012

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: Unique batch file leading zeros

#8 Post by neorobin » 02 May 2012 21:36

In Unique.bat, this line remove the leading zeros:

Code: Select all

set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: Unique batch file leading zeros

#9 Post by neorobin » 02 May 2012 21:52

Simply do a little modify below, please try again.
This function is dependent on also code page and date format.
To have better versatility, still needs improvement.

Code: Select all

:Unique ret -- returns a unique string based on a date-time-stamp, YYYYMMDDhhmmsscc
::          -- ret    [out,opt] - unique string
:$created 20060101 :$changed 20120503 :$categories StringOperation,DateAndTime
:$source http://www.dostips.com
SETLOCAL
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
  for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do (
    set %%a=%%A&set %%b=%%B&set %%c=%%C)
)
set /a "yy=1000%yy%,mm=10%mm%,dd=10%dd%"
for /f "tokens=1-4 delims=:. " %%A in ("%time: =0%") do @set UNIQUE=%yy:~-4%%mm:~-2%%dd:~-2%%%A%%B%%C%%D
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%
EXIT /b

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: Unique batch file leading zeros

#10 Post by neorobin » 02 May 2012 22:32

Or try this below, it is not dependent on the date format and code page settings.
Tested under WinXP.

Code: Select all

@echo off
call :unique
pause
exit

:Unique ret -- returns a unique string based on a date-time-stamp, YYYYMMDDhhmmsscc
::          -- ret    [out,opt] - unique string
SETLOCAL
set "RegKey=HKCU\Control Panel\International"
for /f "tokens=2*" %%a in ('reg query "%RegKey%" /v sShortDate^|find/i"sSh"') do (
  >nul reg add "%RegKey%" /v sShortDate /t REG_SZ /d "yyyyMMdd" /f
  set "OrgFormat=%%~b"
)
set "tm=%time: =0%"
if defined OrgFormat (
  set "UNIQUE=%date%%tm:~0,2%%tm:~3,2%%tm:~6,2%%tm:~-2%"
  >nul reg add "%RegKey%" /v sShortDate /t REG_SZ /d "%OrgFormat%" /f
)
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%
exit /b
Last edited by neorobin on 02 May 2012 23:15, edited 2 times in total.

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: Unique batch file leading zeros

#11 Post by neorobin » 02 May 2012 22:52

dcrossmier wrote:
foxidrive wrote:This will get you the date with leading zeros that you can parse too.

Code: Select all

WMIC OS Get LocalDateTime|findstr \.


Very nice, never heard of that! But how do I get the output into a DOS variable in my batch file?


The WMIC method:

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('WMIC OS Get LocalDateTime /value^|find /i "="') do set unique=%%a
set "unique=%unique:~0,14%%unique:~15,2%"
echo %unique%
pause

Post Reply