How to identify the date format used in "DIR" and "%~T"?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

How to identify the date format used in "DIR" and "%~T"?

#1 Post by Aacini » 01 Apr 2015 12:19

Yes, I know that there are several methods to get the current date in a locale independent way. However, I want not the current date, but the dates related to files that show the DIR command and the %~T modifier in FOR replaceable parameters. Is there any way to get such format? I am, talking about the date order (MM/DD/YYYY or DD/MM/YYYY), if the Day Of Week appears before the date, if the year have 4 or 2 digits, etc.

The objective is to calculate how many days old a list of files are, but in a way as fast as possible, so WMIC is out of the question. Also, I prefer to not use JScript/VBS if possible...

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

Re: How to identify the date format used in "DIR" and "%~T"?

#2 Post by Squashman » 01 Apr 2015 13:01

Aacini wrote: Also, I prefer to not use JScript/VBS if possible...

Bummer. I thought Dave's GetTimeStamp.bat was the perfect solution.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to identify the date format used in "DIR" and "%~T"?

#3 Post by aGerman » 01 Apr 2015 13:13

You could use REG QUERY to get the related values in "HKCU\Control Panel\International"
iDate is for the order (https://technet.microsoft.com/en-us/lib ... 78637.aspx)
sDate is for the separator
also have a look at sShortDate

Regards
aGerman

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: How to identify the date format used in "DIR" and "%~T"?

#4 Post by npocmaka_ » 01 Apr 2015 14:46

Yes , the registry is the most convenient option.

...though the options to read it are REGEDIT and REG

REG is not available on all Windows machines by default
REGEDIT require admin permissions or otherwise an ugly pop-up appears (reg query "HKCU\Control Panel\International" does not require admin permissions ) and you'll have a temp reg file.

Here's another a little bit clumsy option (e.g.) :

Code: Select all

w32tm /ntpte 3763314900


w32tm /ntpte and /ntte will print the date in the same format as the one saved in "HKCU\Control Panel\International"

to distinguish which one is the day , year and the month you'll need to pass a date with all the parts different.

( more info can be found here http://stackoverflow.com/questions/1432 ... -with-this )


w32tm is available on every windows system and will not create any temp files and will not require admin permissions.

(Why not jsctript or vbscript? looks like a lot of work with pure batch)

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

Re: How to identify the date format used in "DIR" and "%~T"?

#5 Post by foxidrive » 01 Apr 2015 20:35

Aacini wrote:The objective is to calculate how many days old a list of files are, but in a way as fast as possible, so WMIC is out of the question. Also, I prefer to not use JScript/VBS if possible...


Is it feasible to get the current date from any tool and then compare the terms with the %~t output to determine the format?

Robocopy, Makecab, Mshta can give you the date/time strings also.

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to identify the date format used in "DIR" and "%~T"?

#6 Post by miskox » 02 Apr 2015 03:56

Can this help you?

Code: Select all

@echo off
for /f "tokens=1-4 delims=/.- " %%A in ("%date%") do if %%D!==! (set CurrentDate=20%%C%%B%%A) else (set CurrentDate=20%%D%%C%%B)
for /f "tokens=1-4 delims=:,. " %%f in ("%time%") do set CurrentTime=0%%f%%g%%h%%i
set CurrentDateTime=%CurrentDate:~-8%%CurrentTime:~-8%

echo %CurrentDateTime%

REM gets date/time for current .cmd - replace with your filename
for %%q in (%0) do for /f "tokens=1,2 delims= " %%u in ("%%~tq") do set dateFOR=day %%u&&set timeFOR=%%v:00,00

for /f "tokens=1-4 delims=/.- " %%A in ("%dateFOR%") do if %%D!==! (set CurrentDateFOR=20%%C%%B%%A) else (set CurrentDateFOR=20%%D%%C%%B)
for /f "tokens=1-4 delims=:,. " %%f in ("%timeFOR%") do set CurrentTimeFOR=0%%f%%g%%h%%i
set CurrentDateTimeFOR=%CurrentDateFOR:~-8%%CurrentTimeFOR:~-8%

echo %CurrentDateTimeFOR%



Output:

Code: Select all

c:\>progname.cmd
2015040211543448
2015040211540000

c:\>


This code should be tested on different regional versions. Should work for DD-MM-YYYY and MM-DD-YYYY. Probably will not work for YYYY-MM-DD or YYYY-DD-MM?

Saso

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: How to identify the date format used in "DIR" and "%~T"?

#7 Post by jwoegerbauer » 02 Apr 2015 06:44

miskox wrote:Can this help you?

Code: Select all

@echo off
for /f "tokens=1-4 delims=/.- " %%A in ("%date%") do if %%D!==! (set CurrentDate=20%%C%%B%%A) else (set CurrentDate=20%%D%%C%%B)
for /f "tokens=1-4 delims=:,. " %%f in ("%time%") do set CurrentTime=0%%f%%g%%h%%i
set CurrentDateTime=%CurrentDate:~-8%%CurrentTime:~-8%

echo %CurrentDateTime%

REM gets date/time for current .cmd - replace with your filename
for %%q in (%0) do for /f "tokens=1,2 delims= " %%u in ("%%~tq") do set dateFOR=day %%u&&set timeFOR=%%v:00,00

for /f "tokens=1-4 delims=/.- " %%A in ("%dateFOR%") do if %%D!==! (set CurrentDateFOR=20%%C%%B%%A) else (set CurrentDateFOR=20%%D%%C%%B)
for /f "tokens=1-4 delims=:,. " %%f in ("%timeFOR%") do set CurrentTimeFOR=0%%f%%g%%h%%i
set CurrentDateTimeFOR=%CurrentDateFOR:~-8%%CurrentTimeFOR:~-8%

echo %CurrentDateTimeFOR%



Output:

Code: Select all

c:\>progname.cmd
2015040211543448
2015040211540000

c:\>


This code should be tested on different regional versions. Should work for DD-MM-YYYY and MM-DD-YYYY. Probably will not work for YYYY-MM-DD or YYYY-DD-MM?

Saso


This works well on Windows 8.1 Core "German" version.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to identify the date format used in "DIR" and "%~T"?

#8 Post by Aacini » 02 Apr 2015 17:08

Thanks a lot everybody for your answers! I started to did some tests with the data provided and it seems that the pure Batch method is not just hard, but it also may fail in certain particular cases. I think I will use the Batch-JScript solution finally...

Antonio

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

Re: How to identify the date format used in "DIR" and "%~T"?

#9 Post by foxidrive » 02 Apr 2015 19:33

I borrowed penpen's code to create a c# executable and this seems to work in the single location I have tried it on. :D

This is what it shows here: DateFormat="dd/MM/yyyy", TimeFormat="HH:mm"

Code: Select all

// // >nul 2> nul & @goto :main
/*
 * Author of this routine to compile a c# script: Ulf Schneider aka penpen
 * He lives at www.dostips.com and it was borrowed from CmdFont.cs.bat
 */

/*
:main
   @echo off
   setlocal
   cls

   set "csc="

   pushd "%SystemRoot%\Microsoft.NET\Framework"
   for /f "tokens=* delims=" %%i in ('dir /b /o:n "v*"') do (
      dir /a-d /b "%%~fi\csc.exe" >nul 2>&1 && set "csc="%%~fi\csc.exe""
   )
   popd

   if defined csc (
rem      echo most recent C#.NET compiler located in:
rem      echo %csc%.
   ) else (
      echo C#.NET compiler not found.
      goto :eof
   )
   
 :retry   
 set "name=%temp%\report-format-%random%"   
 if exist "%name%.*" goto :retry
   
   for %%a in ("%~dpn0") do for %%b in ("%%~dpna") do (
rem      %csc% /?
      del "%name%.*" 2>nul
      %csc% /nologo /optimize /warnaserror /nowin32manifest /unsafe /debug- /target:exe /out:"%name%.exe" "%~f0"
   )

 "%name%.exe">"%name%.bat"
 call "%name%.bat"
 del "%name%.*" 2>nul
 echo DateFormat="%DateFormat%", TimeFormat="%TimeFormat%"
 pause
   exit /B
*/

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string DateFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;
      string TimeFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortTimePattern;
         Console.WriteLine("set DateFormat="+DateFormat);
         Console.WriteLine("set TimeFormat="+TimeFormat);         
   }
}


Post Reply