フォルダパスを指定して、直下にあるファイルの作成日時・更新日時を元に戻します。
拡張子.ps1で保存し、PowerShellで実行してください。
$dir = Read-Host "フォルダのパスを入力してください"
$files = (Get-ChildItem -Path $dir -Recurse).Fullname
foreach($file in $files) {
$dirPath = Split-Path $file
$fileName = Split-Path $file -Leaf
Write-Host $fileName
# Shell
$shell = New-Object -ComObject Shell.Application
$shellDir = $shell.namespace($dirPath)
$shellFile = $shellDir.parseName($fileName)
#$fUpdateDate = ""
#$fCreateDate = ""
$cCreateDate = ""
$cUpdateDate = ""
# 詳細
for ($i = 0; $i -lt 310; $i++) {
$propName = $shellDir.getDetailsOf($Null, $i)
## 3: 更新日時
#if($propName -eq "更新日時") {
# $fUpdateDate = $shellDir.getDetailsOf($shellFile, $i)
#}
## 4: 作成日時
#if($propName -eq "作成日時") {
# $fCreateDate = $shellDir.getDetailsOf($shellFile, $i)
#}
# 152: コンテンツの作成日時
if($propName -eq "コンテンツの作成日時") {
$cCreateDate = $shellDir.getDetailsOf($shellFile, $i)
}
# 154: 前回保存日時
if($propName -eq "前回保存日時") {
$cUpdateDate = $shellDir.getDetailsOf($shellFile, $i)
}
}
# 作成日時
if ($cCreateDate) {
# プロパティ詳細から取得した日付には制御文字が含まれるため取り除く
$cCreateDate = $cCreateDate -replace "[^0-9 /:]", ""
Set-ItemProperty $file -Name CreationTime -Value $cCreateDate
}
# 更新日時
if ($cUpdateDate) {
# プロパティ詳細から取得した日付には制御文字が含まれるため取り除く
$cUpdateDate = $cUpdateDate -replace "[^0-9 /:]", ""
Set-ItemProperty $file -Name LastWriteTime -Value $cUpdateDate
}
}
コメント