开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜