Page 1 of 8
How get data/time independent from localization
Posted: 08 May 2013 01:54
by darioit
Hello,
I have many script that get data and time in this way
Code: Select all
SET G=%DATE:~0,2%
SET M=%DATE:~3,2%
SET Y=%DATE:~6,4%
FOR /F "tokens=1 delims=." %%G IN ('TIME /T') DO SET HH=%%G
SET MM=%TIME:~3,2%
SET SS=%TIME:~6,2%
SET DD=%Y%%M%%G%
SET DD0=%Y%-%M%-%G%
SET DD1=%G%/%M%/%Y%
SET ORA=%HH%%MM%
SET ORA1=%HH%.%MM%.%SS%
or
CALL :sub_routine file.txt
Code: Select all
:sub_routine
SET start=%time%
SET shh=%start:~0,2%
SET smm=%start:~3,2%
SET sss=%start:~6,2%
SET sms=%start:~9,2%
SET DateTime2=%date% %shh%.%smm%.%sss%.%sms%
FOR /f "tokens=1-7 delims=/. " %%a in ("%Datetime2%") do (set "H=0%%d" &call set "DateTime3=%%c-%%b-%%a %%H:~-2%%:%%e:%%f.%%g")
FOR /f "tokens=1-7 delims=/. " %%a in ("%~t1") do (set "H=0%%d" &call set "FileDateTime=%%c-%%b-%%a %%H:~-2%%:%%e:00.00")
I must migrate from Microsoft Server 2003 with Italy regional settings (18/04/2013 15:40:00) to Microsoft Server 2008 with english regional settings (4/18/2013 3:40:00 pm) and I don't want use anymore variable %date% and %time% to avoid this problem.
There is a "universal" way to get this information indipendent from localization? (also for get created date of a file)
Best Regards
Re: Get data/time independent from localization
Posted: 08 May 2013 02:51
by foxidrive
Wmic should work - XP Pro and higher.
Code: Select all
:: timestamp YYYYMMDD_HHMMSS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%
echo %dt%
pause
Code: Select all
:: timestamp YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
echo %dt%
pause
Re: How get data/time independent from localization
Posted: 08 May 2013 03:11
by darioit
Doesn't works!! It's a loop that open many many cmd.exe !!
Re: How get data/time independent from localization
Posted: 08 May 2013 03:15
by Endoro
do you have permission to write to the registry (HKCU)?
Re: How get data/time independent from localization
Posted: 08 May 2013 03:33
by darioit
ok now works thanks, I fix the permission
but how can I get date creation of a specific file?
FOR /f "tokens=1-7 delims=/. " %%a in ("%~t1") do (set "H=0%%d" &call set "FileDateTime=%%c-%%b-%%a %%H:~-2%%:%%e:00.00")
Re: How get data/time independent from localization
Posted: 08 May 2013 03:46
by Endoro
do you have "AM/PM" times? or like military --> 14:23
Re: How get data/time independent from localization
Posted: 08 May 2013 04:21
by darioit
on new server is in English (United States) in this format
4/30/2013 2:05 PM
Re: How get data/time independent from localization
Posted: 08 May 2013 04:29
by Endoro
The creation date doesn't work with "%%~ti", so use the "dir /tc" command, eg.
Code: Select all
@echo off &setlocal
for /f "delims=" %%i in ('dir /a-d /tc ^| findstr /b [0-9]') do (
for /f "tokens=1-7*delims=/: " %%a in ("%%i") do (
set "month=%%a"
set "day=%%b"
set "year=%%c"
set "hour=%%d"
set "min=%%e"
set "AMPM=%%f"
set "fname=%%h"
)
setlocal enabledelayedexpansion
echo(!year!-!month!-!day! !hour!:!min! !AMPM! # !fname!
endlocal
)
Re: How get data/time independent from localization
Posted: 08 May 2013 05:03
by foxidrive
This is independent of region/locale I think. Executing wmic in a for in do is a problem when the file or path contains an & so this uses a temp file.
Code: Select all
:: file timestamp YYYYMMDD_HHMMSS
:: syntax: getfiledate.bat "c:\path\file.exe"
@echo off
set "file=%~1"
set "file=%file:\=\\%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%
echo %dt%
del file.tmp
pause
Re: How get data/time independent from localization
Posted: 08 May 2013 07:04
by darioit
many thanks WMIC is really great !!!
A last consideration, I have about 200 script that are running anytime this script to get time and date
It's correct to use only 1 bat to get date/time and passing variable to main script?
For example:
Myscript.batCode: Select all
call timestamp.bat
call datetimefile.bat "C:\myfile.txt"
echo %dt% %dt1%
timestamp.batCode: Select all
:: timestamp YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
goto :eof
datetimefile.batCode: Select all
:: file timestamp YYYYMMDD_HHMMSS
:: syntax: getfiledate.bat "c:\path\file.exe"
@echo off
set "file=%~1"
set "file=%file:\=\\%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt1=%%a
set dt1=%dt:~0,8%_%dt:~8,6%
del file.tmp
goto :eof
Or it's better put head to each script his own variable to get date/time?
Regards
Re: How get data/time independent from localization
Posted: 08 May 2013 07:47
by foxidrive
darioit wrote:WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt1=%%a
set dt1=%dt1:~0,8%_%dt1:~8,6%
Note the two changes above where you changed the variable name but missed it in two spots.
If you ever contemplate changing the date and filestamp routines then keeping them in self contained batch files is a good idea.
The only time it could be an issue is if two or more batch files call the routine at the same time.
Re: How get data/time independent from localization
Posted: 08 May 2013 09:10
by darioit
thanks for the reply I had already correct the error
So about the issue of two or more batch, it's better to call it with the command "CMD /C timestamp.bat" ?
Regards
Re: How get data/time independent from localization
Posted: 08 May 2013 13:14
by miskox
Re: How get data/time independent from localization
Posted: 08 May 2013 13:34
by carlos
I write this script in 2009, it only works in 32 bits windows (uses debug), but is other possibility:
Code: Select all
@Echo Off
:dateinfo
::last modification: 22/11/2009
setlocal enableextensions
for /f "tokens=2,6,8 delims== " %%a in (
'^(echo.e100 B4 2A CD 21 CC^&echo.g^&echo.q^)^|debug^|find "AX="'
) do set "df=%%a%%b%%c"
set /a "d=0x%df:~10,2%,m=0x%df:~8,2%,y=0x%df:~4,4%,wd=%df:~2,2%"
echo.day=%d%
echo.month=%m%
echo.year=%y%
echo.weekday=%wd%
exit /b
Re: How get data/time independent from localization
Posted: 08 May 2013 21:32
by foxidrive
darioit wrote:So about the issue of two or more batch, it's better to call it with the command "CMD /C timestamp.bat" ?
This is fine: call timestamp.bat
Using cmd /c will not make any difference to multiple calls.