1
私はPowerShellの初心者です。次のスクリプトを使用して、XLSおよびXLSXファイルのディレクトリを調べています。その後、各ファイルの作成日を取得し、作成日を末尾に追加してファイル名の名前を変更します。PowerShellを使用して保存プロンプトなしでXSLの名前を変更
このスクリプトは、XLSXファイルで正常に動作します。しかし、XLSファイルが見つかった場合は、保存プロンプトが表示されます。"変更をxxx.xlsに保存しますか?"
この保存プロンプトを削除する方法を教えてください。以下は私のコードです。ありがとう:
Param(
$path = "C:\Excel",
[array]$include = @("*.xlsx","*.xls")
)
$application = New-Object -ComObject Excel.Application
$application.Visible = $false
$binding = "System.Reflection.BindingFlags" -as [type]
[ref]$SaveOption = "microsoft.office.interop.Excel.WdSaveOptions" -as [type]
## Get documents
$docs = Get-childitem -path $Path -Recurse -Include $include
foreach($doc in $docs)
{
try
{
## Get document properties:
$document = $application.Workbooks.Open($doc.fullname)
$BuiltinProperties = $document.BuiltInDocumentProperties
$pn = [System.__ComObject].invokemember("item",$binding::GetProperty,$null,$BuiltinProperties,"Creation Date")
$value = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$pn,$null)
## Clean up
$document.close([ref]$saveOption::wdDoNotSaveChanges)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($BuiltinProperties) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($document) | Out-Null
Remove-Variable -Name document, BuiltinProperties
## Rename document:
$date=$value.ToString('yyyyMMdd');
$strippedFileName = $doc.BaseName;
$extension = $doc.Extension;
#write-host $strippedFileName;
$newName = "$strippedFileName" +"_" + "$date"+ "$extension";
write-host $newName;
Rename-Item $doc $newName
}
catch
{
write-host "Rename failed."
$_
}
}
$application.quit()
$application.Workbooks.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($application) | Out-Null
'$ document.Savedをお試しください= $ true '' $ document.close() 'の直前 –
ありがとうマティアス!それはうまくいった。 –