readdir

Sorting a directory in perl, taking numbers into account

安稳与你 提交于 2019-12-04 18:20:20
I think I need some sort of Schwartzian Transform to get this working, but I'm having trouble figuring it out, as perl isn't my strongest language. I have a directory with contents as such: album1.htm album2.htm album3.htm .... album99.htm album100.htm I'm trying to get the album with the highest number from this directory (in this case, album100.htm). Note that timestamps on the files are not a reliable means of determining things, as people are adding old "missing" albums after the fact. The previous developer simply used the code snippet below, but this clearly breaks down once there are

nodejs express fs iterating files into array or object failing

匿名 (未验证) 提交于 2019-12-03 02:59:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So Im trying to use the nodejs express FS module to iterate a directory in my app, store each filename in an array, which I can pass to my express view and iterate through the list, but Im struggling to do so. When I do a console.log within the files.forEach function loop, its printing the filename just fine, but as soon as I try to do anything such as: var myfiles = []; var fs = require('fs'); fs.readdir('./myfiles/', function (err, files) { if (err) throw err; files.forEach( function (file) { myfiles.push(file); }); }); console.log(myfiles

PHP Scandir returns extra periods

匿名 (未验证) 提交于 2019-12-03 02:28:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So I am trying to build a script that scans a directory and returns random images to be used as backgrounds. The php looks like this: $dir = "views/img/bg/"; $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } $random_key = array_rand($files, 1); $random = $files[$random_key]; Then I am just using some simple jquery to attach the images as backgrounds: <script> $(document).ready(function(){ $("body").css( "background" , "url(http://'.$url_root.'/views/img/bg/'.$random.'), center center" ); }); </script

K&amp;R interface for reading directories: superfluous DIR structure?

匿名 (未验证) 提交于 2019-12-03 01:34:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In the 2nd edition of "The C Programming Language" by Kernighan and Ritchie they implement a simplified version of the UNIX command ls (section 8.6 "Example - Listing Directories" , p. 179). For this purpose they create the following interface which provides a system-independent access to the name and inode number of the files stored in a directory. #define NAME_MAX 14 /* longest filename component; */ /* system dependent */ typedef struct { /* portable director-entry */ long ino; /* inode number */ char name[NAME_MAX+1]; /* name + '\0'

Checking if a dir. entry returned by readdir is a directory, link or file

匿名 (未验证) 提交于 2019-12-03 01:14:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am making a program which is run in a Linux shell, and accepts an argument (a directory), and displays all the files in the directory, along with their type. Output should be like this: If no argument is made, it uses the current directory. Here is my code: #include #include #include int main(int argc, char *argv[]) { struct stat info; DIR *dirp; struct dirent* dent; //If no args if (argc == 1) { argv[1] = "."; dirp = opendir(argv[1]); // specify directory here: "." is the "current directory" do { dent = readdir(dirp); if (dent) { printf("

How can I list all files in a directory using Perl?

匿名 (未验证) 提交于 2019-12-03 00:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I usually use something like my $dir="/path/to/dir"; opendir(DIR, $dir) or die "can't open $dir: $!"; my @files = readdir DIR; closedir DIR; or sometimes I use glob , but anyway, I always need to add a line or two to filter out . and .. which is quite annoying. How do you usually go about this common task? 回答1: I will normally use the glob method: for my $file (glob "$dir/*") { #do stuff with $file } This works fine unless the directory has lots of files in it. In those cases you have to switch back to readdir in a while loop (putting

Android_函数opendir 、readdir叙述

匿名 (未验证) 提交于 2019-12-03 00:19:01
一、结构体DIR struct __dirstream { void * __fd ; char * __data ; int __entry_data ; char * __ptr ; int __entry_ptr ; size_t __allocation ; size_t __size ; __libc_lock_define (, __lock) }; /* This is the data type of directory stream objects. The actual structure is opaque to users. */ //这是一个目录流对象的数据类型,实际上该结构体对用户是不可见的   typedef struct __dirstream DIR ; 1、DIR结构体类似于FILE,是一个内部结构; 2、以下几个函数用这个内部结构保存当前正在被读取的目录的有关信息(摘自《UNIX环境高级编程(第二版)》)。 extern DIR *opendir ( const char *__name); extern int closedir (DIR *__dirp); extern struct dirent *readdir (DIR *__dirp); 3、函数 DIR *opendir(const char *pathname),即打开文件目录

fopen() in C returns NULL even when file exists (is returned by readdir())

痞子三分冷 提交于 2019-12-02 10:20:21
问题 I am trying to open a directory, and then open a file named "YouTubeSign", directory opens fine, but for some reason fopen() on the file fails even if the file exists; any ideas? int main(int argc, char **argv) { DIR *pdir = NULL; struct dirent *in_file; FILE *signature = NULL; pdir = opendir(argv[1]); if (pdir == NULL) { printf("could not open directory %s", argv[1]); return -1; } while (in_file = readdir(pdir)) { if (strcmp(in_file->d_name, "YouTubeSign") == 0) { signature = fopen(in_file-

Start new column when readdir moves to new dir

余生颓废 提交于 2019-12-02 09:47:44
Original Issue: I am using readdir to snag file names from multiple directories. I was wondering if there is a way to arrange the data so that each time it starts a new directory it makes a new column i.e. ---| user1 | user2 | user3 | ---| file1a | file2a | file3a | ---| file1b | file2b | file3b | ---| file1c | file2c | file3c | ---| file1d | ^ | file3d | ---| file1e | | | ^ | ---| ^ | end | | | ---| | | dir2 | end | ---| end | #start | dir3 | ---| dir1 | col3 | etc. | ---| #start | | | ---| col2 | | | if this is possible that would be great; as far as displaying the user names across the top

fopen() in C returns NULL even when file exists (is returned by readdir())

﹥>﹥吖頭↗ 提交于 2019-12-02 03:23:48
I am trying to open a directory, and then open a file named "YouTubeSign", directory opens fine, but for some reason fopen() on the file fails even if the file exists; any ideas? int main(int argc, char **argv) { DIR *pdir = NULL; struct dirent *in_file; FILE *signature = NULL; pdir = opendir(argv[1]); if (pdir == NULL) { printf("could not open directory %s", argv[1]); return -1; } while (in_file = readdir(pdir)) { if (strcmp(in_file->d_name, "YouTubeSign") == 0) { signature = fopen(in_file->d_name, "r"); printf("opening youtubesign"); break; } } if (signature == NULL){ printf("could not open