سؤال

I've inherited and am running the Powershell script below, and I'm having one problem interpreting it and applying some of its functionality to other scripts I'm trying to write.

In particular, I'm looking at the following lines of code:

ForEach ($doc in $srcfiles) { 
saveas-document -docs $doc
}

From the program functionality, I know that each instance of a document in $srcfiles is being assigned to the variable $doc, and that variable is being passed to the saveas-document function as the input value. However, I'm not sure where $doc is coming from. Does this statement declare the $doc variable on the fly? Or is it a Powershell reserved word the represents the document object in my source path? Also, does the -docs switch in dessence declare that $doc is equal to the $docs variable expected by the function? I need some help understanding HOW this works so I can apply that knowledge to other projects.

$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()
هل كانت مفيدة؟

المحلول

Run the follwing commands, and read through the output:

Get-Help about_foreach -ShowWindow
Get-Help about_functions -ShowWindow
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top