{"id":71303,"date":"2004-10-04T13:31:00","date_gmt":"2004-10-04T13:31:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/04\/how-can-i-print-text-files-using-a-script\/"},"modified":"2004-10-04T13:31:00","modified_gmt":"2004-10-04T13:31:00","slug":"how-can-i-print-text-files-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-print-text-files-using-a-script\/","title":{"rendered":"How Can I Print Text Files Using a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! I have a bunch of text files in a folder. Is there a way to print those files using a script? I know I can use Microsoft Word to print the files from a script, but I don\u2019t have Microsoft Word installed on that computer.<BR><BR>&#8212; MA<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, MA. Don\u2019t tell anyone from Microsoft we said this, but that\u2019s OK; you don\u2019t need Microsoft Word just to print a few text files. Instead, you can use the Shell object &#8211; which is built right into the operating system &#8211; to print text files. And what\u2019s cool about this is the fact that you can print these files even though the Shell object doesn\u2019t have a Print method. Strange but true!<\/P>\n<P>You can do this is because the Shell object has a method called InvokeVerbEx that lets you perform the tasks that show up when you right-click a file in Windows Explorer. Right-click one of your text files, and look at the context menu that pops up. You should see things like <B>Open<\/B>, <B>Print<\/B>, <B>Edit<\/B>, <B>Cut<\/B>, <B>Copy<\/B>, <B>Delete<\/B>, etc. The Shell object lets you perform all those tasks programmatically. In fact, here\u2019s a script that prints all the files in the folder C:\\Logs:<\/P><PRE class=\"codeSample\">TargetFolder = &#8220;C:\\Logs&#8221; \nSet objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(TargetFolder) \nSet colItems = objFolder.Items\nFor Each objItem in colItems\n    objItem.InvokeVerbEx(&#8220;Print&#8221;)\nNext\n<\/PRE>\n<P>All we\u2019re doing here is connecting to the C:\\Logs folder and then grabbing a collection of all the items found in that folder. We store the set of files in the variable colItems, using this line of code:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>Note that we\u2019ve made it easy on ourselves in this example by making sure that C:\\Logs contains only log files, and that we want to print each of those files. If C:\\Logs contains other files, files that we <I>don\u2019t<\/I> want to print, you\u2019ll have include code that distinguishes between the files you want to print and the files you don\u2019t want to print.<\/P>\n<P>After stashing the set of files in colItems, we use a For Each loop to loop through all the items in the collection. For each item in the collection (in other words, for each file in the folder) we use the InvokeVerbEx method, and we tell InvokeVerbEx that we want to print each file:<\/P><PRE class=\"codeSample\">objItem.InvokeVerbEx(&#8220;Print&#8221;)\n<\/PRE>\n<P>That\u2019s it. Run this script and all the files will dutifully print, one right after the other.<\/P>\n<P>Now what if we wanted to do something else, something like, say, edit each file? No problem; just replace the Print parameter with the appropriate parameter name from the context menu:<\/P><PRE class=\"codeSample\">TargetFolder = &#8220;C:\\Logs&#8221; \nSet objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(TargetFolder) \nSet colItems = objFolder.Items\nFor Each objItem in colItems\n    objItem.InvokeVerbEx(&#8220;Edit&#8221;)\nNext\n<\/PRE>\n<P>One thing to keep in mind is that when you use InvokeVerbEx your script replicates the same actions you see in Windows Explorer. For example, what happens in Windows Explorer if you right-click a file and choose Delete? That\u2019s right: instead of the file being automatically deleted, a dialog box appears asking if you are sure you want to send the file to the Recycle Bin. That\u2019s important, because the same dialog box will appear if you pass Delete to InvokeVerbEx. This script won\u2019t automatically delete the files in C:\\Logs, but instead pops up a confirmation dialog box for each file in the folder:<\/P><PRE class=\"codeSample\">TargetFolder = &#8220;C:\\Logs&#8221; \nSet objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(TargetFolder) \nSet colItems = objFolder.Items\nFor Each objItem in colItems\n    objItem.InvokeVerbEx(&#8220;Delete&#8221;)\nNext\n<\/PRE>\n<P>Keep in mind, too, that you might not be able to use <I>all<\/I> the options that appear in the Context menu. For more information on determining which options are available to you and which ones are not, check out this section of the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_fil_gmtl.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a bunch of text files in a folder. Is there a way to print those files using a script? I know I can use Microsoft Word to print the files from a script, but I don\u2019t have Microsoft Word installed on that computer.&#8212; MA Hey, MA. Don\u2019t tell anyone from [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[445,404,3,4,707,14,5],"class_list":["post-71303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-scripting-techniques","tag-shell-application","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a bunch of text files in a folder. Is there a way to print those files using a script? I know I can use Microsoft Word to print the files from a script, but I don\u2019t have Microsoft Word installed on that computer.&#8212; MA Hey, MA. Don\u2019t tell anyone from [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71303\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}