{"id":67053,"date":"2006-06-22T15:49:00","date_gmt":"2006-06-22T15:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/22\/how-can-i-determine-whether-a-file-exists-on-a-remote-computer\/"},"modified":"2006-06-22T15:49:00","modified_gmt":"2006-06-22T15:49:00","slug":"how-can-i-determine-whether-a-file-exists-on-a-remote-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-whether-a-file-exists-on-a-remote-computer\/","title":{"rendered":"How Can I Determine Whether a File Exists on a Remote Computer?"},"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 file exists on a remote computer even if that file isn\u2019t in a shared folder?<BR><BR>&#8212; SP<\/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=\"TechNet Script Center\" height=\"288\" alt=\"TechNet 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, SP. You know, the Scripting Guy who writes this column had minor periodontal surgery this morning. (Well, maybe his <I>dentist<\/I> thought it was minor. The Scripting Guy, who\u2019s a bit of a baby when it comes to these sorts of things, begs to differ.) And, yes, our hero came to work anyway, meaning he\u2019s either a real trouper or, well, an idiot. <I>(Editor\u2019s Note: I think we all know the answer to that one.)<\/I> At any rate, even though he\u2019s in no pain or discomfort (aside from the fact that he was only allowed to eat yogurt at lunch) he was definitely hoping to find an easy question to answer for today\u2019s column. SP, this is just what the doctor ordered!<\/P>\n<P>OK, technically the doctor ordered Vicodin and penicillin. But you know what we mean.<\/P>\n<P>As you noted, you want to verify the existence of a file on a remote computer; however, that file might not be stored in a shared folder. Is that a problem? Well, it could be, if you were using the FileSystemObject. But if you\u2019re using WMI, well, then it\u2019s no problem at all: WMI can locate files in a regular old folder just as easily as it can locate files in a shared folder. Verify the existence of a file on a remote computer? It\u2019s as easy as pulling teeth.<\/P>\n<P>So to speak.<\/P>\n<P>For example, here\u2019s a script that will check to see if the file C:\\Scripts\\Test.vbs can be found on the remote computer atl-fs-01:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService. _\n    ExecQuery(&#8220;Select * From CIM_DataFile Where Name = &#8216;C:\\\\Scripts\\\\Test.vbs'&#8221;)<\/p>\n<p>If colFiles.Count = 0 Then\n    Wscript.Echo &#8220;The file does not exist on the remote computer.&#8221;\nElse\n    Wscript.Echo &#8220;The file exists on the remote computer.&#8221;\nEnd If\n<\/PRE>\n<P>We told you this Scripting Guy was looking for an easy question to answer; as you can see, there\u2019s not much to this script at all. We begin by assigning the name of the remote computer to a variable named strComputer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>In an unusual plot twist (at least for this column), that brings up an interesting question: could we run this same script on the <I>local<\/I> computer? You bet; all we have to do is assign a dot (.) to the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EMD\" 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>. Why a dot? That\u2019s easy: when constructing a WMI path the dot provides a way to indicate the local computer, even if you don\u2019t know the <I>name<\/I> of the local computer.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After assigning the computer name to strComputer we connect to the WMI service on atl-fs-01. That brings us to this line of code:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where Name = &#8216;C:\\\\Scripts\\\\Test.vbs'&#8221;)\n<\/PRE>\n<P>Here we\u2019re retrieving a collection of all the files on atl-fs-01 that have a name equal to C:\\Scripts\\Test.vbs. (In WMI, the <B>Name<\/B> property is equivalent to the file path.) And no, you\u2019re not seeing double: we really <I>did<\/I> write out the path as <B>C:\\\\Scripts\\\\Test.vbs<\/B>. That\u2019s not due to us taking one-too-many painkillers (we\u2019re too tough to take <I>any<\/I> painkillers!). Instead, that\u2019s due to the fact that the \\ is a reserved character in WMI; because of that any time you use the \\ in a query you must \u201cescape\u201d the character by prefacing it with another \\. It\u2019s a bit goofy-looking, but it works.<\/P>\n<P>Now, where were we? <I>(On painkillers, tough guy.)<\/I> Oh, right: as we noted, our query returns a collection of all the files with the Name C:\\Scripts\\Test.vbs. To determine whether or not the file exists all we have to do is check the <B>Count<\/B> of the collection; as the name implies, the Count tells you the number of items in the collection. Suppose Count is equal to 0. That means there are no items in the collection; in turn, that means that there are no files named C:\\Scripts\\test.vbs located on atl-fs-01. Therefore, we echo back the fact that the file does not exist. If Count is not equal to 0 (and, because file paths must be unique, the only other possibility is a Count equal to 1) we echo back the fact that the file <I>does<\/I> exist on atl-fs-01.<\/P>\n<P>Pretty simple, right? All that takes place inside this block of code:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService. _\n    ExecQuery(&#8220;Select * From CIM_DataFile Where Name = &#8216;C:\\\\Scripts\\\\Test.vbs'&#8221;)<\/p>\n<p>If colFiles.Count = 0 Then\n    Wscript.Echo &#8220;The file does not exist on the remote computer.&#8221;\nElse\n    Wscript.Echo &#8220;The file exists on the remote computer.&#8221;\nEnd If\n<\/PRE>\n<P>And there you have it. Because WMI works by connecting to the remote machine itself, it doesn\u2019t matter whether the file is in a shared folder or not; WMI will track it down for you.<\/P>\n<P>Ah, good question: suppose you\u2019re looking for the file Test.vbs but you aren\u2019t sure what folder that file might be stored in. Can WMI located a file even when you don\u2019t know the folder name? You bet it can. We won\u2019t discuss this script in any detail today, but it searches atl-fs-01 for any file named Test.vbs, regardless of the folder that file might be stored in:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService. _\n    ExecQuery(&#8220;Select * From CIM_DataFile Where FileName = &#8216;Test&#8217; and Extension = &#8216;vbs'&#8221;)<\/p>\n<p>If colFiles.Count = 0 Then\n    Wscript.Echo &#8220;The file does not exist on the remote computer.&#8221;\nElse\n    Wscript.Echo &#8220;The file exists on the remote computer.&#8221;\nEnd If\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E1E\" 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>. For more information on using scripts to manage files and folders &#8211; including an explanation of what properties such as <B>FileName<\/B> and <B>Extension<\/B> represent &#8211; see the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_fil_overview.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We hope that helps, SP. We\u2019d be happy to stay and chat about this, except it\u2019s time to go home and eat dinner. And, trust us, after being limited to yogurt for lunch we are <I>starved<\/I>. Fortunately, for dinner the dentist has said we can have \u2026 um, yogurt \u2026. Oh: and maybe a banana. How \u2026 nice \u2026.<\/P>\n<P>Come to think of it, SP, if you\u2019d like to chat more about this script, well, maybe we <I>can<\/I> stay a bit longer after all \u2026.<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0622.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0622.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I check to see if a file exists on a remote computer even if that file isn\u2019t in a shared folder?&#8212; SP Hey, SP. You know, the Scripting Guy who writes this column had minor periodontal surgery this morning. (Well, maybe his dentist thought it was minor. The Scripting Guy, [&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-67053","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 file exists on a remote computer even if that file isn\u2019t in a shared folder?&#8212; SP Hey, SP. You know, the Scripting Guy who writes this column had minor periodontal surgery this morning. (Well, maybe his dentist thought it was minor. The Scripting Guy, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67053","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=67053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67053\/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=67053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}