Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting file Date+Time with Y M D H M S ?

63 views
Skip to first unread message

Dr J R Stockton

unread,
Jul 8, 2017, 11:17:52 AM7/8/17
to

I have the following code, which shows me the local date-time of
TRY.LOG as YYYY-MM-DD HH:MM - but I want YYYY-MM-DD HH:MM:SS .

if %%f==TRY.LOG (
echo Found TRY.LOG %%~tf, new files follow :-
)

Any suggestions that are not too complex or slow?

--
(c) John Stockton, near London, UK. Using Google, no spell-check. |
Mail: J.R.""""""""@physics.org |

pro...@berkeley.edu

unread,
Jul 8, 2017, 2:15:52 PM7/8/17
to
Search for 'NTFS filetime' thread by Frank Westlake in this newsgroup.

JJ

unread,
Jul 8, 2017, 3:44:42 PM7/8/17
to
On Sat, 8 Jul 2017 08:17:50 -0700 (PDT), Dr J R Stockton wrote:
> I have the following code, which shows me the local date-time of
> TRY.LOG as YYYY-MM-DD HH:MM - but I want YYYY-MM-DD HH:MM:SS .
>
> if %%f==TRY.LOG (
> echo Found TRY.LOG %%~tf, new files follow :-
> )
>
> Any suggestions that are not too complex or slow?

This is the least complex and least slow thing I could think of.
Note: the code assume that the current system date+time format is set to
MM/DD/YYYY hh:mm:ss.

@echo off
setlocal enabledelayedexpansion
rem filename can have relative or absolute path
set filename=thefile.dat
>"%temp%\temp.vbs" echo set fs = createobject("scripting.filesystemobject")
>>"%temp%\temp.vbs" echo set f = fs.getfile("%filename%")
>>"%temp%\temp.vbs" echo wscript.echo f.datelastmodified
for /f "tokens=1,2,3,4 delims=/ " %%A in ('cscript//nologo "%temp%\temp.vbs"') do (
echo %%C-%%B-%%A %%D
)

Dr J R Stockton

unread,
Jul 8, 2017, 4:54:50 PM7/8/17
to
On Saturday, July 8, 2017 at 7:15:52 PM UTC+1, pro...@berkeley.edu wrote:
> On Saturday, July 8, 2017 at 8:17:52 AM UTC-7, Dr J R Stockton wrote:
> > I have the following code, which shows me the local date-time of
> > TRY.LOG as YYYY-MM-DD HH:MM - but I want YYYY-MM-DD HH:MM:SS .
> >
> > if %%f==TRY.LOG (
> > echo Found TRY.LOG %%~tf, new files follow :-
> > )
> >
> > Any suggestions that are not too complex or slow?


> Search for 'NTFS filetime' thread by Frank Westlake in this newsgroup.

Found, but too long and complex.

I noticed that it contained something like part of Zeller's Congruence, and then that it was only part like.

But, readers, be aware that code for the Congruence written in the years 1xxx may only have been tested for dates in 1xxx - and is likely to go wrong (because of negative-number mod 7) for some dates in the first fifth of 20xx - so test for all possible dates (a 400 year span will always suffice).

Dr J R Stockton

unread,
Jul 8, 2017, 5:36:25 PM7/8/17
to
My systems are all set to ISO date/time format, not FFF, so
f.datelastmodified gives the correct string directly.

petu...@googlemail.com

unread,
Jul 9, 2017, 8:29:58 AM7/9/17
to
You could use WMIC, whose speed can be acceptable as long as it receives a full file path as its input file specification.

The following example takes a single input as a filename, best implemented by dragging and dropping a file onto the script:

::----- START -----
@ECHO OFF
SET "_I=%~1"
IF NOT DEFINED _I (GOTO :EOF) ELSE IF NOT EXIST "%~1" GOTO :EOF
SET "_M="
FOR /F "SKIP=1 DELIMS=" %%A IN ('WMIC DATAFILE WHERE "NAME='%_I:\=\\%'"^
GET LASTMODIFIED 2^>NUL') DO FOR %%B IN (%%~nA) DO SET "_M=%%B"
IF NOT DEFINED _M (GOTO :EOF
) ELSE SET "_M=%_M:~,4%-%_M:~4,2%-%_M:~6,2% %_M:~8,2%:%_M:~10,2%:%_M:~12,2%"
ECHO %_M%
PAUSE
::------ END ------

JJ

unread,
Jul 9, 2017, 9:46:24 AM7/9/17
to
On Sat, 8 Jul 2017 14:36:23 -0700 (PDT), Dr J R Stockton wrote:
>
> My systems are all set to ISO date/time format, not FFF, so
> f.datelastmodified gives the correct string directly.

So, it's good then. I believe you can modify the code yourself.

BTW... What's "FFF"?

petu...@googlemail.com

unread,
Jul 9, 2017, 3:38:21 PM7/9/17
to
I suppose you could even use your favoured JScript directly from a batch file too:

::----- getLMDT.cmd -----
@if (@X)==(@Y) @end /*

@Echo Off
Set "_I=%~1"
If Not Defined _I (GoTo :EOF) Else If Not Exist "%~1" GoTo :EOF
CScript //NoLogo //E:JScript "%~f0" %*
Pause
Exit/B

*/
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var dateLM = new Date(objFSO.getFile(WScript.Arguments(0)).dateLastModified);
WScript.Echo (""
+ ("" + (10000 + dateLM.getYear() )).substring(1)
+ "-" + ("" + (100 + (dateLM.getMonth() + 1))).substring(1)
+ "-" + ("" + (100 + dateLM.getDate() )).substring(1)
+ " " + ("" + (100 + dateLM.getHours() )).substring(1)
+ ":" + ("" + (100 + dateLM.getMinutes() )).substring(1)
+ ":" + ("" + (100 + dateLM.getSeconds() )).substring(1)
);

Dr J R Stockton

unread,
Jul 9, 2017, 6:31:03 PM7/9/17
to
Partially. I don't want to write a new little file every time,
so made a general one with arguments. It does what I asked for,
but I really need to concatenate the arguments with the date-time,
and I have an as-yet undiagnosed difficulty there.


> BTW... What's "FFF"?

Fred Flintstone Format - M/D/Y dates, used in certain countries
that ignore modern international standards such as ISO 8601.

B00ze

unread,
Jul 10, 2017, 11:58:33 PM7/10/17
to
On 2017-07-09 18:31, Dr J R Stockton <J.R.St...@physics.org> wrote:

>> BTW... What's "FFF"?
>
> Fred Flintstone Format - M/D/Y dates, used in certain countries
> that ignore modern international standards such as ISO 8601.

I refer to that as the Yoda date format, you know: May The With You Be
The Force ;-)

--
! _\|/_ Sylvain / B00...@hotmail.com
! (o o) Member:David-Suzuki-Fdn/EFF/Red+Cross/SPCA/Planetary-Society
oO-( )-Oo LISP: Lots of insignificant single parentheses.

dr.j.r....@gmail.com

unread,
Jul 11, 2017, 6:31:29 PM7/11/17
to
On Sunday, 9 July 2017 13:29:58 UTC+1, petu...@googlemail.com wrote:
> On Saturday, 8 July 2017 16:17:52 UTC+1, Dr J R Stockton wrote:
> > I have the following code, which shows me the local date-time of
> > TRY.LOG as YYYY-MM-DD HH:MM - but I want YYYY-MM-DD HH:MM:SS .
> >
> > if %%f==TRY.LOG (
> > echo Found TRY.LOG %%~tf, new files follow :-
> > )
> >
> > Any suggestions that are not too complex or slow?


> You could use WMIC, whose speed can be acceptable as long as it receives a full file path as its input file specification.
>
> The following example takes a single input as a filename, best implemented by dragging and dropping a file onto the script:


I need it in the middle of an existing batch file. TRY.LOG is a zero-length file used as a marker in date-sorted directory output, re-created after each run and created at the beginning by "IF NOT EXIST TRY.LOG ...", so its checks are not needed. The working code I've derived from yours follows yours (one long line is wrapped here in each). It uses %CD% because the code, and more, is surrounded by PUSHD %Diry% and POPD.


> ::----- START -----
> @ECHO OFF
> SET "_I=%~1"
> IF NOT DEFINED _I (GOTO :EOF) ELSE IF NOT EXIST "%~1" GOTO :EOF
> SET "_M="
> FOR /F "SKIP=1 DELIMS=" %%A IN ('WMIC DATAFILE WHERE "NAME='%_I:\=\\%'"^
> GET LASTMODIFIED 2^>NUL') DO FOR %%B IN (%%~nA) DO SET "_M=%%B"
> IF NOT DEFINED _M (GOTO :EOF
> ) ELSE SET "_M=%_M:~,4%-%_M:~4,2%-%_M:~6,2% %_M:~8,2%:%_M:~10,2%:%_M:~12,2%"
> ECHO %_M%
> PAUSE
> ::------ END ------


SET _I=%CD%\TRY.LOG
FOR /F "SKIP=1 DELIMS=" %%A IN ('WMIC DATAFILE WHERE "NAME='!_I:\=\\!'"^
GET LASTMODIFIED') DO FOR %%B IN (%%~nA) DO SET "_M=%%B "
IF NOT DEFINED _M (set "_M=WMIC datetime fault"
) ELSE SET
"_M=!_M:~,4!-!_M:~4,2!-!_M:~6,2! !_M:~8,2!:!_M:~10,2!:!_M:~12,2!"
ECHO Seen TRY.LOG !_M!, newer files follow it :-

I've not yet understood the "FOR %%B IN (%%~nA) DO" .
The "NOT DEFINED" case should never be able to occur.



I was not wanting to use an auxiliary program here; but I have since realised and tested that the following command line works, where COLS is a Pascal/Delphi program of mine (line has wrapped).

Prompt>wmic datafile where "name='c:\\homepage\\try.log'" get lastmodified
| COLS ^&2 'Prefix * 0-4 '- 5-6 '- 7-8 * 9-10 ': 11-12 ': 13-14 * 'postfix .

gives

Prefix 2017-07-11 22:08:52 postfix


Thanks.
0 new messages