2

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    `For /f "usebackq tokens=2 delims==" %A in (\`wmic datafile where "name='c:\\windows\\notepad.exe'" get creationdate /format:list\`) Do Echo %A` –  Jan 11 '20 at 18:37
  • Thanks Mark. That seems to work... except that since your syntax uses apostrophes instead of double quotes, now I have a problem with paths containing apostrophes. 'c:\\windows\\notepad.exe' works well, but how about 'c:\\test\\L'amour.mkv' ? I have tried escaping the apostrophes ( 'c:\\test\\L^'amour.mkv' ), but it doesn't work... – Jose Hidalgo Jan 11 '20 at 20:26
  • See `for /?`. `UseBackq` changes what characters do. You have to suit it to your input data. –  Jan 11 '20 at 20:38
  • In the `wmic` command line, try to remove the outer quotes and then to replace the apostrophes by quotes... – aschipfl Jan 12 '20 at 06:59

2 Answers2

1

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"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks Compo. I especially like your last suggestion. The problem I have now is that your variable "Name" containing the path to the file is defined with apostrophes instead of double quotes. And some of my paths do have apostrophes. I have tried escaping them (^') but it doesn't seem to work... – Jose Hidalgo Jan 11 '20 at 20:29
  • Updated the WMIC version, @JoseHidalgo. _I'd be interested to know how the other example(s) work for you too._ – Compo Jan 11 '20 at 21:00
  • Thanks @Compo, but I can't make your updated WMIC version work yet. I've only replaced both of your paths with my own paths (the first one with double backslashes and the second one with single backslashes), but the text file isn't created. – Jose Hidalgo Jan 11 '20 at 21:48
  • Turns out your WMIC version works, but only for very basic paths (e.g. E:\\TEST\\test.mkv). Maybe it doesn't like long paths with spaces and stuff? – Jose Hidalgo Jan 11 '20 at 21:56
  • Here's an example of a path that doesn't work: "E:\\VIDEO\\FILMS\\Adventureland (2009) - 1080p\\Adventureland (2009) 1080p BluRay x265.mkv" – Jose Hidalgo Jan 11 '20 at 22:00
  • @Jose, your question was specific, you've now changed your filename twice since I answered. I have edited it again, but please be aware that I do not have a PC and am therefore unable to test the vast majority of what I post on this site. _(your filename was not specified as 'could be anything')_ BTW, the `Dir` output is not consistent accross different Users/PC's/Locales/Languages, hence the reason I haven't offered a method of just parsing your specific output, _as it may not match that of future readers_. – Compo Jan 11 '20 at 22:15
  • I'm very sorry. I just assumed that any offered solution would be valid for any path in my hard drive. I guess I was wrong. I'm happy to announce that your updated line works for most paths. I still had trouble with paths containing commas, but I treated them manually, so ultimately it worked. Thanks for providing a working solution! – Jose Hidalgo Jan 12 '20 at 04:24
  • Done now. Thanks again for your help. – Jose Hidalgo Jan 12 '20 at 15:37
0

Use this:

@For /f "Tokens=1,2 Delims= " %%A in ('dir /T:C "E:\TEST\test.mkv" ^| Find /I "Test.mkv"') Do Echo %%A %%B >output.txt
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Thanks. It doesn't seem to work at the moment. I may need to state that the single line itself must be inside a .bat script, not from the command line. Maybe it changes something? – Jose Hidalgo Jan 11 '20 at 18:09