-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathextension_lookup.cpp
More file actions
70 lines (61 loc) · 1.86 KB
/
extension_lookup.cpp
File metadata and controls
70 lines (61 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* This example code is written by AFASSI Mohamed
*
* It uses the Dirent library to search for files with specific extension in a
* directory. In this example I just print out the files found but instead
* you could manipulate them. For i.e. I used this script in a project to
* find csv files and format them into Binary files
*
* Compile this file with Visual Studio and run the produced command in
* console with an extension name and directory name argument.
*
* Copyright (C) 1998-2019 Toni Ronkko
* This file is part of dirent. Dirent may be freely distributed
* under the MIT license. For all details and documentation, see
* https://github.com/tronkko/dirent
*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <dirent.h>
#include <stdio.h>
#include <cstring>
#include <string.h>
using namespace std;
int
main(int argc, char *argv[])
{
string directory, extension;
DIR *di;
char *ptr1, *ptr2;
int retn;
struct dirent *dir;
cout << "What extension you search for? ";
cin >> extension;
cout << "Directory where you want to search for " + extension + " extension : ";
cin >> directory;
size_t length = directory.length();
char *char_array = new char[length + 1];
strcpy(char_array, directory.c_str());
size_t length2 = extension.length();
char *char_array2 = new char[length2 + 1];
strcpy(char_array2, extension.c_str());
di = opendir(char_array); //specify the directory name
if (!di) {
return 3;
}
while ((dir = readdir(di)) != NULL) {
ptr1 = strtok(dir->d_name, ".");
ptr2 = strtok(NULL, ".");
if (ptr2 != NULL) {
retn = strcmp(ptr2, char_array2);
if (retn == 0) {
cout << ptr1 << "\n";
}
}
}
closedir(di);
return 0;
}