问题描述
我正在使用以下 powershell 脚本打开几千个 HTML 文件并另存为..."Word 文档.
I'm using the following powershell script to open a few thousand HTML files and "save as..." Word documents.
param([string]$htmpath,[string]$docpath = $docpath)
$srcfiles = Get-ChildItem $htmPath -filter "*.htm*"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocument");
$word = new-object -comobject word.application
$word.Visible = $False
function saveas-document
{
$opendoc = $word.documents.open($doc.FullName);
$opendoc.saveas([ref]"$docpath\$doc.FullName.doc", [ref]$saveFormat);
$opendoc.close();
}
ForEach ($doc in $srcfiles)
{
Write-Host "Processing :" $doc.FullName
saveas-document
$doc = $null
}
$word.quit();
内容转换得很好,但我的文件名不符合预期.
The content converts splendidly, but my filename is not as expected.
$opendoc.saveas([ref]"$docpath\$doc.FullName.doc", [ref]$saveFormat);
导致 foo.htm
保存作为 foo.htm.FullName.doc
而不是 foo.doc
.
$opendoc.saveas([ref]"$docpath\$doc.FullName.doc", [ref]$saveFormat);
results in foo.htm
saving as foo.htm.FullName.doc
instead of foo.doc
.
$opendoc.saveas([ref]"$docpath\$doc.BaseName.doc", [ref]$saveFormat);
产生 foo.htm.BaseName.doc
如何设置一个
另存为...
文件名变量等于 BaseName
和 .doc
的串联?
How do I set up a
Save As...
filename variable equal to a concatenation of BaseName
and .doc
?
推荐答案
根据我们上面的评论,移动文件似乎就是您想要完成的全部工作.以下对我有用.在当前目录中,它将 .txt 扩展名替换为 .py 扩展名.我找到了命令 这里.
Based on our comments above, it seems that moving the files is all you want to accomplish. The following works for me. In the current directory, it replaces .txt extensions with .py extensions. I found the command here.
PS C:\testing dir *.txt | Move-Item -Destination {[IO.Path]::ChangeExtension( $_.Name, "py")}
您也可以将
*.txt
更改为 C:\path\to\file\*.txt
这样您就不需要从该位置执行这一行的文件.您应该能够以类似的方式定义目的地,所以如果我找到一种简单的方法来做到这一点,我会回来报告.
You can also change
*.txt
to C:\path\to\file\*.txt
so you don't need to execute this line from the location of the files. You should be able to define a destination in a similar manner, so I'll report back if I find a simple way to do that.
此外,我在搜索时发现了 Microsoft 的 TechNet 库.它有许多关于使用 PowerShell 编写脚本的教程.文件和文件夹,第 3 部分:Windows PowerShell 应该可以帮助您查找有关复制和移动文件的其他信息.
Also, I found Microsoft's TechNet Library while I was searching. It has many tutorials on scripting using PowerShell. Files and Folders, Part 3: Windows PowerShell should help you to find additional info on copying and moving files.
这篇关于如何使用 PowerShell 将“另存为..."不同的文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!