LoginSignup
0
4

More than 5 years have passed since last update.

PowerShellとExcelで指定フォルダ以下のPDFファイル一覧をリンク付きで出力

Posted at

はじめに

「記録文書の一覧が欲しい」
「元のWordファイルじゃなくて署名の入ったPDFファイルの一覧が欲しい」
そうかい…ファイル検索って知ってる…?

参考資料

VBAでハイパーリンクを設定(Hyperlinks.Add)

ソース

make_pdflist.ps1
# フォルダ内のpdfすべてのリンク付きブックを作成する

#いつものを関数化した
function FolderDialog{
    [CmdletBinding()]
    param(
        [string]$path
    )
    Process{
        [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
        $process = [System.Diagnostics.Process]::GetCurrentProcess()
        $window = New-Object Windows.Forms.NativeWindow
        $window.AssignHandle($process.MainWindowHandle)

        $dialog = New-Object System.Windows.Forms.FolderBrowserDialog
        $dialog.Description = "フォルダを選択してください"
        $dialog.SelectedPath = $PWD
        $ret = $dialog.ShowDialog($window)
        if($ret -eq [System.Windows.Forms.DialogResult]::OK){
            $Path = $dialog.SelectedPath
            return $path
        } else {
            Read-Host "キャンセルされました。ENTERキーを押してください。"
            return
        }
    }
}

#フォルダ選択ダイアログ関数からパスを取得
$folder = FolderDialog 
$list = Get-ChildItem -Recurse $folder *.pdf
$num = $list.count

#Excelの下ごしらえ
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $true
$book = $excel.Workbooks.Add()
$sheet = $book.Sheets(1)

#内容を入力していく
1..($num) | %{
    $sheet.cells.item($_,1) = $list[($_-1)].FullName
    $sheet.hyperlinks.Add($sheet.cells.item($_,1),$list[($_-1)].FullName)
}
$book.SaveAs($folder +"\"+ (Split-Path $folder -Leaf)+"_pdflist.xlsx")

#末端処理
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($sheet)

出力

pdflist.png
ヨシ!
あとは好みのフォルダを選択して実行してもらえば大丈夫。

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