Unique batch file leading zeros
Moderator: DosItHelp
-
- Posts: 4
- Joined: 01 May 2012 23:31
Unique batch file leading zeros
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.
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.
Re: Unique batch file leading zeros
Where is this Unique batch file?
This will get you the date with leading zeros that you can parse too.
This will get you the date with leading zeros that you can parse too.
Code: Select all
WMIC OS Get LocalDateTime|findstr \.
Re: Unique batch file leading zeros
Could we see the output of the DATE variable from the CMD shell?
Code: Select all
echo %date%
Re: Unique batch file leading zeros
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"
-
- Posts: 4
- Joined: 01 May 2012 23:31
Re: Unique batch file leading zeros
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#
-
- Posts: 4
- Joined: 01 May 2012 23:31
Re: Unique batch file leading zeros
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?
-
- Posts: 4
- Joined: 01 May 2012 23:31
Re: Unique batch file leading zeros
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
Re: Unique batch file leading zeros
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"
Re: Unique batch file leading zeros
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.
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
Re: Unique batch file leading zeros
Or try this below, it is not dependent on the date format and code page settings.
Tested under WinXP.
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.
Re: Unique batch file leading zeros
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