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? - CS 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’s column, we’d 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.
Let’s 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: Set objFSO = CreateObject('Scripting.FileSystemObject') objStartFolder = 'C: Scripts' Set objFolder = objFSO.GetFolder(objStartFolder) Set colFiles = objFolder.Files For Each objFile in colFiles Wscript.Echo objFile.Name Next As you can see, there really isn’t much to this.
The path is required to be set for using tools such as javac, java, etc. If you are saving the Java source file inside the JDK/bin directory, the path is not required to be set because all the tools will be available in the current directory. Jun 25, 2017 I cant open a Word or Excel document - Office 2011 on a mac and I have the latest update installed 'The OPEN XML file Normal.dotm can not be opened because there are problems with the contents or the file name might contain invalid characters (for example, / ) ' This is not true.
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’d have to do is change the path accordingly, something we do by assigning a different value to objStartFolder: objStartFolder = 'C: Windows' After we’ve connected to the folder, we then create a reference to the Files property using this command: Set colFiles = objFolder.Files 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 - this collection does not include files found in any subfolders of C: Scripts.) At this point the rest is child’s play: we can now use a For Each loop to loop through the collection of files and - if we choose - 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 in the Microsoft Windows 2000 Scripting Guide.
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 plus 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’re not going to explain recursion here; for more details, check out this portion of the.
(Yes, we do seem to be promoting the book quite a bit today, don’t 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’t matter if there are subfolders in the folder; the script doesn’t really care. A recursive function, by contrast, does 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 - well, until every last file has been listed.
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?
Hi, Here is a version of the same script with a few changes One, it has been converted to run within Classic ASP, but also so that it is not necessary to start the tree outside of the recursive sub routine. That's accomplished by checking to see if the argument is an object or path (path on the first initiation running the first folder loop) Other changes are some naming convention and the name of the subroutine itself to ShowFoldersAndFiles since everthing is within one routine with only the FileSystemObject declared outside the Sub.
Also dimensioning (Dim) all variables so Option Explicit will work. Since this will be used for my own use, I provided a DEBUG sub routine that shows the output of each folder/file. That can be turned off by setting the Const DEBUGON to False ore removed altogether(but this give same output as the provided script); as I or anyone else may want to do things(other proceedures) within traversing of each of the folders and files, instead of just showing them. So with this, I'm not doing anything too different form ScriptingGuy1's example but, showing how it can be accomplished all with one routine.
To make this work in WSH, just uncomment the equivalent lines of code that work with WSH, and comment out the ASP specific lines or just remove them. Hope this helps someone wanting a more contained proceedure.Reuben:)+' & vbNewline 'Wscript.Echo Else Response.Write strFsoString & ' & vbNewline 'Wscript.Echo strFileSystem End If End If End Sub.
When I use this is traverses the same recursion path of folders twice. Can anybody please tell me why? Set objFSO = CreateObject('Scripting.FileSystemObject') objStartFolder = 'd:Folder' Set objRootFolder = objFSO.GetFolder(objStartFolder) Counter = 0 List = ShowSubFolders(objRootFolder, '–Start–') wscript.echo List Function ShowSubFolders(Folder, List) List = List & vbCRLF & Folder.Name For Each Subfolder in Folder.SubFolders Set objFolder = objFSO.GetFolder(Subfolder.Path) Set colFiles = objFolder.Files ShowSubFolders = List + ShowSubFolders(SubFolder, List) Next End Function. I have combined two or three source file to make a below scripts for directory listing including sub directories. It will be fully working even though there are some folders which you don't have permission to access.
Please notes that I just modified or added based on other programer's source. Brian —————————————————————————————————— On Error Resume Next Const WINDOWHANDLE = 0 Const BIFEDITBOX = &H10 Const BIFNONEWFOLDER = &H0200 Const BIFRETURNONLYFSDIRS = &H1 Dim depth Set objShell = CreateObject('Shell.Application') Set wshShell = CreateObject('WScript.Shell') Set objFSO = CreateObject('Scripting.FileSystemObject') depth = 0 '.Browse For Folder To Be Processed strPrompt = 'Please select the folder to process.'
![Excel Excel](/uploads/1/2/5/5/125583558/638205385.gif)
Yesterday morning my colleagues Excel 2011 (on Mac OSX 10.8) crashed. He reopened Excel and was asked if he wanted to recover the previous document.
Yes, he said and then got a document named previousfilename(version 1). To be certain that this document was saved he continously saved the (recovered) document manually during the day and by the end of the day he saved it finally and went home. Today there is NO trace of the document whatsoever. We have looked in original location, trash, userfolder/Microsoft userdata/Office 2011 AutoRecovery etcetera and there is no document except for the original unedited version. Spotlight doesn't find it either.
Since we have a running Time Machine backup we have lots of backups from yesterday but the same locations are also empty. This is a freshly installed product. If this behaviour is expected Excel then having no auto-recover function at all would actually be better. I hope someone out there might help out. Manually saving a document SHOULD leave a trace somewhere in the filesystem?
Finally we found the file in the folder: /Library/Application Support/Microsoft/Office/Office 2011 AutoRecovery As the /Library folder is a hidden folder since OS X 10.7 and not by default searched by Spotlight it's incredibly stupid that this folder is auto-suggested when saving a recovered file. Also since there IS a folder with the same name under Documents/Microsoft userdata/ that is empty this was hard to find! To go to this folder, open Finder and in the menu click Go » Go to Folder or press ⇧⌘G. Then, paste the path from above. Or, to make the /Library folder always visible, open Terminal.app from /Applications/Utilities and enter: chflags nohidden /Library.