我继承并运行了下面的 Powershell 脚本,但我在解释它并将它的一些功能应用到我正在尝试编写的其他脚本时遇到了问题。
特别是,我正在查看以下代码行:
ForEach ($doc in $srcfiles) {
saveas-document -docs $doc
}
从程序功能中,我知道文档的每个实例$srcfiles
都被分配给变量$doc
,并且该变量作为输入值传递给saveas-document
函数。但是,我不确定$doc
来自哪里。该语句是否动态声明$doc
变量?还是代表我的源路径中的文档对象的 Powershell 保留字?此外,本质上的-docs
开关是否声明$doc
等于$docs
函数预期的变量?我需要一些帮助来理解它是如何工作的,以便我可以将这些知识应用到其他项目中。
$global:word = new-object -comobject word.application
$word.Visible = $False
# PATHS
$backupPath = "\\Server\path\to\source\files\"
$srcfiles = Get-ChildItem $backupPath -filter "*htm.*"
$dPath = "\\Server\path\to\desitination\files\"
$htmPath = $dPath + "HT\" # Data path for HTML
$docPath = $dPath + "DO\" # Data path for *.DOC
$doxPath = $dPath + "DX\" # Data path for *.DOCX
$txtPath = $dPath + "TX\" # Data path for *.TXT
$rtfPath = $dPath + "RT\" # Data path for *.RTF
# SAVE FORMATS
$saveFormatDoc = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 0);
$saveFormatTxt = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 4);
$saveFormatRTF = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 6);
$saveFormatDox = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 16);
$saveFormatXML = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 14);
# Convert Documents
function saveas-document ($docs) {
$savepath = "$docPath$($docs.BaseName)"
"Converting to $savepath.doc"
$opendoc.saveas([ref]"$savepath", [ref]$saveFormatDoc)
$opendoc.close()
"Success with conversions."
" "
}
#
ForEach ($doc in $srcfiles) {
saveas-document -docs $doc
}
#
$word.quit()