Sometimes you might require to get a list of all the files in a folder, I was thinking how it can be implemented, the solution is to used the WSH interface and write a script in VBScript. Below is a sample script which will get the list of files on a directory, say c:\windows and save it to the file c:\windows\FileList.txt. Code: On Error Resume Next Dim fso, folder, files, NewsFile,sFolder Set fso = CreateObject("Scripting.FileSystemObject") sFolder = Wscript.Arguments.Item(0) If sFolder = "" Then Wscript.Echo "No Folder parameter was passed" Wscript.Quit End If Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True) Set folder = fso.GetFolder(sFolder) Set files = folder.Files For each folderIdx In files NewFile.WriteLine(folderIdx.Name) Next NewFile.Close Example usage: Code: lister.vbs "c:\documents and settings" More VB Articles VB.Net Array Basics Variables in Visual Basic Strings in VB - Part I Binary Conversion In VB VB6 Small Backup Program
Hey, i would like to enchance it by providing a dialog box. However, i failed to make it work. Care to point me on the right direction?
Code: On Error Resume Next Const WINDOW_HANDLE = 0 Const BIF_EDITBOX = &H10 Const BIF_NONEWFOLDER = &H0200 Const BIF_RETURNONLYFSDIRS = &H1 Set objShell = CreateObject("Shell.Application") Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") '**Browse For Folder To Be Processed strPrompt = "Please select the folder to process." intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX strTargetPath = wshShell.SpecialFolders("MyDocuments") strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath) Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\filelist.txt", True) Set objFolder = objFSO.GetFolder(strFolderPath) Set objColFiles = objFolder.Files For Each file In objColFiles objNewFile.WriteLine(file.Name) Next objNewFile.Close '**Browse4Folder Function Function Browse4Folder(strPrompt, intOptions, strRoot) Dim objFolder, objFolderItem On Error Resume Next Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot) If (objFolder Is Nothing) Then Wscript.Quit End If Set objFolderItem = objFolder.Self Browse4Folder = objFolderItem.Path Set objFolderItem = Nothing Set objFolder = Nothing End Function Here ya go. I would change the createtext file to another location, such as to 'c:\'. You can also change the strTargetPath to any valid path.
It's really simple ! You just need to check <folder>.folders instead of <folder>.files. Code: On Error Resume Next Const WINDOW_HANDLE = 0 Const BIF_EDITBOX = &H10 Const BIF_NONEWFOLDER = &H0200 Const BIF_RETURNONLYFSDIRS = &H1 Set objShell = CreateObject("Shell.Application") Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") '**Browse For Folder To Be Processed strPrompt = "Please select the folder to process." intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX strTargetPath = wshShell.SpecialFolders("MyDocuments") strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath) Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\filelist.txt", True) Set objFolder = objFSO.GetFolder(strFolderPath) ' CHANGE STARTS HERE : Set objColFolders = objFolder.Folders For Each tfolder In objColFolders objNewFile.WriteLine(tfolder.Name) Next ' CHANGE DONE. objNewFile.Close '**Browse4Folder Function Function Browse4Folder(strPrompt, intOptions, strRoot) Dim objFolder, objFolderItem On Error Resume Next Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot) If (objFolder Is Nothing) Then Wscript.Quit End If Set objFolderItem = objFolder.Self Browse4Folder = objFolderItem.Path Set objFolderItem = Nothing Set objFolder = Nothing End Function
HI hi, very nice a wonderful example code and i thank u really great......... Staffing service Seo master
This is definitley a great example. How can it be modified to list files in the main directory and any subdirectories? I'd like to write something that will give me the name, create date, modify date for all files in the main directory and for any files in the sub-directories. I wrote the following, but it can not handle sub-directories. Any help is appreciated. Thanks. On Error Resume Next Dim fso, folder, files, NewsFile,sFolder Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder("C:\Documents and Settings\Phat Buddha\desktop\") Set outfile = fso.CreateTextFile("c:\temp\testout.txt") Set files = folder.Files For each folderIdx In files outfile.WriteLine(folderIdx.Name & ";" & folderIdx.DateCreated & ";" & folderIdx.DateLastModified) Next outfile.Close
Hi: I am a novice to VBS. I was wodering if it is possible to read thru the files in a folder and capture the last line in every file(csv file) and write to a logfile that has the name(with date and time) Also, is it possible that when capturing the last line , I have to extract the last field value compare it with a base value and if it is greater than the base value then write it the log file. Since it loops thru each file in the folder, base values for each file keep changing. I am not quite sure how to do it. Any help would be great. Thanks, Yukta.
Hallo This is what i was surching for a very long time. But i must filter the files i want listed in the .txt file by the extension of the files.. could someone help me?
I have tried this in both Windows Vista, and again on XP (thinking that maybe Vista had issues), and I cannot get this to read the folders. It just creates an empty 'filelist.txt' file. I have not modified this script in any way, and have tried diff. folders. Any suggestions from anyone?
Hi, Thanx for reporting. There was a typo at this line :Set objColFolders = objFolder.Folders It should read :Set objColFolders = objFolder.SubFolders. Correct code [ TESTED ] is : Code: On Error Resume Next Const WINDOW_HANDLE = 0 Const BIF_EDITBOX = &H10 Const BIF_NONEWFOLDER = &H0200 Const BIF_RETURNONLYFSDIRS = &H1 Set objShell = CreateObject("Shell.Application") Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") '**Browse For Folder To Be Processed strPrompt = "Please select the folder to process." intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX strTargetPath = wshShell.SpecialFolders("MyComputer") strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath) Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\filelist.txt", True) Set objFolder = objFSO.GetFolder(strFolderPath) ' CHANGE STARTS HERE : Set objColFolders = objFolder.SubFolders For Each tfolder In objColFolders objNewFile.WriteLine(tfolder.Name) Next ' CHANGE DONE. objNewFile.Close '**Browse4Folder Function Function Browse4Folder(strPrompt, intOptions, strRoot) Dim objFolder, objFolderItem On Error Resume Next Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot) If (objFolder Is Nothing) Then Wscript.Quit End If Set objFolderItem = objFolder.Self Browse4Folder = objFolderItem.Path Set objFolderItem = Nothing Set objFolder = Nothing End Function
How do i code such that, the filename that is captures will be stored into an array of strings? Thanks!
Is it possible to tiune this skript, so I get a file contianing fileextention, size and owner of the file? (I found something similar in asp: classicasp.aspfaq.com/files/directories-fso/how-do-i-find-the-owner-author-and-other-properties-of-a-file.html but I dont know how to do so in vbs or similar
This is very good and fairly simple. It would be nice if you explained each line for those not familiar with VB
Hi here, Very good example guys. However, I would like to push a little bit ... Let's say I have a folder containing 140k .CSVs files (avg size 5k) and I want to list them all files not to a file but to memory into an array or something... so then after listing I would be about concatenate all those files into one single big.CSV file. is that possible? Rom