top of page
Search

How to list the files in a folder and all its subfolders? .TXT

  • gormsenbladt70hoiw
  • Jun 11, 2020
  • 4 min read

How you can list the files in a folder and all its subfolders? Q: Hello, scripting expert! The best way to list all files inside a folder and all files in all subfolders of that folder?

--MA

Answer: Hello, MA. Quite a few customers have asked this query, and we have not had time for you to answer it yet. That is simply because there isn't any efficient and very simple option to this trouble: the script that will achieve this job is destined to be somewhat tough to understand, and can't be solved within the straightforward and clear way that this column is utilised to. However, clients are constantly right: in the event you want a script that could list all files in 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 need to be solved. Initially, you should pick a scripting technique. WMI, FileSystemObject, and Shell objects can list files in folders and subfolders in folders. However, how to recover partition table windows 7 of these technologies can automatically list the files in these subfolders (not to mention the following level of folders that might exist). Working with any in the above approaches can realize the goal, however it isn't quite effortless.

We tend to work with WMI. Scripts written using it may be a lot more complicated than equivalent scripts written utilizing FileSystemObject or Shell objects, but the advantage of WMI scripts is that retrieving such details on the local computer system is as hassle-free as retrieving from a remote laptop. Neither FileSystemObject nor Shell objects can do that. What we value will be the flexibility of WMI.

Second, we noticed that none of these scripting procedures has a built-in process to finish the following operations: iterate through folders, list file names, and then automatically iterate by means of all subfolders and list files in them. As a result, recursive functions are needed to carry out this task. Explaining recursion is beyond the scope of this column; for a brief explanation, see the Microsoft Windows 2000 scripting guide. Suffice it to say that we wish to make a function which can contact itself as lots of times as required. In other words, if we have a function that may access a folder and list each of the files in it, the function can get in touch with itself to access the subfolder and list each of the files in it, then get in touch with itself once again to access the next a single Level folder. It truly is hard to illustrate this in an intuitive way, but this method does operate.

In addition, it creates a new challenge, which we are going to go over later. Let's first look at a script, its role will be to list a folder and all its subfolders (however the initial instance script doesn't 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 \\\\). recover mbr partition table 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 diskpart recover partition table 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 . But it definitely works! The operations performed by this script are as follows: bind to the C:\\Scripts folder and echo the names of all files in it, and after that get a list of all subfolders in C:\\Scripts. Then iterate by recover lost partition table of the collection of subfolders and get in touch with the recursive function GetSubFolders for every subfolder. This function will list all the files inside the subfolder, and then check whether or not there is certainly a subfolder within the subfolder. If there's, the recursive function will probably be called once again; continue to repeat this process till it might no longer continue, which is, all the files in C:\\Scripts and all its subfolders are listed.

Tomorrow, it is actually definitely time for you to go back and talk about a simple trouble to resolve. Does recover lost partition table choose to understand how to utilize a script to get the name of the neighborhood personal computer?

 
 
 

Recent Posts

See All

Comments


Join my mailing list

Thanks for submitting!

© 2023 by The Book Lover. Proudly created with Wix.com

bottom of page