DIR output format depends on Region and Language settings.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

DIR output format depends on Region and Language settings.

#1 Post by thefeduke » 12 Mar 2016 17:55

[Edited: Topic name change for clarification.]
My batch script failed on a different system using 'FOR /F' processing DIR output.
I tested on Windows 7 (32bit and 64bit) and Windows 8.0
My victim is running Windows 10 over 8.1. Here is a excerpt of my correspondence:

Code: Select all

 . . . it's always a simple one as you know.
There is indeed a difference and it is in the default output of the 'DIR' command.

Your test run showed:
 Directory of E:\Temp\Spflite_Dir1
11-Mar-16  13:47    <DIR>          Dir2
11-Mar-16  13:47    <DIR>          Dir3

We on the old systems got:
 Directory of C:\Users\Zani\AppData\Local\Temp\ForCmdDiag_Dir1
03/12/2016  01:02 PM    <DIR>          Dir2
03/12/2016  01:02 PM    <DIR>          Dir3

So, without the AM/PM designation, I was never going to find the '<DIR>' token and all of the Directory processing goes kaput.

So basic, but who knew they would change that, watching from my island. At least, now I know what to do.  I just have to figure out how.

I don't know if this date/time format change came with 8.1 or 10, but when MS did this, can anyone let me know if they added a switch to 'DIR' for compatibility as they have in the past? That would certainly save some re-coding if I could catch this at the source of that data instead of manipulating it.

Thanks,
John A.
Last edited by thefeduke on 12 Mar 2016 21:12, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: DIR output format change between Windows versions

#2 Post by ShadowThief » 12 Mar 2016 18:15

This is a setting that can be changed in the Control Panel, under "Clock, Language, and Region."

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

Re: DIR output format change between Windows versions

#3 Post by foxidrive » 12 Mar 2016 18:27

Both the date format and time format are dependent on region and the settings changed by the user.

This is a reliable method to get a known date and time string

Code: Select all

@echo off
rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.
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 "MS=%dt:~15,3%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"

echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "date-time=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%"
echo date-time: "%date-time%"
pause

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: DIR output format change between Windows versions

#4 Post by ShadowThief » 12 Mar 2016 19:07

I don't think they're looking for a timestamp, just that the system time being in 24-hour format is messing up their token numbering.

It would be helpful to know what the script is supposed to do. Since you mentioned that you're looking for the <DIR> tag, you may want to use

Code: Select all

dir /ad
to list all of the directories in the current directory. (Or dir /a-d to get the non-directory files.)

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

Re: DIR output format change between Windows versions

#5 Post by Aacini » 12 Mar 2016 20:23

Well, you may use my StdDate.exe auxiliary program to perform several date-related operations independant from locale settings, or using the same format of locale settings. For example, running StdDate with no parameters returns the current date in the same 3 lines in all computers:

Code: Select all

C:\> StdDate
YYYY=2016
MM=12
DD=03

You may also give StdDate a date in the current locale format; for example:

Code: Select all

C:\> StdDate 12/03/2016
YYYY=2016
MM=03
DD=12

This example run in my computer, that use DD/MM/YYYY date format, but if you execute "StdDate 11-Mar-16" or "StdDate 03/12/2016" or any other format, this program will give a correct result as long as the given date use the same locale format.

Besides, StdDate program returns the Julian Day Number of the date in errorlevel, so you may use it to know how many days old a file is; just take today's JDN and subtract from it the file's JDN.

You may also know a future or past date; just add or subtract a number of days to a JDN and pass the result to StdDate. For example, which date will be 30 days from now on?

Code: Select all

C:\> StdDate
YYYY=2016
MM=03
DD=12

C:\> set /A future=%errorlevel%+30
2457490
C:\> StdDate %future%
11/04/2016

You may download StdDate.exe auxiliary program from this site, but foxidrive may give you another link that allows a simpler download...

Antonio

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

DIR output format changes with 'Region and Language' time settings

#6 Post by thefeduke » 12 Mar 2016 20:59

ShadowThief wrote:This is a setting that can be changed in the Control Panel, under "Clock, Language, and Region."

I retract the way that I stated the question. Thank you, Shadowthief. If I can (and did) change this setting, then supposedly, so can any 'client'. I used 'Region and Language' settings on Win 7.

So, if I write a script that uses DIR output, I cannot expected that this setting has not been customized. I am still in the dark as to whether the default is the same or not, but it should not matter, if I wish to support this properly.

I shall change the topic name appropriately.

Thanks for your suggestions. I'll clean up and post a little work-around using 'DIR /ad'

John A.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: DIR output format change between Windows versions

#7 Post by thefeduke » 12 Mar 2016 23:08

foxidrive wrote:Both the date format and time format are dependent on region and the settings changed by the user.

Bang on!
Thanks, as well for your and Antonio's date and time tips. I am acquainted with some of your good work.

Here is the code that should solve what I still see as a support problem. I can't expect anyone to change their settings to suit my coding. The DIR command can be replaced by any 'DIR /AD' of choice but now runs against the Current Directory.

Code: Select all

   @Echo Off
:ForDIRtime Version 1.0 03/12/2016 process DIR output with FOR /F with two time formats
::  Author - John Andrejsons - accepting PM to user: thefeduke at DOStips forum
::  acknowledges code and influences from DOStips.com and its forum
    SetLOCAL EnableDelayedExpansion
    Set Ctr=0
    Echo.
    For /f "usebackq tokens=1-4,* delims= " %%H in (
        `Dir %cd:~,-1%*`
    ) do (
rem.    Echo.Dir text: H=%%H I=%%I J=%%J K=%%K L=%%L
        IF /I "%%H" EQU "Directory" (
            Set "ForCmdDIR=%%J %%K %%L"
            IF .%%L EQU . Set "ForCmdDIR=%%J %%K"
            IF .%%K EQU . Set "ForCmdDIR=%%J"
        )
        IF /I "%%K" EQU "<DIR>" (
            Set /A "Ctr+=1"
            If .!Ctr! EQU .1 Echo.Times were found using format: '%%I %%J'.
            Echo.Found %%K '%%L' in '!ForCmdDIR!'
        )
        IF /I "%%J" EQU "<DIR>" (
            Set /A "Ctr+=1"
            If .!Ctr! EQU .1 Echo.Times were found using format: '%%I'.
            Echo.Found %%J '%%K' in '!ForCmdDIR!'
        )
    )

    Exit /B

:: SAMPLE OUTPUT:

:: Times were found using format: '07:56 PM'.
:: Found <DIR> 'Programs' in 'J:\'

:: J:\Programs>fordirtime

:: Times were found using format: '19:56'.
:: Found <DIR> 'Programs' in 'J:\'

John A.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: DIR output format depends on Region and Language settings.

#8 Post by penpen » 13 Mar 2016 07:37

You could read the actual time and date formatting from the registry.

Here is an example:
http://www.dostips.com/forum/viewtopic.php?p=43580#p43580
Note the two possible sources (and their handling):
- "HKCU\Control Panel\International", and
- "HKU\.DEFAULT\Control Panel\International".

The registry key holding the value, which clock (12h/24h) actually is in use is "iTime":
https://technet.microsoft.com/en-us/library/cc784925(v=ws.10).aspx


penpen

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

Re: DIR output format depends on Region and Language settings.

#9 Post by foxidrive » 13 Mar 2016 08:22

It's always good to see someone bring up a task, because then the brain is free to find different ways to do it.
I did so here too - thanks for the entertainment, John, it was sorely needed. :)

There's three bits - the middle bit just shows 12 or 24 hour time and the lower bit displays the folders.
Your code hasn't taken spaces into account, John, and the foldernames show incorrectly.

Code: Select all

@echo off
:: detect 12 or 24 hour time format
for /f "tokens=1-5" %%a in ('dir "%windir%\system.ini" /a-d') do (
  if /i "%%d"=="system.ini" set f=24&set t=%%b
  if /i "%%e"=="system.ini" set f=12&set t=%%b %%c
)
 echo %f% hour time format detected: %t%
 echo(

@echo off
dir \ /ad |findstr /r /c:":[0-9][0-9] [ ]">nul && (set t=24) || (set t=12)
 echo found %t% hour time format
 echo(

for /d %%a in (*) do echo found %%a in "%cd%"
 echo(
pause & goto :EOF

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: DIR output format depends on Region and Language settings.

#10 Post by thefeduke » 13 Mar 2016 13:11

foxidrive wrote:Your code hasn't taken spaces into account, John, and the foldernames show incorrectly.
Thanks for the heads-up. '%%K' does not pick up the rest like %%L did. I'll make it '%%K %%L' to allow for the shift.
Thanks for the lead penpen. I always want to check out those registry pointers, in principle, but that education keeps staying on the back burner. Meanwhile, some of Foxi's code is crying out for a marriage of convenience.

It gives me great joy, Foxi, to keep you entertained. A change like this attracted me:

Code: Select all

  if /i "%%d"=="system.ini" set f=24&set t=%%b     &Set "<DIR>=J" &Set SubDIR=K %%L 
  if /i "%%e"=="system.ini" set f=12&set t=%%b %%c &Set "<DIR>=K" &Set SubDIR=L
to mate with my code in a previous post:

Code: Select all

        IF /I "%%%<DIR>%" EQU "<DIR>" (
            Set /A "Ctr+=1"
            If .!Ctr! EQU .1 Echo.In this test, the DIR command is outside of 'FOR'.
            Echo.Found %%%SubDIR% in '!ForCmdDIR!'
        )

John A.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: DIR output format depends on Region and Language settings.

#11 Post by thefeduke » 13 Mar 2016 14:04

Thank, you for your tips. I am pretty settled on this technique:

Code: Select all

   @Echo Off
:: READ ME - If this is distributed as a .txt file, rename to a .bat file after download.
:FORDIRtime Version 1.2 03/13/2016 Incorporate Foxidrive's date style detection
:: http://www.dostips.com/forum/viewtopic.php?p=45750#p45750
:: Post subject: Re: DIR output format depends on Region and Language settings
:FORDIRdiag Version 1.1 03/12/2016 Add comparison display and ForCmdDIR trace
:FORDIRdiag Version 1.0 03/11/2016 Shoot the program folder bug.
    SetLOCAL EnableDelayedExpansion
    Echo.

    If Not Exist "%Temp%\%~n0_Dir1"      MkDir "%Temp%\%~n0_Dir1"
    If Not Exist "%Temp%\%~n0_Dir1\Dir2" MkDir "%Temp%\%~n0_Dir1\Dir2"
    If Not Exist "%Temp%\%~n0_Dir1\Dir3" MkDir "%Temp%\%~n0_Dir1\Dir3"
    If Not Exist "%Temp%\%~n0_Dir1\Dir 4" MkDir "%Temp%\%~n0_Dir1\Dir 4"
    If Exist "%Temp%\%~n0_Dir1\%~n0_Dir.txt" Erase "%Temp%\%~n0_Dir1\%~n0_Dir.txt"
   (Dir /ad "%Temp%\%~n0_Dir1\Dir*")>"%Temp%\%~n0_Dir1\%~n0_Dir.txt"
        Type "%Temp%\%~n0_Dir1\%~n0_Dir.txt"
    Echo.
    Echo.===== that was the input =====
    Echo.
 
:: detect 12 or 24 hour time format
for /f "tokens=1-5" %%a in ('dir "%windir%\system.ini" /a-d') do (
  if /i "%%d"=="system.ini" set f=24&set t=%%b     &Set "<DIR>=J" &Set SubDIR=K %%L
  if /i "%%e"=="system.ini" set f=12&set t=%%b %%c &Set "<DIR>=K" &Set SubDIR=L
)
 echo %f% hour time format detected: %t%
 echo(
 echo All above message display would normally be suppressed.
 echo(
rem.for /d %%a in (*) do echo found %%a in "%cd%"

    Set Ctr=0
    For /f "usebackq tokens=1-4,* delims= " %%H in (
        `Type "%Temp%\%~n0_Dir1\%~n0_Dir.txt"`
    ) do (
        IF /I "%%H" EQU "Directory" (
            Set "ForCmdDIR=%%J %%K %%L"                                                            r
            IF .%%L EQU . Set "ForCmdDIR=%%J %%K"
            IF .%%K EQU . Set "ForCmdDIR=%%J"
        )
        IF /I "%%%<DIR>%" EQU "<DIR>" (
            Set /A "Ctr+=1"
            If .!Ctr! EQU .1 Echo.In this demo, the DIR command precedes the 'FOR /F' command.
            Echo.Found %%%SubDIR% in '!ForCmdDIR!'
        )
        Rem. The real work of processing the folder happens here.
    )

    Exit /B

John A.

Post Reply