Ways to list the files in a folder and all its subfolders? Q: Hello, scripting expert! The way to list all files within a folder and all files in all subfolders of that folder? --MA Answer: Hello, MA. Numerous users have asked this query, and we haven't had time for you to answer it but. That is because there is absolutely no powerful and very simple resolution to this difficulty: the script that could achieve this task is destined to be somewhat tough to understand, and cannot be solved in the basic and clear way that this column is utilised to. However, customers are always proper: should you will need a script that could list all files within a folder and all files in all subfolders of that folder, what else can we do What did you say? Just before beginning to write a script, two complications will have to be solved. 1st, you have to pick a scripting strategy. WMI, FileSystemObject, and Shell objects can list files in folders and subfolders in folders. Even so, none of these technologies can automatically list the files in these subfolders (to not mention the next amount of folders that could exist). Applying any of the above tactics can accomplish the aim, nevertheless it will not be incredibly uncomplicated. We tend to work with WMI. Scripts written making use of it may be far more complicated than related scripts written applying FileSystemObject or Shell objects, but the benefit of WMI scripts is the fact that retrieving such info on the regional computer system is as easy as retrieving from a remote computer system. Neither FileSystemObject nor Shell objects can do this. What we value is definitely the flexibility of WMI. Second, we noticed that none of those scripting procedures features a built-in system to finish the following operations: iterate through folders, list file names, and then automatically iterate via all subfolders and list files in them. As a result, recursive functions are essential to execute this task. Explaining google is beyond the scope of this column; to get a brief explanation, see the Microsoft Windows 2000 scripting guide. Suffice it to say that we desire to create a function that could call itself as a lot of instances as needed. In other words, if we've a function that could access a folder and list all the files in it, the function can get in touch with itself to access the subfolder and list each of the files in it, and then get in touch with itself once again to access the following 1 Level folder. It's difficult to illustrate this in an intuitive way, but this process does work. In addition, it creates a brand new trouble, which we'll talk about later. Let's initially look at a script, its part is usually to list a folder and all its subfolders (however the very first example script does not list any files in these folders): strComputer='.'SetobjWMIService=GetObject('winmgmts:\\\\'strComputer'\oot\\cimv2')strFolderName='c:\\scripts'SetcolSubfolders=objWMIService.ExecQuery_('AssociatorsofWin32_Directory.Name=''strFolderName'' '_'WhereAssocClass=Win32_Subdirectory'_'ResultRole=PartComponent')ForEachobjFolderincolSubfoldersGetSubFoldersstrFolderNameNextSubGetSubFolders(strFolderName)SetcolSubfolders2=objWMIService.ExecQuery_('AssociatorsofWin32_Directory.Name=''str'''str''''' )ForEachobjFolder2incolSubfolders2strFolderName=objFolder2.NameWscript.EchoobjFolder2.NameGetSubFoldersstrFolderNameNextEndSub The role of the above script is to use the AssociatorsOf query to get a list of all subfolders of the folder C:\\Scripts. The basic meaning of our query is to provide me with a list of all items related to the directory C:\\Scripts, but only if these items are subdirectories (WhereAssocClass=Win32_Subdirectory). This script gets a list of all top-level subfolders: for example, C:\\Scripts\\Folder1 and C:\\Scripts\\Folder2. It cannot get any lower-level folders; this query cannot return folders like C:\\Scripts\\Folder1\\SubfolderA. To get these next-level subfolders (subfolders of subfolders), we need to use recursive queries. The subroutine GetSubFolders can achieve this purpose. We will pass the name of each subfolder found (such as C:\\Scripts\\Folder1 and C:\\Scripts\\Folder2) to this subroutine one by one, so that it queries these subfolders for the next level of subfolders . If there is any sub-folder at the next level, the function will automatically call itself and find if there is a sub-folder at the next level. Feeling confused? Don't be frustrated; many people feel this way. But don't worry, just leave the code as it is and run it. To search other folders (that is, folders other than C:\\Scripts), simply change the value of the variable that contains the folder to be searched. For example, if you want to search C:\\Windows, use this line of code: strFolderName='c:\\windows' So, how to list all the files in these folders? From now on, the situation has indeed become more complicated. This is because we need to execute another query: we use one query to get the names of all subfolders, and then another query to get the collection of files in each folder. This second query happens to be very similar to the following: SetcolFiles=objWMIService.ExecQuery_('Select*fromCIM_DataFilewherePath=''strPath''') This is not too complicated, but the following situation is different: In such queries, you must escape the \\(used in the file path Two \\\\). You cannot use C:\\Scripts\\Folder1\\ in the query; you must use C:\\\\Scripts\\\\Folder1\\\\. You will find that the code in the script replaces each \\ with \\\\; this is exactly what we need to do when referencing file paths in such queries. A considerable portion of the script is dedicated to converting folder path names so that they can be used in queries. There are so many things to note. The following is a script that every user wants to see quickly: strComputer='.'SetobjWMIService=GetObject('winmgmts:\\\\'strComputer'\oot\\cimv2')strFolderName='c:\\scripts'SetcolSubfolders=objWMIService.ExecQuery_('AssociatorsofWin32_Directory.Name=''strFolderName'' '_'WhereAssocClass=Win32_Subdirectory'_'ResultRole=PartComponent')Wscript.EchostrFolderNamearrFolderPath=Split(strFolderName,'\\')strNewPath=''Fori=1toUbound(arrFolderPath)strNewPath=strNewPath'\\\\'arrFolderPath(i)NextstrPath= '\\\\'SetcolFiles=objWMIService.ExecQuery_('Select*fromCIM_DataFilewherePath=''strPath''')ForEachobjFileincolFilesWscript.EchoobjFile.NameNextForEachobjFolderincolSubfoldersGetSubFoldersstrFolderNameNextSubGetSubFolders(strFolderName)SetcolSubfold' _'WhereAssocClass=Win32_Subdirectory'_'ResultRole=PartComponent')ForEachobjFolder2incolSubfolders2strFolderName=objFolder2.NameWscript.EchoWscript.EchoobjFolder2.NamearrFolderPath=Split(strFolderName,'\\')strNewPath='(Fori=1='Fori rFolderPath)strNewPath=strNewPath'\\\\'arrFolderPath(i)NextstrPath=strNewPath'\\\\'SetcolFiles=objWMIService.ExecQuery_('Select*fromCIM_DataFilewherePath=''strPath''')ForEachobjFileincolFilesWscript.EchoobjFile.NameNextGetSubFoldersstrFold . Nevertheless it truly operates! how to recover deleted files from pendrive free performed by this script are as follows: bind to the C:\\Scripts folder and echo the names of all files in it, then receive a list of all subfolders in C:\\Scripts. Then iterate by means of the collection of subfolders and call the recursive function GetSubFolders for every single subfolder. This function will list all of the files inside the subfolder, after which check irrespective of whether there is certainly a subfolder inside the subfolder. If there's, the recursive function are going to be named once more; continue to repeat this course of action until it could no longer continue, that is, all the files in C:\\Scripts and all its subfolders are listed. Tomorrow, it's actually time for you to go back and discuss a simple trouble to solve. Does any person wish to understand how to utilize a script to get the name of the nearby computer system? |