readdir

How can I read the files in a directory in sorted order?

我的梦境 提交于 2021-02-07 06:15:14
问题 When I read a directory in Perl with opendir , readdir , and closedir , the readdir function doesn't seem to read the files in any specific order (that I can tell). I am reading a directory that has subdirectories named by epoch timestamp: 1224161460 1228324260 1229698140 I want to read in these directories in numerical order, which would put the oldest directories first. When I use readdir , the first one it reads is 1228324260, which is the middle one. I know I could put the directory

Allocating a struct dirent without malloc()

折月煮酒 提交于 2020-12-12 11:38:47
问题 I need to use readdir_r() to read the contents of a directory in a multithreaded program. Since the size of struct dirent is filesystem dependent, man readdir_r recommends name_max = pathconf(dirpath, _PC_NAME_MAX); if (name_max == -1) /* Limit not defined, or error */ name_max = 255; /* Take a guess */ len = offsetof(struct dirent, d_name) + name_max + 1; to find the size of the allocation needed. To allocate it entryp = malloc(len); is called, and finally readdir_r() uses it like this:

fs.readdir ignore directories

北慕城南 提交于 2020-08-01 10:47:06
问题 I would like to scan the folder, but ignore all the folders/directories that are included in it. All I have in the (C:/folder/) are .txt files and other folders, I just want to scan the txt files, and ignore the folders. app.get('/generatE', function (req, res) { const logsFolder = 'C:/folder/'; fs.readdir(logsFolder, (err, files) => { if (err) { res.send("[empty]"); return; } var lines = []; files.forEach(function(filename) { var logFileLines = fs.readFileSync (logsFolder + filename, 'ascii'

How can I list all files in a directory sorted alphabetically using PHP?

帅比萌擦擦* 提交于 2020-04-05 15:52:27
问题 I'm using the following PHP code to list all files and folders under the current directory: <?php $dirname = "."; $dir = opendir($dirname); while(false != ($file = readdir($dir))) { if(($file != ".") and ($file != "..") and ($file != "index.php")) { echo("<a href='$file'>$file</a> <br />"); } } ?> The problem is list is not ordered alphabetically (perhaps it's sorted by creation date? I'm not sure). How can I make sure it's sorted alphabetically ? 回答1: The manual clearly says that: readdir

Does readdir() guarantee an order?

跟風遠走 提交于 2020-01-18 09:52:28
问题 I'm getting a list of files on a linux-like system using opendir/readdir. It appears that the directory entries are returned in alphabetical order of file name. However, I don't see anything in the man pages about this order being guaranteed. Can anyone tell me whether or not readdir guarrantees an order? 回答1: The readdir method doesn't guarantee any ordering. If you want to ensure they are sorted alphabetically you'll need to do so yourself. Note: I searched for a bit for definitive

Does readdir() guarantee an order?

不打扰是莪最后的温柔 提交于 2020-01-18 09:52:05
问题 I'm getting a list of files on a linux-like system using opendir/readdir. It appears that the directory entries are returned in alphabetical order of file name. However, I don't see anything in the man pages about this order being guaranteed. Can anyone tell me whether or not readdir guarrantees an order? 回答1: The readdir method doesn't guarantee any ordering. If you want to ensure they are sorted alphabetically you'll need to do so yourself. Note: I searched for a bit for definitive

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

陌路散爱 提交于 2020-01-13 09:36:10
问题 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 of: while (my $file = readdir($dir)) { ... } because supposedly with the latter version if the filename is just "0" (zero) it should terminate the loop, whereas it returns 'undef' when there are no more files. However at some point in the past this test for

How to search file in folder by using php?

落花浮王杯 提交于 2020-01-05 07:32:03
问题 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 find the file by with keywords, example to-dlkkl is the keyword, and I will get the file 3454-go-to-dlkkl-sdf.html . Glob seems not work, and opendir and readdir are not work well, any ideas? 回答1: Use loop foreach and strpos function: $files = scandir

How to search file in folder by using php?

雨燕双飞 提交于 2020-01-05 07:31:51
问题 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 find the file by with keywords, example to-dlkkl is the keyword, and I will get the file 3454-go-to-dlkkl-sdf.html . Glob seems not work, and opendir and readdir are not work well, any ideas? 回答1: Use loop foreach and strpos function: $files = scandir

PHP Get dimensions of images in dir

假装没事ソ 提交于 2019-12-30 11:18:06
问题 I have a huge ammount of photos that need sorting through. I need to know the dimensions of each photo in order to know or it needs re-sizing. As a programmer I'm convinced there must be a quicker way of doing this. I got quite far. The following code reads the dir and all the sub dirs. But the moment I try to extract the dimensions the loop halts at 8% of all the pictures that need checking. Could it be PHP is not allowed to do more calculations? What is going on!? This is how far I got: