4/6/2023 0 Comments Find any file alternatives![]() ![]() I'm not sure why you think this is better than File::Find. readdir(DIR)", etc.)īe sure to read my standard disclaimer if this is a reply. (update: made minor adjustments to comments in the code, added "closedir" also wanted to point out that the loop over files could be moderated by using "grep. Here, Perl can just skip over any subdirectories that it sees, because the output from "find" will bring those up for treatment in due course. Meanwhile, perl is very good with reading directory contents and manipulating data files, and it's really easy to do this when you're just working with data files in one directory at a time. The nice thing about this approach is that the "find" utility is very good with the recursive descent into subdirectories, and that's all it needs to do. # anything else we need to do regarding this directory # do what needs to be done with data files Next if ( -d "$dir/$file" ) # outer while loop will handle al l dirs Warn "$toppath/$dir: opendir failed: $!\n" Local $/ = "\x0" # added thanks to etcshadow's reply # string terminator for each file name (don't rely on "\n", # the "-print0" (thanks, etcshadow) sets a null byte as the # will only list the paths of directories contained here $toppath), and because of the "-type d" option, # find will traverse downward from current directory type d -print0 |" ) or die "can't run find: $!" # assume you have a $toppath, which is where the traversal startsĬhdir $toppath or die "can't cd to $toppath: $!" It involves using the standard unix "find" utility (which has been ported for ms-windows users, of course). To traverse a directory tree and do stuff with some or all of the data files therein, this method works very fast, takes up very little memory, and is a relatively easy framework for handling lots of jobs of this ilk. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |