{"id":69563,"date":"2005-06-20T19:10:00","date_gmt":"2005-06-20T19:10:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/20\/how-can-i-determine-if-a-file-exists-and-if-it-does-exit-the-script\/"},"modified":"2005-06-20T19:10:00","modified_gmt":"2005-06-20T19:10:00","slug":"how-can-i-determine-if-a-file-exists-and-if-it-does-exit-the-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-a-file-exists-and-if-it-does-exit-the-script\/","title":{"rendered":"How Can I Determine if a File Exists and, if It Does, Exit the Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I check to see if a particular file exists and, if it does, exit the script?<BR><BR>&#8212; TO<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, TO. There are at least two ways to check for the existence of a file (that\u2019s the real key here; exiting the script is easy). One of these approaches uses the <B>FileSystemObject<\/B> and the other uses WMI, and we\u2019re going to show you both methods today. Why? Well, that way if anyone comes by and asks us to help them with something we can say, \u201cWow, we\u2019d like to, but we have to finish this column, and because we\u2019re showing people <I>two<\/I> scripts, well, that could take awhile.\u201d<\/P>\n<P>OK, so that\u2019s only partly true: there are actually a couple valid reasons why you might choose one approach over the other. People generally find the FileSystemObject a bit easier to script; the only problem is that the FileSystemObject is designed to work solely on the local computer. WMI scripts tend to be a tad bit more complicated (although, as you\u2019ll see, they\u2019re still pretty easy), but WMI scripts can work with remote computers just as easily as they can the local computer. What does that mean? It means that, if you\u2019re working on the local computer, you might want to use the FileSystemObject. If you\u2019re working against a remote computer you need to use WMI.<\/P>\n<P>In the grand tradition of the Scripting Guys let\u2019s do the easy one first. Let\u2019s take a look at a FileSystemObject script that checks for the existence of the file C:\\Scripts\\Test.txt. If the file is found the script exits; if the file is <I>not<\/I> found then the script displays a message to that effect.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>If objFSO.FileExists(&#8220;C:\\Scripts\\Test.txt&#8221;) Then\n    Wscript.Quit\nElse\n    Wscript.Echo &#8220;The file does not exist.&#8221;\nEnd If\n<\/PRE>\n<P>We told you it was easy. We begin by creating an instance of the <B>Scripting.FileSystemObject<\/B> and then call the <B>FileExists<\/B> method, passing the method the complete path to the file in question. If FileExists returns True that means the file was found; at that point we then use <B>Wscript.Quit<\/B> to exit the script. If FileExists is <I>not<\/I> found we then use this line of code to echo back our failure to find the file:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;The file does not exist.&#8221;\n<\/PRE>\n<P>That\u2019s pretty much all we have to do.<\/P>\n<P>Incidentally, it\u2019s a good idea to use the FileExists method any time you\u2019re working with the FileSystemObject. The FileSystemObject is very powerful, but also very sensitive: it just falls to pieces when things go wrong. Worst of all, even using On Error Resume Next won\u2019t help; if the FileSystemObject tries to work with a non-existent file or folder your scripts are likely to crash even if you use On Error Resume Next. The way to work around that problem? Just use FileExists (or its companion method, <B>FolderExists<\/B>) before trying to bind to a file or folder.<\/P>\n<P>OK, well, that\u2019s all for today &#8211; oh, that\u2019s right, we said we\u2019d show you <I>two<\/I> scripts, didn\u2019t we? (What were we <I>thinking<\/I>?) As we noted, you can also check for the existence of a file using WMI. The nice thing about using WMI is that the same script can run against remote computers as well as local computers; all you have to do is change the name of the computer and have at it.<\/P>\n<P>Here\u2019s a WMI script that checks for the existence of C:\\Scripts\\Test.txt and, if the file is found, exits:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_Datafile Where Name = &#8216;C:\\\\Scripts\\\\Test.txt'&#8221;)<\/p>\n<p>If colFiles.Count &gt; 0 Then\n    Wscript.Quit\nElse\n    Wscript.Echo &#8220;The file does not exist.&#8221;\nEnd If\n<\/PRE>\n<P>Like we said, a tad bit more complicated, but nothing that you shouldn\u2019t be able to handle. We begin by setting the value of the variable strComputer to a dot; in the wonderful world of WMI the dot represents the local computer. But didn\u2019t we say that this same script could be used against remote computers? Yes, we did, and that\u2019s a simple enough task: just set the value of strComputer to the name of that remote computer. For example, this line of code will cause the script to run against the computer atl-fs-01:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>Next we connect to the WMI service, then use the <B>ExecQuery<\/B> method to select all instances of the <B>CIM_Datafile<\/B> class where the <B>Name<\/B> property is equal to C:\\\\Scripts\\\\Test.txt. And don\u2019t panic, you haven\u2019t suddenly been afflicted with double vision. In WMI the \\ is a reserved character; that means any time a \\ appears in a query the character must be \u201cescaped,\u201d which simply means prefacing the \\ with a second \\. Thus a file path like C:\\Scripts\\Test.txt gets written out as C:\\\\Scripts\\\\Test.txt. Hey, as long as it works, right?<\/P>\n<TABLE class=\"dataTable\" id=\"ETE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. By the way, this could be worse. For example, suppose we had a path like \\\\atl-fs-01\\public\\scripts. That would get written out like so: \\\\\\\\atl-fs-01\\\\public\\\\scripts, with every single \\ doubled up.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Our query returns a collection consisting of all the files on the computer named C:\\Scripts\\Test.txt. (Needless to say, there can either be one such file or no such files.) To determine whether or not the file was found we check the <B>Count<\/B> property of the returned collection. If Count is greater than 0 that means the file was found; in turn we call Wscript.Quit and exit the script. If the Count is not greater than 0 than no such file could be found and we echo a message to that effect.<\/P>\n<P>So there you have it: not one but <I>two<\/I> ways to determine whether a file exists and, if it does, exit the script. Wow, no doubt someday people will be saying, \u201cWhere were <I>you<\/I> on the day the Scripting Guys showed us two ways to determine whether a file exists?\u201d Until then, just use whichever script makes the most sense for you.<\/P><BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I check to see if a particular file exists and, if it does, exit the script?&#8212; TO Hey, TO. There are at least two ways to check for the existence of a file (that\u2019s the real key here; exiting the script is easy). One of these approaches uses the FileSystemObject [&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":[38,3,12,5],"class_list":["post-69563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I check to see if a particular file exists and, if it does, exit the script?&#8212; TO Hey, TO. There are at least two ways to check for the existence of a file (that\u2019s the real key here; exiting the script is easy). One of these approaches uses the FileSystemObject [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69563","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=69563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69563\/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=69563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}