LoginSignup
11
14

More than 5 years have passed since last update.

PowerShellでディレクトリのファイル一覧とタイムスタンプを取得する

Last updated at Posted at 2014-12-09

概要

コマンドプロンプトのdirコマンドだとタイムスタンプが分までしか出ないので
powershellで取得する方法を調べたので残す
(コメントの指摘内容を反映して(Split-Path)を使用するように修正)

  • Select-Objectでもある程度は取得できるが微調整をしたかったのでGet-ItemPropertyを使う
  • ファイルを除外したいときなどは-excludeオプションを追加する

使い方

  1. コマンドプロンプトを起動
  2. cd /d で該当ディレクトリまで移動
  3. powershellを起動
  4. 以下のコマンドを実行
powershell.exe
#ディレクトリ、フルパス、拡張子、ファイル名、更新日時
$logname="hoge.log"
echo "" > $logname
$List=Get-ChildItem  -Recurse -Name
foreach($a in $List)
{
    $fullname=$(Get-ItemProperty $a).FullName
    $name=$(Get-ItemProperty $a).Name
    $dir_fullpath=$(Split-Path $fullname -Parent) #フルパスフォルダ名を取得

    $lastIndex=$dir_fullpath.LastIndexOf("\")
    $dir=$(Split-Path $dir_fullpath -Leaf) #ディレクトリ名を取得

    $extension=$(Get-ItemProperty $a).Extension
    $timestamp=$(Get-ItemProperty $a).LastWriteTime.ToString('yyyy/MM/dd HH:mm:ss')
    echo "$dir`t$dir_fullpath`t$extension`t$name`t$timestamp" >>$logname
}

11
14
3

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
11
14