Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does glob() have negation?

Tags:

php

glob

I know I can do this...

glob('/dir/somewhere/*.zip');

...to get all files ending in .zip, but is there a way to return all files that are not ZIPs?

Or should I just iterate through and filter off ones with that extension?

like image 462
alex Avatar asked Dec 09 '09 23:12

alex


People also ask

How does glob work?

glob (short for global) is used to return all file paths that match a specific pattern. We can use glob to search for a specific file pattern, or perhaps more usefully, search for files where the filename matches a certain pattern by using wildcard characters.

What is the difference between glob and Re in Python?

The main difference is that the regex pattern matches strings in code, while globbing matches file names or file content in the terminal. Globbing is the shell's way of providing regular expression patterns like other programming languages.

What is the glob command?

In computer programming, glob (/ɡlɑːb/) patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txt textfiles/ moves ( mv ) all files with names ending in .txt from the current directory to the directory textfiles .

What does glob glob return in Python?

Python glob. glob() method returns a list of files or folders that matches the path specified in the pathname argument. This function takes two arguments, namely pathname, and recursive flag. pathname : Absolute (with full path and the file name) or relative (with UNIX shell-style wildcards).


2 Answers

You could always try something like this:

$all = glob('/dir/somewhere/*.*');
$zip = glob('/dir/somewhere/*.zip');
$remaining = array_diff($all, $zip);

Although, using one of the other methods Pascal mentioned might be more efficient.

like image 62
Atli Avatar answered Oct 20 '22 19:10

Atli


A quick way would be to glob() for everything and use preg_grep() to filter out the files that you do not want.

preg_grep('#\.zip$#', glob('/dir/somewhere/*'), PREG_GREP_INVERT)

Also see Glob Patterns for File Matching in PHP

like image 41
salathe Avatar answered Oct 20 '22 21:10

salathe