2015-05-15 44 views
1

我对Powershell非常缺乏经验 - 但通过试验和错误我已经设法获得.doc/.docx到.pdf的转换,适用于指定的文件夹和所有子文件夹。在Powershell中转换后删除文件

$wdFormatPDF = 17 
$word = New-Object -ComObject word.application 
$word.visible = $false 
$fileTypes = "*.docx","*.doc" 
Get-ChildItem -Recurse -path "C:\test-acrobat" -include $fileTypes | 
foreach-object ` 
{ 
$path = ($_.fullname).substring(0,($_.FullName).lastindexOf(".")) 
"Converting $path to pdf ..." 
$doc = $word.documents.open($_.fullname) 
$doc.saveas($path, $wdFormatPDF) 
$doc.close() 
} 
$word.Quit() 

现在我希望能够在转换后删除原始.doc/.docx文件。做一些搜索,我发现,我认为什么工作:

{ 
    remove-item $fileTypes # delete file from file-system 
} 

但我宁愿检查不是扔在一个命令来删除文件...

任何帮助是极大的赞赏。 菲利普

+0

提示:'删除-Item'具有参数'-confirm'提示进行确认和'-whatif'显示会发生什么。请参阅:help -full remove-item –

回答

0

我会在foreach循环内添加删除。

所以,你会得到:

$wdFormatPDF = 17 
$word = New-Object -ComObject word.application 
$word.visible = $false 
$fileTypes = "*.docx","*.doc" 
Get-ChildItem -Recurse -path "C:\test-acrobat" -include $fileTypes | 
foreach-object ` 
{ 
    $path = ($_.fullname).substring(0,($_.FullName).lastindexOf(".")) 
    Write-Host "Converting $path to pdf ..." 
    $doc = $word.documents.open($_.fullname) 
    $doc.saveas($path, $wdFormatPDF) 
    $doc.close() 
    Remove-Item $_.fullname 
} 
$word.Quit()