Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 1 in /home/r5652521/public_html/soma-engineering.com/wp-content/themes/affinger/functions.php on line 1548
Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 1 in /home/r5652521/public_html/soma-engineering.com/wp-content/themes/affinger/functions.php on line 1548
Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 1 in /home/r5652521/public_html/soma-engineering.com/wp-content/themes/affinger/functions.php on line 1548
Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 1 in /home/r5652521/public_html/soma-engineering.com/wp-content/themes/affinger/functions.php on line 1548
Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 1 in /home/r5652521/public_html/soma-engineering.com/wp-content/themes/affinger/functions.php on line 1548
Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 1 in /home/r5652521/public_html/soma-engineering.com/wp-content/themes/affinger/functions.php on line 1548
Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 1 in /home/r5652521/public_html/soma-engineering.com/wp-content/themes/affinger/functions.php on line 1548
こんにちは!そーまんです。
今回は、特定のフォルダ内にあるファイルを取得する方法についてご紹介します。
どういう時に使うか
利用用途は様々ですが、例えば以下のような目的で使用することができます。まずはファイルの一覧を取得するところから始めます。
- 特定のフォルダの中にあるファイルの一覧を取得したい時や、一覧の中に特定の拡張子を持つファイル、例えば CSV ファイルがあるかどうか確認したい時
- ファイルを取得した後に一覧をテキストファイルに書き込んだり、ファイルを別の場所に移動したり、または Import-Csv コマンドレットでファイルを読み込んで ForEach-Object で繰り返し処理をしたい時
ファイルを取得する方法
Get-ChildItem コマンドレットを使います。
試しに C:\Windows\System32 の中のファイルを取得してみましょう。かなり多く表示されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Get-ChildItem C:\Windows\System32 # 実行結果 ディレクトリ: C:\Windows\System32 Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 3/19/31 15:20 0409 d----- 3/19/31 13:53 AdvancedInstallers d----- 5/10/30 14:20 af-ZA d----- 3/19/31 13:53 am-et d----- 3/19/31 13:52 AppLocker ... |
特定のフォルダにある特定の拡張子を含むファイル名を取得したい場合は -Filter を使います。
ここでは、System32 フォルダ内にテキストがあるかファイルを取得してみました。
1 2 3 4 5 6 7 8 9 10 |
Get-ChildItem "C:\Windows\System32" -Filter *.txt # 実行結果 ディレクトリ: C:\Windows\System32 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 6/11/01 11:17 6658 VmChipset Third-Party Notices.txt -a---- 3/19/31 13:46 1649 WindowsCodecsRaw.txt |
特定のフォルダにあるフォルダ名を取得したい場合は -Directory を使います。
ここでは、System32 フォルダ内にフォルダがあるか取得してみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Get-ChildItem -Directory "C:\Windows\System32" # 実行結果 ディレクトリ: C:\Windows\System32 Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 3/19/31 15:20 0409 d----- 3/19/31 13:53 AdvancedInstallers d----- 5/10/30 14:20 af-ZA d----- 3/19/31 13:53 am-et d----- 3/19/31 13:52 AppLocker d----- 5/24/01 9:25 appmgmt d----- 6/1/01 8:42 appraiser d---s- 3/19/31 15:23 AppV d----- 5/24/01 8:47 ar-SA d----- 5/10/30 14:20 as-IN d----- 11/16/30 23:16 az-Latn-AZ ... |
特定のフォルダにあるファイルを再帰的に取得したい場合は、-Recurce を使います。
それをパイプで Where-Object コマンドレットに渡してファイルの属性を a (アーカイブ) を指定しています。
属性の一覧は以下にまとめておきました。
属性 | 説明 |
---|---|
d | ディレクトリ |
a | アーカイブ |
r | 読み取り専用 |
h | 隠しファイル |
s | システムファイル |
以下は、User1 のデスクトップから再帰的に 1 日より前のファイルを取得するといった内容です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Get-ChildItem -Recurse "C:\Users\user1\desktop" | ` Where-Object {($_.Mode -eq "-a----") -and ($_.CreationTime -lt (Get-Date).AddDays(-1))} # 実行結果 ディレクトリ: C:\Users\user1\desktop Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 6/11/01 20:18 1861 Windows10インストール.pptx - ショートカット.lnk -a---- 6/11/01 20:34 2503 PowerShell記事.doc -a---- 6/30/01 15:39 0 新しいテキスト ドキュメント.txt ディレクトリ: C:\Users\user1\desktop\新しいフォルダー Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 6/29/01 14:19 22350 1.jpg -a---- 6/29/01 14:19 16145 2.jpg |
ファイルを取得した後にどうするか (応用)
上の方法でファイルの一覧を取得したり、特定の拡張子を持つファイルを取得した後に様々な処理をする事ができます。
ここで重要なのが、取得したファイルの情報 (オブジェクト) をこの縦棒 | を使って次の処理に渡してあげるという事です。
この縦棒がパイプライン (以降パイプと省略) というものになります。パイプについては以下の記事で説明しています。
パイプで処理結果を渡した後に、次の処理を行います。ここではいくつか簡単なサンプルを挙げてみましたので参考になれば幸いです。
ファイルを取得した後にテキストに書き込む
Out-File コマンドレットでテキストファイルとして出力する事ができます。
1 2 3 |
Get-ChildItem -Recurse "C:\Users\user1\desktop" | ` Where-Object {($_.Mode -eq "-a----") -and ($_.CreationTime -lt (Get-Date).AddDays(-1))} | ` Out-File -FilePath "C:\Users\user1\desktop\list.txt" |
ファイルを取得した後に別の場所にコピー・または移動する
取得したファイルの情報 $File に代入し、移動元のフォルダ ($Source) と取得したファイル名 ($File) を Join-Path コマンドレットでフルパスに結合します。
その後に Move-Item コマンドレットでファイルと移動先のフォルダ ($Target) へ移動します。
1 2 3 4 5 |
$Source = "D:\Work" $Target = "D:\Work\Log" $File = Get-ChildItem $Source -Filter *.log $Source = Join-Path $Source $File Move-Item $Source $Target -Force |
最後に
以上になります。いかがでしょうか。
ここではファイルを取得した後にパイプを使って様々な処理ができる事が分かりました。
実際に現場で使える処理になりますので、まずはファイルの取得をする方法を覚えておきましょう。
取得する処理が理解できたら、次へ上に書いたとおりにテキストに出力したり、移動したり削除したりする処理にパイプを使って渡してあげてみて下さい。
また色々な処理を試してみて効率の良い処理などを試してみると面白いかと思います。
それでは最後までお読みいただきありがとうございました!