How to search a folder and all of its subfolders for files of a certain type
You want the Find module. Find.find
takes a string containing a path, and will pass the parent path along with the path of each file and sub-directory to an accompanying block. Some example code:
require 'find'
pdf_file_paths = []
Find.find('path/to/search') do |path|
pdf_file_paths << path if path =~ /.*\.pdf$/
end
That will recursively search a path, and store all file names ending in .pdf in an array.
Python: Searching a directory and subdirectories for a certain file type
You can do it using listdir
and endswith
to identify characters at the end of a string:
filetypes = ['cpp', 'hpp', 'ipp']
dir = "target directory"
files = [[f for f in os.listdir(dir) if f.endswith(type_)] for type_ in filetypes]
This will result in list of lists where each list will hold files of specific type.
Using grep to search for specific type of files in all subdirectories
You can use the following option of grep:
--include=GLOB
Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).
And for the line number you should use the -n
option.
From within the root of the folders you want to look into, you can use this final command:
grep -nr "Mutual_Values_23.0" --include="gnuout_mutual_*txt"
How can I recursively find all files in current and subfolders based on wildcard matching?
Use find
:
find . -name "foo*"
find
needs a starting point, so the .
(dot) points to the current directory.
Searching for All Files Containing a Specific Extention in a Folder and Subfolders
FileSearch
was removed from VBA in Office 2007. Thankfully it's not difficult to create your own routine for searching files using the FileSystemObject (add the Windows Scripting Runtime as a reference to get Intellisense code hints).
This is the one that I use - your list of files will be returned as a Collection by the FileList function. It should be simple to add a filter to this to only populate the collection with files of a particular extension.
[Note that you'll need to add the Windows Scripting Runtime reference as mentioned above since the objects are early bound in my example]
Function FileList(Path As String) As Collection
Dim FSO as New Scripting.FileSystemObject
Dim StartingFolder As Scripting.Folder
Set StartingFolder = FSO.GetFolder(Path)
Set FileList = New Collection
RecursiveGetFiles StartingFolder, FileList
End Function
Private Sub RecursiveGetFiles(StartingFolder As Scripting.Folder, ByRef FullFileList As Collection)
Dim File As Scripting.File
For Each File In StartingFolder.Files
FullFileList.Add File, File.Path
Next File
Dim SubFolder As Scripting.Folder
For Each SubFolder In StartingFolder.SubFolders
RecursiveGetFiles SubFolder, FullFileList
Next SubFolder
End Function
This code can then be called by some parent routine, i.e.
Sub Search(Path As String)
Dim ListOfFiles As Collection
Set ListOfFiles = FileList(Path)
Dim File As Scripting.File
For Each File In ListOfFiles
Debug.Print File.Name
Next File
End Sub
Recursively Find Files With Particular File Extension and Content
What do you mean by keyword? Is that a word, present inside the file? Or is it a part of the filename?
In case it's the part of the filename, you can use file:
in the Windows search, like in following example:
file:*keyword*.py
This will show you all files, called *keyword*.py
. After you've done that, you might change your Windows explorer's view, clicking on the "View" tab and choose "Details", this will also show you the directory where those files are located.
Find all files in a directory with extension .txt in Python
You can use glob
:
import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)
or simply os.listdir
:
import os
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))
or if you want to traverse directory, use os.walk
:
import os
for root, dirs, files in os.walk("/mydir"):
for file in files:
if file.endswith(".txt"):
print(os.path.join(root, file))
Related Topics
Best Practices For Reusing Code Between Controllers in Ruby on Rails
How Would You Parse a Url in Ruby to Get the Main Domain
Installing Jekyll Without Root
Nokogiri Gem Installation Error
How to Use "Gets" and "Gets.Chomp" in Ruby
Stop Devise from Clearing Session
Difference Between Gemfile and Gemfile.Lock in Ruby on Rails
Best Practices for New Rails Deployments on Linux
What Will Give Me Something Like Ruby Readline with a Default Value
Rails - Best-Practice: How to Create Dependent Has_One Relations
Serving Static Files With Sinatra
How to Display Ruby on Rails Form Validation Error Messages One At a Time
How to Avoid Nomethoderror For Nil Elements When Accessing Nested Hashes
Undefined Method Attr_Accessible
Why Do Two Strings Separated by Space Concatenate in Ruby
How to Understand the Difference Between Class_Eval() and Instance_Eval()
Using Rvm on Ubuntu 12.04 to Use Rails. the Program 'Rails' Is Currently Not Installed