Questions tagged [readdir]

The C, Perl or PHP library function for reading from an open directory.

The readdir C library function reads and returns the next entry, as a struct dirent *, from an open DIR *. You should open the directory using opendir first. This can also refer to the Perl or PHP functions with the same purpose.

Links:

301 questions
6
votes
2 answers

Microsoft Visual Studio: opendir() and readdir(), how?

I've used this kind of code in my Dev-cpp before: if((dh = opendir(folder)) !== false){ while((file = readdir(dh)) !== false){ // do my stuff } closedir(dh); } But now i am using MSVC++ and i dont know how to add those files…
Newbie
6
votes
1 answer

How is "0" result from readdir() not false in a while condition?

See also: Where in the documentation does it say that while tests readdir for definedness?. (Not a duplicate; just closely related.) Many people treat the loop below as idiomatic: while (defined(my $file = readdir($dir)) { ... } instead…
Alnitak
  • 334,560
  • 70
  • 407
  • 495
6
votes
3 answers

C: Checking the type of a file. Using lstat() and macros doesn't work

I use opendir() to open a directory and then readdir() and lstat() to get the stats of each file in that directory. Following this manpage I wrote the code under which doesn't work as thought. It does list all the files in the current directory but…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
5
votes
1 answer

DT_DIR undefined

I want to check if file returned by readdir is directory. I tried do it using DT_DIR constant (as man readdir says) but it's undefined. What file should I include to get it? Now I use #include #include #include…
RiaD
  • 46,822
  • 11
  • 79
  • 123
5
votes
3 answers

Perl: Using Loop or Map/Grep?

I'm writing a program to step through a directory tree (Yes, I know about File::Find, but I'm writing a replacement). In my program, I'm doing a readdir on a whole directory and placing it in a list. I need to do two things: Remove . and .. from…
David W.
  • 105,218
  • 39
  • 216
  • 337
5
votes
1 answer

How do I avoid this race condition with readdir/inotify?

Suppose I want to invoke some command on all files in a directory and set a watch to invoke that command on all files that get created in that directory. If I do: while( ( sdi = readdir( d )) != NULL ) { ... } closedir( d ); /* Files created here…
William Pursell
  • 204,365
  • 48
  • 270
  • 300
5
votes
4 answers

Nodejs readdir - only find files

When reading a directory, I currently have this: fs.readdir(tests, (err, items) => { if(err){ return cb(err); } const cmd = items.filter(v => fs.lstatSync(tests + '/' + v).isFile()); k.stdin.end(`${cmd}`); …
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
5
votes
4 answers

How to search file in folder by using php?

I have a folder called allfiles, and there are some files in this folder, such as 1212-how-to-sddk-thosd.html 3454-go-to-dlkkl-sdf.html 0987-sfda-asf-fdf-12331.html 4789-how-to-fdaaf-65536.html I use scandir to list all files, and now I need to…
user2688045
5
votes
1 answer

Unix readdir (3) and stat (2) giving different inodes

I'm the teacher's assistant for my university's system's programming class. Lately the students have been working on an assignment that involves replicating the program pwd. Some of the students are noticing what appears to be an inconsistency. …
Thomas
  • 871
  • 2
  • 8
  • 21
5
votes
1 answer

readdir returning dirent with d_type == DT_UNKNOWN for directories . and

I have the following code that mimicks ls: #include #include char* dirent_type_to_str(unsigned char dirent_type) { switch (dirent_type) { case DT_DIR: return "Dir "; case DT_REG: return "File"; } …
James Ko
  • 32,215
  • 30
  • 128
  • 239
5
votes
0 answers

Golang bug ioutil.ReadDir() listing files on CIFS share? Or, doing something wrong?

Trying to get a list of filenames from a directory that is mounted to a CIFS file system. When there are a certain number of files (>55) that are in a sequence, i.e. 00000.xxx through 00299.xxx, some files are missing from the list (e.g. file…
Keith Hogan
  • 117
  • 1
  • 9
5
votes
2 answers

Perl's 'readdir' Function Result Order?

I am running Perl in Windows and I am getting a list of all the files in a directory using readdir and storing the result in an array. The first two elements in the array seem to always be "." and "..". Is this order guaranteed (assuming the…
Hans Goldman
  • 564
  • 3
  • 12
5
votes
2 answers

Is scandir really thread safe?

In the UNIX® System Threads Reference, under the heading of "Thread-safety" is a list of functions are "not guaranteed to be thread-safe on all UNIX systems." The function scandir() is absent from this list, while readdir() appears on the list. …
Mike Godin
  • 3,727
  • 3
  • 27
  • 29
5
votes
2 answers

PHP readdir and sort

I'm making a little gallery. I want to read the file names off a directory and print the file names below after I've stripped some leading numerals and file extensions. I have two versions of the code. Version 1 doesn't sort $current_dir =…
Marino
4
votes
6 answers

How can I speed up Perl's readdir for a directory with 250,000 files?

I am using Perl readdir to get file listing, however, the directory contains more than 250,000 files and this results long time (longer than 4 minutes) to perform readdir and uses over 80MB of RAM. As this was intended to be a recurring job every 5…
Walinmichi
  • 385
  • 2
  • 5
  • 12
1
2
3
20 21