Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select files with certain extension

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
is it possible to select all the files in a directory that have say "_test" in their name in one swoop?
if this is similar to the linux command example: find -name "*_the.jpg" then that would work perfectly
 
didnt know about glob but this is a part of the script i made a while ago for a text newscript i first wrote... it seperates the file extension from the file and then makes sure the file is a # because my news posts were # format.. but you could modify very easily..

Code:
//get news files from dir
	$currentdir = $news_directory;              //current dirdirectory
	$dir = opendir("$currentdir");				//opens the directory
	while ($file = readdir($dir))				//loops through gettign all files
	{
		$fcheck = explode(".",$file);			//splits file name from extension
		$fname= $fcheck[0];						//gets file name
		$fext = $fcheck[1];						//gets extension
		if (is_numeric($fname))					//checks if its a news post
		{
			$flist = $flist.":$fname.$fext" ;	//puts all file names into a string
		}
	}
	$news = explode(":",$flist);				//splits file name string up into an array
	rsort($news);								//sorts the news posts newest to oldest

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
unborn:

You forgot to mention that the snippet of code you posted will not work with a filename with more than one decimal. It will fail on a filename of 'this.is.a.test'.

To fix that you'd need to replace:

Code:
$fcheck = explode(".",$file);            //splits file name from extension
$fname= $fcheck[0];                        //gets file name
$fext = $fcheck[1];                        //gets extension

with something like

Code:
// Should work with something like
// 'this.is.a.test'
$fnameSeperator = strrpos($filename, '.');
$fname = substr($filename, 0, $fnameSeperator);
$fext = substr($filename, $fnameSeperator + 1);

I haven't tested that though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top