Getting the filenames of all files in a folder
You could do it like that:
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
Do you want to only get JPEG files or all files?
Java - how to get all the file names in a folder
File file = new File("C:\\");
File[] files = file.listFiles();
for (File f:files)
{
System.out.println(f.getAbsolutePath());
}
listFiles()
has more options. see the documentation here
How can I get the list of files in a directory using C or C++?
UPDATE 2017:
In C++17 there is now an official way to list files of your file system: std::filesystem
. There is an excellent answer from Shreevardhan below with this source code:
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}
Old Answer:
In small and simple tasks I do not use boost, I use dirent.h. It is available as a standard header in UNIX, and also available for Windows via a compatibility layer created by Toni Ronkko.
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
It is just a small header file and does most of the simple stuff you need without using a big template-based approach like boost (no offence, I like boost!).
Getting the names of all files in a directory with PHP
Don't bother with open/readdir and use glob
instead:
foreach(glob($log_directory.'/*.*') as $file) {
...
}
Batch File; List files in directory, only filenames?
The full command is:
dir /b /a-d
Let me break it up;
Basically the /b
is what you look for.
/a-d
will exclude the directory names.
For more information see dir /?
for other arguments that you can use with the dir
command.
Get all files within a folder containing string, push filenames to array and return array using Node FS
You should ditch callback syntax and use fs.promises Api. This looks much cleaner
const fs = require("fs").promises;
const path = require("path");
const dirname = path.resolve("./results/");
async function readDir(dirname) {
const allResults = [];
try {
const files = await fs.readdir(dirname);
for (const fileName of files) {
try {
const content = await fs.readFile(`${dirname}/${fileName}`, {
encoding: "utf-8"
});
if (content.includes("content string")) {
allResults.push(fileName);
}
} catch (error) {
console.log(error.message);
}
}
return allResults;
} catch (error) {
console.log(error);
}
}
readDir(dirname).then(data => {
console.log(data);
});
Related Topics
Regular Expression to Match Urls in Java
How to "Intercept" Ctrl+C in a Cli Application
What Is the Default Access Specifier in Java
Recommendations on a Free Library to Be Used for Zipping Files
Why Does Java Implicitly (Without Cast) Convert a 'Long' to a 'Float'
How to Load a Resource from Web-Inf Directory of a Web Archive
How to Add and Remove Elements of Enumeration at Runtime in Java
How to Get Pid of Process I'Ve Just Started Within Java Program
How to Properly Shutdown Java Executorservice
Java Replace Line in Text File
Where's My Invalid Character (Ora-00911)
Convert Rgb Values to Color Name
How to Read Jsch Command Output
Integer Wrapper Class and == Operator - Where Is Behavior Specified