We again use $_, which is the current item on the pipeline, and the fullname property, which includes both the path and the file name. We then use parentheses to group the next operation. We use 0, which means to start at the beginning. The first one is the position to start looking. Substring is a string method, and it takes two parameters. We are interested in the fullname property because it includes both the path and the entire filename. This is like your enumerator when you use the Foreach statement in either VBScript or in Windows PowerShell. The $_ is an automatic variable that refers to the current item on the pipeline. The reason we do not want the file extension is that we are going to save the file as a. Now we want to retrieve the path to the file, the file name but not the file extension. The backtick character is used for line continuation. The way that it returns a single item can be thought of as working in a similar fashion as the Foreach statement. The ForEach-Object cmdlet is used to examine items as they come streaming through the pipeline. This code can be seen here: Get-ChildItem -path $folderpath -include $fileTypes | The bar ( |) character is used for the pipeline character. We take this collection of items (Word documents in this example) and pass them into the pipeline. The Get-ChildItem cmdlet returns a collection of items. Next we call the Get-ChildItem cmdlet and tell it to file all the file types we stored in the $fileTypes variable, and to search the path we stored in the $folderPath variable. This is shown here: $fileTypes = “*.docx”,”*doc” We need to include the * wild card character to tell Get-ChildItem we are interested in any. doc files and we include each of them in a set of quotation marks. We now create an array of file extensions that we will pass to the Get-ChildItem cmdlet. This is required by the Get-ChildItem cmdlet and is seen here: $folderpath = “c:\fso\*” The thing to keep in mind is that we need to include the trailing backslash and star: \*. This is seen here: $word.visible = $false To allow the Word application to be invisible, we set the visible property to false. We do not want to make the Word application visible because we will be iterating through a collection of many documents. This is illustrated here: $word = New-Object -ComObject word.application To create the Word application object, we use the New-Object cmdlet to specify that it is a COM object and save the resulting object into the $word variable. This is the main object and is one that we always need to create if we are working with Word. When we have created our variable to hold the enumeration value, we need to create an instance of the Word application object. pdf file is wdFormatPDF, so we decided to use that as our variable name: $wdFormatPDF = 17 This is from the Word enumeration value called wdSaveFormat. The first thing we do in the script is create a variable that we will use to tell the Word Document object how to save our file. ![]() To begin with, we have a folder such as the one seen in this image. Get-ChildItem -path $folderpath -include $fileTypes | $word = New-Object -ComObject word.application Return to work tanned and relaxed, and then run this script: $wdFormatPDF = 17 Bring him into your office, demonstrate creating and saving one file, and then tell him that due to the importance of the project you should not be disturbed. Tell the PHB that it will take you at least two weeks to do this project. But still, there are thousands of files in there, and it will take at least two weeks to open and save and close all those files. Luckily, we have deployed Word 2007, and have also deployed the PDF add-in. Geez, one of these days, one of these days. This is despite the fact that a couple of months ago he had us deploy Word viewer, so everyone can view the documents. The pointy-headed boss (PHB) is being particularly obtuse this week, and has decided that he would like to have all the Word documents turned into PDF files so everyone can view them. Hey, Scripting Guy! I have a folder full of Word documents.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |