Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split %date% in a batch file regardless of Regional Settings

Is there a way to split the %date% in a batch file (say, in 3 environment variables), but regardless of Regional Settings? Today's date would be 3/13/2013 for US, but with my Regional Settings it is 13.3.2013 - the delimiter is changed and the order as well.

like image 436
sashoalm Avatar asked Mar 13 '13 06:03

sashoalm


People also ask

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do I run a batch file from any location?

Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME. bat.

What does %* mean in batch?

%* expands to the complete list of arguments passed to the script. You typically use it when you want to call some other program or script and pass the same arguments that were passed to your script.

How do you handle a space in a batch file?

When you send arguments, those with poison or space characters need to be doublequoted. Inside your batch file, if you no longer need the surrounding doublequotes, you'd remove them by using %~5 instead of %5 . Additionally the recommended syntax for the set command is Set "VariableName=VariableValue" .


2 Answers

Four years have passed, but this question doesn't get old, and I think now there's a slightly better answer than using wmic (on win7 onwards).

for /F "tokens=1,2,3 delims=_" %%i in ('PowerShell -Command "& {Get-Date -format "MM_dd_yyyy"}"') do (
    set MONTH=%%i
    set DAY=%%j
    set YEAR=%%k
)
echo %MONTH% %DAY% %YEAR%

With powershell Get-Date you can fine-tune the format you want (short,long, numbers,names,etc..). In this example "MM_dd_yyyy" will get you a numeric date with leading zeros in case of single digit months or days

like image 71
Federico Destefanis Avatar answered Sep 19 '22 17:09

Federico Destefanis


This function should work on all regions.

Remarks:

Returns month in text format for MMM regions

@echo off
  call :GetDateIntl year month day 
  echo/Using GetDateIntl you get: %year%-%month%-%day%
goto:eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDateIntl yy mm dd [/A]
:: Returns the current date on any machine with regional-independent settings
:: Arguments:
::   yy = variable name for the year output
::   mm = variable name for the month output
::   dd = variable name for the day output
::   /A = OPTIONAL, removes leading 0 on days/months smaller than 10 (example: 01 becomes 1)
:: Remarks:
::  Will return month in text format in regions with MMM month
::
SETLOCAL ENABLEEXTENSIONS
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
  for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
    set '%%a'=%%i
    set '%%b'=%%j
    set '%%c'=%%k
  )
)
if /I "%'yy'%"=="" set "'yy'=%'aa'%"
if /I "%'yy'%"=="" ( set "'yy'=%'jj'%" & set "'dd'=%'tt'%" )
if %'yy'% LSS 100 set 'yy'=20%'yy'%
endlocal&set %1=%'yy'%&set %4 %2=%'mm'%&set %4 %3=%'dd'%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
like image 36
cyberponk Avatar answered Sep 19 '22 17:09

cyberponk