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 file in directories recursively
You could use this overload of Directory.GetFiles which searches subdirectories for you, for example:
string[] files = Directory.GetFiles(sDir, "*.xml", SearchOption.AllDirectories);
Only one extension can be searched for like that, but you could use something like:
var extensions = new List<string> { ".txt", ".xml" };
string[] files = Directory.GetFiles(sDir, "*.*", SearchOption.AllDirectories)
.Where(f => extensions.IndexOf(Path.GetExtension(f)) >= 0).ToArray();
to select files with the required extensions (N.B. that is case-sensitive for the extension).
In some cases it can be desirable to enumerate over the files with the Directory.EnumerateFiles Method:
foreach(string f in Directory.EnumerateFiles(sDir, "*.xml", SearchOption.AllDirectories))
{
// do something
}
Consult the documentation for exceptions which can be thrown, such as UnauthorizedAccessException if the code is running under an account which does not have appropriate access permissions.
If the UnauthorizedAccessException is a problem, then please see the fine answers at Directory.EnumerateFiles => UnauthorizedAccessException.
Go find files in directory recursively
The standard library's filepath
package includes Walk
for exactly this purpose: "Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root." For example:
libRegEx, e := regexp.Compile("^.+\\.(dylib)$")
if e != nil {
log.Fatal(e)
}
e = filepath.Walk("/usr/lib", func(path string, info os.FileInfo, err error) error {
if err == nil && libRegEx.MatchString(info.Name()) {
println(info.Name())
}
return nil
})
if e != nil {
log.Fatal(e)
}
How can I find all *.js file in directory recursively in Linux?
find /abs/path/ -name '*.js'
Edit: As Brian points out, add -type f
if you want only plain files, and not directories, links, etc.
C Recursive search for a file in a folder and subfolders
Use FindFirstFile
to list all the files in a directory, then compare with each file. Or do a recursive search and search each sub directory.
You can adjust the "wildcard" also.
This example looks for "something.png"
on "c:\\test"
, if it doesn't find it, it will look in sub directories of "c:\\test"
Make sure you don't do a recursive search on "C:\"
because that's going to go through all the files on the drive.
#include <stdio.h>
#include <Windows.h>
int findfile_recursive(const char *folder, const char *filename, char *fullpath)
{
char wildcard[MAX_PATH];
sprintf(wildcard, "%s\\*", folder);
WIN32_FIND_DATA fd;
HANDLE handle = FindFirstFile(wildcard, &fd);
if(handle == INVALID_HANDLE_VALUE) return 0;
do
{
if(strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
continue;
char path[MAX_PATH];
sprintf(path, "%s\\%s", folder, fd.cFileName);
if(_stricmp(fd.cFileName, filename) == 0)
strcpy(fullpath, path);
else if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
findfile_recursive(path, filename, fullpath);
if(strlen(fullpath))
break;
} while(FindNextFile(handle, &fd));
FindClose(handle);
return strlen(fullpath);
}
int main(void)
{
char fullpath[MAX_PATH] = { 0 };
if(findfile_recursive("c:\\test", "something.png", fullpath))
printf("found: %s\n", fullpath);
return 0;
}
How to recursively search for files with certain extensions?
You can use the following find command to do that:
find /path/to/search -iname '*.psd'
iname
does a case insensitive search.
Bash - What is a good way to recursively find the type of all files in a directory and its subdirectories?
This may help: How to recursively list subdirectories in Bash without using "find" or "ls" commands?
That said, I modified it to accept user input as follows:
#!/bin/bash
recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
recurse "$i"
elif [ -f "$i" ]; then
echo "file: $i"
fi
done
}
recurse $1
If you didn't want the files portion (which it appears you don't) then just remove the elif and line below it. I left it in as the original post had it also. Hope this helps.
How to search for files in different directories recursively in VSCode?
In my testing, using the globstar does provide the functionality you desire.
https://github.com/isaacs/node-glob#glob-primer:
**
If a "globstar" is alone in a path portion, then it matches zero
or more directories and subdirectories searching for matches. It does
not crawl symlinked directories.
So that ./path/to/folder/**/*.foo
for example searches within all subdirectories of folder
no matter how deep within files with the foo
extension.
Same at https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options:
**
to match any number of path segments, including none
Related Topics
Multiple Parallel.Foreach Loops in .Net
Unity How to Make a Visual Joystick in Unity
What Is the Equivalent of Java's Final in C#
How to Determine If a Type Implements an Interface with C# Reflection
C# Short/Long/Int Literal Format
How to Get the Last Day of a Month
Merge Two (Or More) Lists into One, in C# .Net
How to Run a Test Method with Multiple Parameters in Mstest
Preprocessor Directives in Razor
Enter "&" Symbol into a Text Label in Windows Forms
Strip the Byte Order Mark from String in C#
Reliably Stop System.Threading.Timer
.Net Core 3.0 Possible Object Cycle Was Detected Which Is Not Supported
How to Convert a Timespan to a Formatted String
Dictionary Returning a Default Value If the Key Does Not Exist