How do I collect filenames from a folder into a list?
I am looking开发者_开发知识库 for a way to collect filenames into a list with Perl. For example, I go into a folder with a hundred different filenames ranging from text files to MP3, and I would like to put each and every filename in a list. How would I do that? I was looking everywhere and cannot seem to figure it out. I have gotten as far as using the chdir
function but I can't seem to read the filenames and print them. Can anyone help?
Look for the function glob:
my @allfiles = glob '*.*';
my @musics = glob '*.mp3';
One way is to open the current dir, and get all its contents, something like:
opendir(DIR, "yourDIR");
my @files = readdir(DIR);
obviously you can use grep
like
my @files = grep {...} readdir(DIR);
to get specific types of files, etc.
精彩评论