Created
June 26, 2025 12:33
-
-
Save yyya-nico/41de3ee7704dbf8903b2a1f86b9f7383 to your computer and use it in GitHub Desktop.
DVテープで作成したAviファイルを撮影日時ファイル名に書き換えるPowerShellスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #SetAviDate.ps1 | |
| #[Version] | |
| # 2.0.0-yyya-nico | |
| #[Released] | |
| # 2025/06/26 | |
| #[Abstract] | |
| # MediaInfoコマンドから取得できる撮影日情報(Recorded_Dateの値)を使って、 | |
| # ファイル名とファイルプロパティの作成日と更新日を変更します。 | |
| #[Usage] | |
| # PowerShell SetAviDate.ps1 | |
| #[Requirement] | |
| # PowerShell 5.1以上 | |
| #[Note] | |
| # このコードは http://funcs.org/889 で公開されていたスクリプトを自分好みに改良したものです。 | |
| #StrictModeをLatestに設定 | |
| Set-StrictMode -version Latest | |
| #------------------------------------------------------------------------- | |
| #設定 | |
| #★操作対象のファイルが格納されているディレクトリ(デフォルト) | |
| [String] $targetDir = [Environment]::GetFolderPath([Environment+SpecialFolder]::MyVideos); | |
| #★操作対象のファイル拡張子 | |
| [String] $targetExt = ".avi"; | |
| #★MediaInfoコマンド(CLI版)のフルパス | |
| [String] $mediaInfoCmd = """$PSScriptRoot\MediaInfo_CLI_25.04_Windows_x64\MediaInfo.exe"""; | |
| #MediaInfoコマンド実行時に指定する引数 | |
| [String] $mediaInfoArg = " --Inform=General;%Recorded_Date%"; | |
| #------------------------------------------------------------------------- | |
| #関数 | |
| #MediaInfoコマンドを実行する関数 | |
| #引数 | |
| # $strCmdArg : MediaInfoコマンドの引数 | |
| Function ExecMediaInfoCmd([String]$strCmdArg) | |
| { | |
| $proc = [System.Diagnostics.Process]; | |
| $pinfo = [System.Diagnostics.ProcessStartInfo]; | |
| $pinfo = New-Object System.Diagnostics.ProcessStartInfo; | |
| #実行するコマンド名(cmd.exe, 環境変数ComSpecより取得) | |
| $pinfo.FileName = [System.Environment]::GetEnvironmentVariable("ComSpec"); | |
| #コマンドの引数(実行コマンド名) | |
| $pinfo.Arguments = "/c " + """" + $mediaInfoCmd + " " + $strCmdArg + """"; | |
| #標準出力を受け取る場合はFalse | |
| $pinfo.UseShellExecute = $false; | |
| #標準出力を受け取る場合はTrue | |
| $pinfo.RedirectStandardOutput = $true; | |
| #コマンド実行 | |
| $proc = [System.Diagnostics.Process]::Start($pinfo); | |
| #コマンド実行の完了を監視(500ミリ秒ごとにポーリング) | |
| While(! $proc.HasExited){ | |
| #スリープ(500ミリ秒) | |
| Start-Sleep -m 500 | |
| } | |
| $output = [String]; | |
| #コマンドの標準出力 | |
| $output = $proc.StandardOutput.ReadToEnd(); | |
| $proc.WaitForExit(); | |
| $proc.Close(); | |
| Return $output; | |
| } | |
| #------------------------------------------------------------------------- | |
| #メイン処理 | |
| #外部コマンドのテスト | |
| [String] $usageOutput = ExecMediaInfoCmd; | |
| If($usageOutput -eq ""){ | |
| "MediaInfoコマンドが不正です。CLI版を使っているか確認してください。"; | |
| }Else{ | |
| #MediaInfoの引数を追加 | |
| $mediaInfoCmd += $mediaInfoArg; | |
| # 標準入力からディレクトリパスを受け取る | |
| Write-Host "変換対象ファイル($targetExt)が格納されたディレクトリのパスを入力してください。" | |
| Write-Host "何も入力せずEnterでデフォルト [$targetDir] を使用します。" | |
| $inputDir = Read-Host "ディレクトリパス" | |
| if (![string]::IsNullOrWhiteSpace($inputDir)) { | |
| $targetDir = $inputDir -Replace """",""; | |
| } | |
| # 操作対象ディレクトリ以下のサブディレクトリを再帰的に対象ファイルを検索 | |
| Get-ChildItem($targetDir) -Recurse | ForEach-Object { | |
| #拡張子名取得 | |
| [String] $extName = $_.Extension; | |
| If($extName -eq $targetExt){ | |
| #変更前のファイル名(フルパス) | |
| [String] $oldFilePath = $_.FullName; | |
| #ディレクトリ名取得 | |
| [String] $dirName = $_.DirectoryName; | |
| #変更前のファイル名 | |
| [String] $oldFileNameWoExt = [System.IO.Path]::GetFileNameWithoutExtension($_); | |
| #MediaInfoの実行とRecorded_Dateの取得(書式:「yyyy-MM-dd HH:mm:ss.000」) | |
| [String] $recordedTime = ExecMediaInfoCmd """$oldFilePath"""; | |
| #改行コードを削除 | |
| $recordedTime = $recordedTime -replace "[\r\n]",""; | |
| If($recordedTime -eq ""){ | |
| "Recorded_Dateが取得できませんでした。対象ファイル:{0}" -f $oldFilePath; | |
| }Else{ | |
| #末尾「.000」を削除 | |
| $recordedTime = $recordedTime -replace "\.[0-9]+ *$",""; | |
| #変更後のファイル名(書式:「yyyy-MM-dd_HH-mm-ss.avi」) | |
| [String] $newFileNameWoExt = $recordedTime -replace " ","_" -replace ":","-"; | |
| #ファイル名の変更前後で一致する場合は操作の対象外 | |
| If(! $oldFileNameWoExt.Equals($newFileNameWoExt)){ | |
| [String] $newFileName = $newFileNameWoExt + $extName; | |
| [String] $newFilePath = $dirName + "\" + $newFileName; | |
| #変更後ファイル名が存在する場合は操作の対象外 | |
| If(! (Test-Path -path $newFilePath)){ | |
| #ファイル名の変更 | |
| Rename-Item -Path $oldFilePath -newName $newFileName; | |
| #ファイルの作成日と更新日を設定(書式:「yyyy/MM/dd HH:mm:ss」) | |
| [String] $propTime = $recordedTime -replace "-","/"; | |
| Set-ItemProperty $newFilePath -Name CreationTime -Value $propTime; | |
| Set-ItemProperty $newFilePath -Name LastWriteTime -Value $propTime; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment