2014年12月18日木曜日

改訂番号を含めてWordのプロパティ値を削除する(メモ)


近いうちに整形する。

---

# 作業ディレクトリの作成
$tmpDir = "C:\ps-test\tmp"
New-Item $tmpDir -ItemType Directory

# ターゲットファイル(docx)
$targetFile = "C:\ps-test\2.docx"

# オリジナルのファイル名のみ抽出
$originalFileName = Split-Path $targetFile -Leaf
# オリジナルの格納ディレクトリのみ抽出
$originalFileDir = Split-Path $targetFile -Parent

# zipファイル名の生成
$tmpFile = ($targetFile -replace ".docx$", ".zip")

# リネーム( docx => zip )
# (Shell.Applicationからアーカイブ操作するため)
Rename-Item -Path $targetFile $tmpFile

# シェルオブジェクトの作成
$shell = New-Object -Com Shell.Application

$shell.NameSpace($tmpFile).Items() | ?{ $_.Name -eq "docProps" } | %{$_.GetFolder.items()} | ?{$_.Name -eq "core.xml"} | %{$_.Name;$shell.NameSpace($tmpDir).movehere($_)}

# 作業用 core.xml のパス生成
$coreFile = ($tmpDir + "\" + "core.xml")

$contentCoreFile = Get-Content $coreFile
$contentCoreFile =[regex]::Replace($contentCoreFile, "<dc\:title>[\s\S]*<\/dc\:title>", "<dc:title>testT</dc:title>")
$contentCoreFile =[regex]::Replace($contentCoreFile, "<dc\:creator>[\s\S]*<\/dc\:creator>", "<dc:creator>Creator</dc:creator>")
$contentCoreFile =[regex]::Replace($contentCoreFile, "<cp\:lastModifiedBy>[\s\S]*<\/cp\:lastModifiedBy>", "<dc:creator></dc:creator>")
$contentCoreFile =[regex]::Replace($contentCoreFile, "<cp\:revision>[\s\S]*<\/cp\:revision>", "<cp:revision></cp:revision>")
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($coreFile, $contentCoreFile, $Utf8NoBomEncoding)

# 編集後のcore.xmlをzipの中へ移動
$shell.NameSpace($tmpFile + "\docProps").movehere($coreFile)

Start-Sleep -Milliseconds 500

If( Test-Path $coreFile ){
  Write-Host "remove.."
  Remove-Item $coreFile
}

$shell = $nothing

# リネーム( zip => docx )
# (zipから元に戻す)
Rename-Item -Path $tmpFile $targetFile

Remove-Item $tmpDir

2014年12月12日金曜日

PowershellでWordのプロパティ値を操作

# ターゲットファイル(docx)
$dest = "C:\fo\test.docx"

# ファイル名の抽出
$filename = Split-Path $dest -Leaf
$work_name = $filename -replace ".docx", ""
# ダミーディレクトリ名の生成
$parent = $(Split-Path $dest -Parent) + "\"
$work_dir = $parent

# ダミーファイル名の生成
$work_dest = $work_dir + $work_name  + ".zip"

$core_dest = $work_dir + "core.xml"

# zip読み込みのためにダミーコピー
Copy-Item $dest $work_dest

# zipファイルの展開
$shell = New-Object -Com Shell.Application
$zipItem = $shell.NameSpace($work_dest)
$items1 = $zipItem.Items()
foreach($item1 in $items1) {
  If( $item1.Name -eq "docProps") {
    $items2 = $item1.GetFolder.items()
    foreach( $item2 in $items2){
      If( $item2.Name -eq "core.xml" ) {
        $item2.Name
        $shell.NameSpace($parent).copyhere($item2)
      }
    }
  }
}

$test = Get-Content .\orgcore.xml
$test2 =[regex]::Replace($test, "<dc\:title>[\s\S]*<\/dc\:title>", "<dc:title></dc:title>")
[System.IO.File]::WriteAllLines("C:\Users\pirshiki-admin\Desktop\ps-test\trush\test.xml", $test2, $Utf8NoBomEncoding)

#$xml = New-Object XML
#$xml.Load($core_dest)
# Write-Host $(Get-Content $($work_dir + "\docProps\core.xml"))
#$xml.coreProperties.revision = "aaa"
#$str_xml = $xml | ConvertTo-Xml -As String
#$str_xml = $xml.OuterXml
#$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
#[System.IO.File]::WriteAllLines($core_dest, $str_xml, $Utf8NoBomEncoding)
# $xml.Save($core_dest)

Remove-Item ($work_dest + "\docProps\core.xml")
$shell.NameSpace($work_dest + "\docProps\").Copyhere($core_dest)

2014年12月4日木曜日

PowerShellでExcel/Wordの個人情報を消去する


Officeのファイルを編集したとき、「最終保存者」が必ず付いてしまう。
それをPowerShellで消去する方法。