opendir DIR, "MainDir" or die ""; while( $file = readdir(DIR) ) { #do whatever you want to the file } closedir DIR; #### while( $file = <*.cpp> ) { #do something to the file }