LoginSignup
4
3

More than 3 years have passed since last update.

コマンドプロンプトでファイルの更新日時をミリ秒単位まで取得する

Last updated at Posted at 2020-04-30

概要

コマンドプロンプトから dir コマンドを実行するとファイルの一覧とともにファイルの更新日時を取得できますが、秒以下の詳細時刻を確認することができません。

本記事ではコマンドプロンプトからワンライナーで更新日時のミリ秒単位まで取得する方法をご紹介します。

ミリ秒単位まで取得する方法

コマンドプロンプトで更新日時を取得したいフォルダーまでカレントを移動し、以下のコマンドを実行します。

PowerShell "Get-ChildItem -Recurse | Select Name, Length, Attributes, @{Name=\"LastWriteTime\"; Expression={$_.LastWriteTime.ToString(\"yyyy/MM/dd HH:mm:ss.ffffff\")}}"

コマンドを実行すると、ファイル名、サイズ、属性、更新日時(ミリ秒単位)が取得できます。

C:\Windows\Temp>PowerShell "Get-ChildItem -Recurse | Select Name, Length, Attributes, @{Name=\"LastWriteTime\"; Expression={$_.LastWriteTime.ToString(\"yyyy/MM/dd HH:mm:ss.ffffff\")}}"

Name                                                                                     Length  Attributes LastWriteTime
----                                                                                     ------  ---------- -------------
DELD8CB.tmp                                                                              881968     Archive 2020/03/06 11:07:24.821173
DELD8DB.tmp                                                                              228352     Archive 2020/03/03 07:23:04.000000
DELD8EB.tmp                                                                              81920      Archive 2017/11/18 14:00:42.000000

更新日時の出力フォーマットは yyyy/MM/dd HH:mm:ss.ffffff の部分を変更することでカスタマイズできます。

その他、列を追加することで更新日時に加えて作成日時も同時に取得することができます。
以下の例では CreationTime を追加し、フォーマットは更新日時と同じものを指定しています。

PowerShell "Get-ChildItem -Recurse | Select Name, Length, Attributes, @{Name=\"LastWriteTime\"; Expression={$_.LastWriteTime.ToString(\"yyyy/MM/dd HH:mm:ss.ffffff\")}}, @{Name=\"CreationTime\"; Expression={$_.CreationTime.ToString(\"yyyy/MM/dd HH:mm:ss.ffffff\")}}"

コマンドを実行すると、ファイル名、サイズ、属性、更新日時と作成日時(ミリ秒単位)が取得できます。

C:\Windows\Temp>PowerShell "Get-ChildItem -Recurse | Select Name, Length, Attributes, @{Name=\"LastWriteTime\"; Expression={$_.LastWriteTime.ToString(\"yyyy/MM/dd HH:mm:ss.ffffff\")}}, @{Name=\"CreationTime\"; Expression={$_.CreationTime.ToString(\"yyyy/MM/dd HH:mm:ss.ffffff\")}}"

Name          : DELD8CB.tmp
Length        : 881968
Attributes    : Archive
LastWriteTime : 2020/03/06 11:07:24.821173
CreationTime  : 2020/03/06 11:07:40.414842

Name          : DELD8DB.tmp
Length        : 228352
Attributes    : Archive
LastWriteTime : 2020/03/03 07:23:04.000000
CreationTime  : 2020/03/03 07:23:04.000000

Name          : DELD8EB.tmp
Length        : 81920
Attributes    : Archive
LastWriteTime : 2017/11/18 14:00:42.000000
CreationTime  : 2017/11/18 14:00:42.000000

ファイル一覧は必要に応じてファイル名でソートすることができます。
ソートしたい場合は末尾に | Sort-Object Name を付加してコマンドを実行します。

PowerShell "Get-ChildItem -Recurse | Select Name, Length, Attributes, @{Name=\"LastWriteTime\"; Expression={$_.LastWriteTime.ToString(\"yyyy/MM/dd HH:mm:ss.ffffff\")}} | Sort-Object Name"

ファイル一覧に出力する列として指定可能なプロパティは Get-Member コマンドレットから確認できます。

PowerShell "Get-ChildItem | Get-Member -MemberType property"
C:\Windows\Temp>PowerShell "Get-ChildItem | Get-Member -MemberType property"

   TypeName: System.IO.DirectoryInfo

Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   datetime CreationTime {get;set;}
CreationTimeUtc   Property   datetime CreationTimeUtc {get;set;}
Exists            Property   bool Exists {get;}
Extension         Property   string Extension {get;}
FullName          Property   string FullName {get;}
LastAccessTime    Property   datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property   datetime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   datetime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   datetime LastWriteTimeUtc {get;set;}
Name              Property   string Name {get;}
Parent            Property   System.IO.DirectoryInfo Parent {get;}
Root              Property   System.IO.DirectoryInfo Root {get;}


   TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   datetime CreationTime {get;set;}
CreationTimeUtc   Property   datetime CreationTimeUtc {get;set;}
Directory         Property   System.IO.DirectoryInfo Directory {get;}
DirectoryName     Property   string DirectoryName {get;}
Exists            Property   bool Exists {get;}
Extension         Property   string Extension {get;}
FullName          Property   string FullName {get;}
IsReadOnly        Property   bool IsReadOnly {get;set;}
LastAccessTime    Property   datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property   datetime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   datetime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   datetime LastWriteTimeUtc {get;set;}
Length            Property   long Length {get;}
Name              Property   string Name {get;}

参考情報

従来の dir コマンドで取得した場合

秒以下の詳細時刻は確認できません。

C:\Windows\Temp>dir
 ドライブ C のボリューム ラベルは Windows です
 ボリューム シリアル番号は XXXX-XXXX です

 c:\Windows\Temp のディレクトリ

2020/04/30  12:56    <DIR>          .
2020/04/30  12:56    <DIR>          ..
2020/03/06  11:07           881,968 DELD8CB.tmp
2020/03/03  07:23           228,352 DELD8DB.tmp
2017/11/18  14:00            81,920 DELD8EB.tmp
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3