gpt4 book ai didi

powershell - 需要帮助弄清楚为什么Powershell会引发错误16

转载 作者:行者123 更新时间:2023-12-03 01:21:18 25 4
gpt4 key购买 nike

我发布了此脚本the other day,以期找到一种“另存为”时更改文件扩展名的好方法。我遇到了麻烦,但是从今天早上开始,脚本将无法正确运行。这是我收到的错误消息:

Processing : C:\users\xxx\Desktop\ht\Automatic_Post-Call_Survey.htm
Exception calling "SaveAs" with "16" argument(s): "This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was typed correctly.
* Select a file from the list of files and folders."
At C:\users\xxx\Desktop\hd.ps1:11 char:20
+ $opendoc.saveas <<<< ([ref]"$docpath\$doc.FullName.doc", [ref]$saveFormat);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

如果错误代码为“16”,则表示无法删除目录...但它似乎根本不是我要的,除非某处有一些默认参数。我很困惑。有人有其他可以尝试的想法吗?
$docpath = "c:\users\xxx\desktop\do"
$htmPath = "c:\users\xxx\desktop\ht"

$srcfiles = Get-ChildItem $htmPath -filter "*.htm*"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocument");
$word = new-object -comobject word.application
$word.Visible = $False
$filename = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))

function saveas-document {
$opendoc = $word.documents.open($doc.FullName);
$opendoc.saveas([ref]"$docpath\$filename", [ref]$saveFormat);
$opendoc.close();
}
ForEach ($doc in $srcfiles) {
Write-Host "Processing :" $doc.FullName
saveas-document
$doc = $null
}

$word.quit();

最佳答案

这应该做您需要的,但不是最好的设计:)

$docpath = "c:\users\xxx\desktop\do"
$htmPath = "c:\users\xxx\desktop\ht"


$srcfiles = Get-ChildItem $htmPath -filter "*.htm*"

$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocument");

$global:word = new-object -comobject word.application

$word.Visible = $False

#$filename = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))

function saveas-document ($docs) {
$opendoc = $word.documents.open($docs);
$savepath = $docs -replace [regex]::escape($htmPath),"$docpath"
$savepath = $savepath -replace '\.html*', '.doc'
$opendoc.saveas([ref]"$savepath", [ref]$saveFormat);
$opendoc.close();
}
ForEach ($doc in $srcfiles) {
Write-Host "Processing :" $doc.FullName
saveas-document -doc $doc.FullName
$doc = $null
}

$word.quit();

关于powershell - 需要帮助弄清楚为什么Powershell会引发错误16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13860763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com