Page 1 of 1

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

Posted: 01 Apr 2015 12:19
by Aacini
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...

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

Posted: 01 Apr 2015 13:01
by Squashman
Aacini wrote: Also, I prefer to not use JScript/VBS if possible...

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

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

Posted: 01 Apr 2015 13:13
by aGerman
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

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

Posted: 01 Apr 2015 14:46
by npocmaka_
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)

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

Posted: 01 Apr 2015 20:35
by foxidrive
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.

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

Posted: 02 Apr 2015 03:56
by miskox
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

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

Posted: 02 Apr 2015 06:44
by jwoegerbauer
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.

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

Posted: 02 Apr 2015 17:08
by Aacini
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

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

Posted: 02 Apr 2015 19:33
by foxidrive
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);         
   }
}