LoginSignup
0
0

More than 3 years have passed since last update.

フォルダ内のファイルの文字数をカウントし、一覧を作成するPowershellスクリプト

Last updated at Posted at 2019-12-21

テキスト形式で書いたいくつかの文章ファイルがあり、
それぞれの文字数・行数を知りたい(比較したい)と思ったので作成した。

下記をコピペして CountText.ps1 を作成
(文字数・行数をカウントしたいファイルがあるフォルダに配置する)

#結果ファイルを初期化。ついでに時刻と対象フォルダ(スクリプトを配置した場所)を出力
Write-Output $(Get-Date; Get-Location|Select-Object Path) > count.log

#対象となるファイル(txtファイル)の一覧を取得し、それぞれの数を数える
$itemList = Get-ChildItem -File -Name *.txt
foreach($item in $itemList)
{
    $Result_Char = (Get-Content -Encoding UTF8 $item |Measure-Object -Character).Characters
    $Result_Line = (Get-Content -Encoding UTF8 $item |Measure-Object -Line ).Lines
    Write-Output "$item : $Result_Char 文字, $Result_Line 行" >> count.log
} 

作成出来たら、その CountText.ps1 を右クリックし、 Powershellで実行 をすればOK。
そのフォルダ内に count.log が作成されます。

count.log の中身はこんな感じ


2019年12月21日 12:21:12

Path : D:\test\writing

file01.txt : 1249 文字, 31 行
file02.txt : 4114 文字, 76 行
file03.txt : 4109 文字, 77 行
file04.txt : 1895 文字, 34 行
memo01.txt : 8720 文字, 133 行

※行数について、空白行は無視される

実行できない場合

powershellのスクリプトを実行する際には実行可能になるように設定する必要があります。

管理者権限で開いたPowershell上で、
Set-ExecutionPolicy RemoteSigned

を実行すれば実行できるようになります。
※セキュリティリスクもあるので、一度 Set-ExecutionPolicy で調べてから自己責任でどうぞ。
 

0
0
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
0
0