Write a c program command line program (in Windows) to find names of files and d
ID: 3709066 • Letter: W
Question
Write a c program command line program (in Windows) to find names of files and directories that match a specified pattern.
1. The program must be called walker.exe.
2. The program must take two command line arguments – one for the starting directory, and the other as a regular expression describing the pattern that must be matched by the name of the directory or file.
3. The output of the program should display all the directory and file names that match the search expression.
a. If there is nothing that matched, you should state, “No file or directory found.”
b. For each directory found, display the full directory path, each on a separate line.
c. For each file found, display the full file path, each on a separate line.
4. To walk the directory structure, a recursive algorithm must be used.
Should be written in C or C++.
WRITTEN FOR WINDOWS, NO DOWNLOADED LIBRARIES MAY BE USED INCLUDING dirent.h
Explanation / Answer
// File Name: FilesDirectoryList.cpp
#include <iostream>
#include <algorithm>
#include <string>
#include <dirent.h>
using namespace std;
// Returns true if the extension of the file matches the file in the directory otherwise returns true
bool checkAvailable(const string &direcotyFileName, const string &fileExtension)
{
return (direcotyFileName.size() >= fileExtension.size()) && equal(fileExtension.rbegin(), fileExtension.rend(), direcotyFileName.rbegin());
}// End of function
// main function definition
int main(int argc, char **argv)
{
// To store file extension entered by the user
string extension;
// To store the directory name entered by the user with path
string directoryPath;
// Accepts directory name entered by the user
cout << "Directory name with path: ";
getline(cin, directoryPath);
// Accepts file extension
cout << "Enter the file extension to search: ";
getline(cin, extension);
// Creates a directory pointer.
// Opens the directory with entered directory path by the user
DIR *myDirectory = opendir(directoryPath.c_str());
// Checks if the directory exits or not
if(!myDirectory)
{
cout<<" No such directory "<<directoryPath;
return 1;
}// End of if condition
// Declares a pointer of type struct dirent
dirent *extractFile;
// Loops till directory contents are not null
while((extractFile = readdir(myDirectory)) != NULL)
{
// Calls the function to check if the file name in the directory with the extension specified by the user available or not
// d_name is a data member of struct dirent which stores the file name
if(checkAvailable(extractFile->d_name, extension))
// If available display the file name
cout << extractFile->d_name << endl;
}// End of while loop
// Close the directory
closedir(myDirectory);
}// End of main functions
Sample Output 1:
Directory name with path: E:TODAYJan
Enter the file extension to search: .exe
FileBinaryAccount.exe
FilesDirectoryList.exe
RockPaperScissors.exe
Sample Output 2:
irectory name with path: E:TODAYJan
Enter the file extension to search: .txt
act9A.txt
address.txt
Assignment.txt
BoardInput.txt
BSTResult.txt
BSTStringData.txt
chessBoard.txt
Command.txt
csis.txt
CustomerAccount.txt
CustomerAccount1.txt
Data.txt
Data1_in.txt
Data1_out.txt
dataset.txt
dictionary_four_letter_words.txt
dna.txt
employee.txt
EVENNUM.txt
GradeAssignment.txt
Graph1.txt
Graph2.txt
Graph3.txt
inputFile.txt
input_txt.txt
Library.txt
maze_flood.txt
name.txt
NUMBERS.txt
ODDNUM.txt
product.txt
Prog3.txt
QuickSortData.txt
save.txt
ScoreJudge.txt
StateCapitals.txt
StringData.txt
stu.txt
Submission.txt
terrain.txt
testFile.txt
treeData.txt
universities.txt
universities_res.txt
Sample Output 3:
Directory name with path: F:PhotoBangal
Enter the file extension to search: .jpg
No such directory F:PhotoBangal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.