Если желаемый вывод - это серия документов Word с именами Dream_File_01.docx
, Dream_File_02.docx
и т. Д., Тогда я полностью понимаю, почему вы хотите использовать регулярное выражение для получения StudentID
.
Как уже прокомментировал Lee_Dailey, счетчик страниц, необходимый для последовательного именования, уже присутствует в переменной $i
.
В любом случае, вот мои модификации вашего сценария.Пожалуйста, прочитайте комментарии, которые я там написал, чтобы вы могли сделать свой выбор, используя StudentID
или нет:
## -- Settings --
#$fileNamePattern = "ID #:\s+(\d+)"
$fileNamePattern = "Student ID #:\s+# (\d+)"
$pageLength = 1
$inputFile = "Dream_File.docx"
$outputPath = "outputDir" # Use Join-Path so don't worry about it not ending with a backslash.
## -- End Settings
[ref]$SaveFormat = "Microsoft.Office.Interop.Word.WdSaveFormat" -as [type]
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($inputFile)
$pages = $doc.ComputeStatistics([Microsoft.Office.Interop.Word.WdStatistic]::wdStatisticPages)
$rngPage = $doc.Range()
for($i = 1; $i -le $pages; $i += $pageLength) {
[void]$word.Selection.GoTo([Microsoft.Office.Interop.Word.WdGoToItem]::wdGoToPage,
[Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute,
$i #Starting Page
)
$rngPage.Start = $word.Selection.Start
[void]$word.Selection.GoTo([Microsoft.Office.Interop.Word.WdGoToItem]::wdGoToPage,
[Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute,
$i+$pageLength #Next page Number
)
$rngPage.End = $word.Selection.Start
$marginTop = $word.Selection.PageSetup.TopMargin
$marginBottom = $word.Selection.PageSetup.BottomMargin
$marginLeft = $word.Selection.PageSetup.LeftMargin
$marginRight = $word.Selection.PageSetup.RightMargin
$rngPage.Copy()
$newDoc = $word.Documents.Add()
$word.Selection.PageSetup.TopMargin = $marginTop
$word.Selection.PageSetup.BottomMargin = $marginBottom
$word.Selection.PageSetup.LeftMargin = $marginLeft
$word.Selection.PageSetup.RightMargin = $marginRight
$word.Selection.Paste() # Now we have our new page on a new doc
$word.Selection.EndKey(6,0) # Move to the end of the file
$word.Selection.TypeBackspace() # Seems to grab an extra section/page break
$word.Selection.Delete() # Now we have our doc down to size
# This part I don't fully understand..
# Why do you want to get the Student ID number here? Should that be part of the filename?
# Can you be sure that on every page this number can be found?
$regex = [Regex]::Match($rngPage.Text, $fileNamePattern)
if($regex.Success) {
# Get the filename without extension from the $inputFile string and append the student ID, the pagecounter '$i' and '.docx' extension to it
$newFileName = '{0}_{1}_{2:00}.docx' -f [System.IO.Path]::GetFileNameWithoutExtension($inputFile), $regex.Groups[1].Value, $i
}
else {
# Get the filename without extension from the $inputFile string and append 'patternNotFound', the pagecounter '$i' and '.docx' extension to it
$newFileName = '{0}_patternNotFound_{1:00}.docx' -f [System.IO.Path]::GetFileNameWithoutExtension($inputFile), $i
}
# If the Student ID is not needed in the filename, use this instead:
# Get the filename without extension from the $inputFile string and append the pagecounter '$i' and '.docx' extension to it
# $newFileName = '{0}_{1:00}.docx' -f [System.IO.Path]::GetFileNameWithoutExtension($inputFile), $i
# next combine the output path with the new filename for Save()
$path = Join-Path -Path $outputPath -ChildPath $newFileName
$newDoc.SaveAs([ref] $path, [ref]$SaveFormat::wdFormatDocumentDefault)
$newDoc.Close()
}
# you're done, exit Word and clean up the Com object
$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Надеюсь, что это поможет