{"id":71183,"date":"2004-10-20T18:06:00","date_gmt":"2004-10-20T18:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/20\/how-can-i-get-a-list-of-all-the-files-in-a-folder-and-its-subfolders\/"},"modified":"2004-10-20T18:06:00","modified_gmt":"2004-10-20T18:06:00","slug":"how-can-i-get-a-list-of-all-the-files-in-a-folder-and-its-subfolders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-the-files-in-a-folder-and-its-subfolders\/","title":{"rendered":"How Can I Get a List of All the Files in a Folder and Its Subfolders?"},"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 use a script to show me all the files in a folder? And then how can I modify that script so it shows me all the files in any subfolders of that folder?<BR><BR>&#8212; CS<\/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, CS. Yesterday we showed everyone a script that changed all the files in a folder from read-only to read-write. We also promised that, in today\u2019s column, we\u2019d explain how we managed to get a list of all the files in a folder in the first place. Your questions are the perfect lead-in to that explanation.<\/P>\n<P>Let\u2019s start with the easy one: a script that simply lists all the files in a folder. This script reports back the file name of all the files found in the folder C:\\Scripts:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjStartFolder = &#8220;C:\\Scripts&#8221;<\/p>\n<p>Set objFolder = objFSO.GetFolder(objStartFolder)<\/p>\n<p>Set colFiles = objFolder.Files\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>As you can see, there really isn\u2019t much to this. We create an instance of the FileSystemObject, and then we use the GetFolder method to bind to folder C:\\Scripts. Pretty straightforward. If we wanted to bind to, say, the Windows folder, all we\u2019d have to do is change the path accordingly, something we do by assigning a different value to objStartFolder:<\/P><PRE class=\"codeSample\">objStartFolder = &#8220;C:\\Windows&#8221;\n<\/PRE>\n<P>After we\u2019ve connected to the folder, we then create a reference to the Files property using this command:<\/P><PRE class=\"codeSample\">Set colFiles = objFolder.Files\n<\/PRE>\n<P>That gives us back a collection consisting of all the files found in the folder. (But- and this has important implications for your second question &#8211; this collection does not include files found in any subfolders of C:\\Scripts.) At this point the rest is child\u2019s play: we can now use a For Each loop to loop through the collection of files and &#8211; if we choose &#8211; do something to each and every one. Because you asked how to get a list of all the files in a folder, all we do is echo the name of the file. But we could do a lot more than that; for example, we could report back the DateCreated property or the Size property. For a more complete rundown of the FileSystemObject and how to use it, you might want to check out the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_scr_xfxi.mspx\"><B>Script Runtime Primer<\/B><\/A> in the Microsoft Windows 2000 Scripting Guide.<\/P>\n<P>In other words, getting back a list of all the files in a folder is trivial. Getting back a list of all the files in a folder <I>plus<\/I> all the files in any subfolders of that folder can be a bit trickier. To do that you need to use a recursive script. We\u2019re not going to explain recursion here; for more details, check out this portion of the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_vbs_kove.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>. (Yes, we <I>do<\/I> seem to be promoting the book quite a bit today, don\u2019t we?) Basically a recursive function is a function that can automatically call itself as many times as needed. That might not make much sense, but think of it this way. The script we showed you above lists all the files in a folder and then stops. It doesn\u2019t matter if there are subfolders in the folder; the script doesn\u2019t really care.<\/P>\n<P>A recursive function, by contrast, <I>does<\/I> care: it will keep working until it has done everything you asked of it, and more. A recursive function will list all the files in a folder, and then check to see if that folder has any subfolders. Suppose it finds subfolders A and B. In that case, the function will call itself, and list any files found in Subfolder A. And what if Subfolder A has a sub-subfolder C? No problem: the function will call itself again, and list all the files in sub-Subfolder C. This will continue until no more subfolders can be found. At that point, the function will loop back, and start working its way through Subfolder B. Furthermore, it will dutifully keep working until it has gone through each and every subfolder and sub-subfolder and sub-sub-subfolder and &#8211; well, until every last file has been listed.<\/P>\n<P>It all sounds terribly complicated, and it is. Fortunately, though, VBScript hides most of this complexity from you. Hey, would we kid you about something like that? Look, here\u2019s a script that uses a recursive function &#8211; ShowSubFolders &#8211; to list all the files in the folder C:\\Scripts and all the files found in any subfolders of C:\\Scripts:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjStartFolder = &#8220;C:\\Scripts&#8221;<\/p>\n<p>Set objFolder = objFSO.GetFolder(objStartFolder)\nWscript.Echo objFolder.Path\nSet colFiles = objFolder.Files\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\nWscript.Echo<\/p>\n<p>ShowSubfolders objFSO.GetFolder(objStartFolder)<\/p>\n<p>Sub ShowSubFolders(Folder)\n    For Each Subfolder in Folder.SubFolders\n        Wscript.Echo Subfolder.Path\n        Set objFolder = objFSO.GetFolder(Subfolder.Path)\n        Set colFiles = objFolder.Files\n        For Each objFile in colFiles\n            Wscript.Echo objFile.Name\n        Next\n        Wscript.Echo\n        ShowSubFolders Subfolder\n    Next\nEnd Sub\n<\/PRE>\n<P>As you can see the first part of the script is exactly the same as the one we saw before (with one exception: this one echoes the folder path so we know which folder we\u2019re looking at). After showing all the files in C:\\Scripts, it then calls the recursive function ShowSubFolders. This function then starts sifting through all the subfolders and reports back any files found. All this \u201crecursion\u201d happens automatically; all you have to do is just sit back and watch.<\/P>\n<P>If you don\u2019t fully understand what\u2019s happening here, don\u2019t worry about it; it takes awhile to fully grasp how recursive functions work. In the meantime, just copy the preceding script and use it any time you need to do something to all the files in a folder and its subfolders.<\/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\/oct04\/hey1020.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\/oct04\/hey1020.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use a script to show me all the files in a folder? And then how can I modify that script so it shows me all the files in any subfolders of that folder?&#8212; CS Hey, CS. Yesterday we showed everyone a script that changed all the files in a [&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,11,3,12,5],"class_list":["post-71183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use a script to show me all the files in a folder? And then how can I modify that script so it shows me all the files in any subfolders of that folder?&#8212; CS Hey, CS. Yesterday we showed everyone a script that changed all the files in a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71183","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=71183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71183\/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=71183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}