Chris
Chris

Reputation: 2763

Enumerate Windows Regional Setting In Batch File

I have a pre-build compilation event which runs a batch file. The batch files takes the date and strips out the year/month/day as follows:

set PRODUCT_YEAR=%DATE:~12,2%
set PRODUCT_MNTH=%DATE:~4,2%
set PRODUCT_DAYS=%DATE:~7,2%

I then dynamically create a header file by piping these variables with other data to an output header file.

Note that I cannot using the compile pre-directives to get the date, because I also need to include this same header file in to an install packet generator which does not support the compiler like pre-directives.

This all works fine but one of our developers found that a different regional setting results in the date being split incorrectly. If you have USA setting the date format in different than CANADA setting.

I am not interested in supporting ALL regional settings. I am not interested in changing the regional setting. I simply want to be able to tell for a certainty if I am running a USA setting, or a CANADIAN setting, so that I can strip the date accordingly.

I have checked SO and there are a lot of very huge code solutions to this which will require quite a bit of study before they can be plugged into our build environment, which is not an option right now.

So my question is: is there a way that I can tell if the regional setting is USA v CANADA in a batch file, so that I can take the appropriate action on it?

Upvotes: 0

Views: 235

Answers (2)

foxidrive
foxidrive

Reputation: 41244

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

Upvotes: 1

SachaDee
SachaDee

Reputation: 9545

Using Wmic you can get this values indenpendantly of the regional setting :

@echo off
for /f "tokens=1-3" %%a in ('wmic path Win32_LocalTime Get Day^,Month^,Year ^|find "0"') do (
set "$day=00%%a"
set "$month=00%%b"
set "$year=%%c")

set PRODUCT_YEAR=%$Year:~-2%
set PRODUCT_MNTH=%$Month:~-2%
set PRODUCT_DAYS=%$Day:~-2%

echo %PRODUCT_YEAR%
echo %PRODUCT_MNTH%
echo %PRODUCT_DAYS%

Upvotes: 2

Related Questions