Britec Tech Support Forum

Full Version: Command to list all files in a folder as well as sub-folders
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys I have been looking all day to find a command which will list all files on a drive including files in  sub folders and save the list to a file.
I create a .bat file from notepad with these instructions and run the bat file from the root of the drive which contains the files and sub folders.

dir /b /s /a:-d  >list.txt

I get a file called list.txt with entries like this

E:\New Movies\The file test.mp4

But I would like to just have it list like this

The file test.mp4

I dont need to know the directories, file only. and for some reason when run on an external hard drive It picks up the recycled folder like this,

E:\$RECYCLE.BIN\S-1-5-21-2095992690-2580178275-4034271705-1001\$I3EZY6W.avi

I want to scrub that out as well.

Cheers for any help.
Try this:

  1. Copy all code below. 
  2. Paste to notepad and Create a vbs file ie: list.vbs
  3. Change directory location GetFiles("YOUR LOCATION") to wherever you want. Example: GetFiles("E:\Downloads")
  4. Right Click list.vbs and open in command prompt
  5. It will create a OutputFiles.csv in the directory you run list.vbs
Code:
Dim fso
Dim ObjOutFile

Set fso = CreateObject("Scripting.FileSystemObject")

Set ObjOutFile = fso.CreateTextFile("OutputFiles.csv")

ObjOutFile.WriteLine("Type,File Name,File Path")

GetFiles("YOUR LOCATION")

ObjOutFile.Close

WScript.Echo("Completed")

Function GetFiles(FolderName)
   On Error Resume Next

   Dim ObjFolder
   Dim ObjSubFolders
   Dim ObjSubFolder
   Dim ObjFiles
   Dim ObjFile

   Set ObjFolder = fso.GetFolder(FolderName)
   Set ObjFiles = ObjFolder.Files

   For Each ObjFile In ObjFiles
   ObjOutFile.WriteLine("File," & ObjFile.Name & "," & ObjFile.Path)
   Next

   Set ObjSubFolders = ObjFolder.SubFolders

   For Each ObjFolder In ObjSubFolders

       ObjOutFile.WriteLine("Folder," & ObjFolder.Name & "," & ObjFolder.Path)


       GetFiles(ObjFolder.Path)
   Next

End Function
(07-02-2016, 12:42 PM)Britec Wrote: [ -> ]Try this:



  1. Copy all code below. 
  2. Paste to notepad and Create a vbs file ie: list.vbs
  3. Change directory location GetFiles("YOUR LOCATION") to wherever you want. Example: GetFiles("E:\Downloads")
  4. Right Click list.vbs and open in command prompt
  5. It will create a OutputFiles.csv in the directory you run list.vbs
Code:
Dim fso
Dim ObjOutFile

Set fso = CreateObject("Scripting.FileSystemObject")

Set ObjOutFile = fso.CreateTextFile("OutputFiles.csv")

ObjOutFile.WriteLine("Type,File Name,File Path")

GetFiles("YOUR LOCATION")

ObjOutFile.Close

WScript.Echo("Completed")

Function GetFiles(FolderName)
   On Error Resume Next

   Dim ObjFolder
   Dim ObjSubFolders
   Dim ObjSubFolder
   Dim ObjFiles
   Dim ObjFile

   Set ObjFolder = fso.GetFolder(FolderName)
   Set ObjFiles = ObjFolder.Files

   For Each ObjFile In ObjFiles
   ObjOutFile.WriteLine("File," & ObjFile.Name & "," & ObjFile.Path)
   Next

   Set ObjSubFolders = ObjFolder.SubFolders

   For Each ObjFolder In ObjSubFolders

       ObjOutFile.WriteLine("Folder," & ObjFolder.Name & "," & ObjFolder.Path)


       GetFiles(ObjFolder.Path)
   Next

End Function

Did this work for you mate? I guess you would have tried it, it created a file but only 1 entry in it.
Thanks for the help mate.
Yeah it works
(07-03-2016, 09:54 PM)Britec Wrote: [ -> ]Yeah it works

And works brilliantly I might add, and lightning fast really incredible!

Is there any way to just list the file without the location and just search for .avi/.mp4/

Brilliant job mate.
Nah not with that script, glad it works for you.