需要帮助找出Powershell抛出错误16的原因吗
我发布这个脚本是为了找到一种在“另存为”时更改文件扩展名的好方法。我解决了这个问题,但是从今天早上开始,这个脚本将不会运行而没有错误。下面是我收到的错误消息:需要帮助找出Powershell抛出错误16的原因吗,powershell,syntax-error,Powershell,Syntax Error,我发布这个脚本是为了找到一种在“另存为”时更改文件扩展名的好方法。我解决了这个问题,但是从今天早上开始,这个脚本将不会运行而没有错误。下面是我收到的错误消息: 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 t
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
这应该满足您的需要,但不是最佳设计:)
$filename=e($.fullname)…
什么是e
?而$\uu
是因为你在管道里吗?@Christian我希望e
是我所有问题的根源;事实并非如此。不过接得好。删除e
(正如我在上面编辑的代码中所做的那样)会导致相同的错误消息。那么$\uuu
呢,我在您的代码中看不到管道。。。。测试$filename
是否有值。。。我认为is$null
可能我误解了管道,但我认为从前面代码中引用的MS Word文档中提取文件名而不是期望该变量来自其他地方是必要的。它需要是$fullname
吗?@Christian我不这么认为,我用$fullname
代替\ux得到了相同的错误代码。$fullname
工作起来就像做梦一样——只是文件扩展名仍然是*.htm而不是*.doc。你知道这是否是你期望的行为吗?我猜是的,我需要用另一种方法来更改文件名。@dwwilson66为扩展名添加了一个替换。You rock,@Christian。这对我了解PowerShell非常有帮助。@dwwilson66很乐意帮忙!花点时间阅读这本很棒的在线电子书:
$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();