windowsdatebatch-filecreation

Write file creation date to a text file, in a single line


This may be easy but it's driving me nuts (2 whole hours spent already! I've tried dozens of things!).

I just need a single line bat script that reads a given file's creation date and writes it to a text file.

I've tried using the dir /T:C command, but it writes way too much info. For example:

dir /T:C "E:\TEST\test.mkv" >"E:\TEST\test.txt"

Writes this in the text.txt file:

Le volume dans le lecteur E s'appelle xxxxxxxx
Le numéro de série du volume est xxxxxxxx

Répertoire de E:\TEST

11/03/2018  20:45     1 947 028 131 test.mkv
               1 fichier(s)    1 947 028 131 octets
               0 Rép(s)  64 368 631 808 octets libres

And I'm only interested in the "11/03/2018 20:45" part.

EDIT: I could also settle for the previous dir /T:C syntax if we could add a bit of code to keep only line #6 of the output and write it to the same text file.


Solution

  • Here is my suggestion, which technically does and doesn't use only one line in the batch file, and uses VBScript.

    <!-- :
    @("%__AppDir__%cscript.exe" //NoLogo "%~f0?.wsf">"E:\TEST\test.txt")&Exit /B
    -->
    <Job><Script Language="VBScript">
    Set o=CreateObject("Scripting.FilesystemObject")
    Set f=o.GetFile("E:\TEST\test.mkv")
    s=Left(f.DateCreated,Len(f.DateCreated)-3)
    </Script></Job>
    

    To remain on topic however here are some exactly one line batch file examples.

    This method leverages powershell.exe:

    @"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "'{0:dd/MM/yyy}' -F(Get-Date(GI 'E:\TEST\test.mkv').CreationTime)">"E:\TEST\test.txt"
    

    And this one uses WMIC.exe:

    @For /F %%I In ('WMIC DataFile Where Name^="E:\\TEST\\test.mkv" Get CreationDate 2^>NUL^|Find "."')Do @Set "d=%%~nI"&(Call Echo %%d:~6,2%%/%%d:~4,2%%/%%d:~,4%% %%d:~8,2%%:%%d:~10,2%%)>"E:\TEST\test.txt"