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?
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.
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.
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 .
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With