Exclude hidden files from scandir
On Unix, you can use preg_grep
to filter out filenames that start with a dot:
$files = preg_grep('/^([^.])/', scandir($imagepath));
Does php scandir() exclude hidden files under Windows?
A short test shows that neither scandir()
nor glob()
or others take care of the hidden flag.
Here is the experiment and the result:
Parts:
- Windows 7
- PHP 5.6.9 (x86)
- Visual Studio 2012 Redistributable x86
So scandir()
does not hide files having the hidden flag set.
Next question is, can more powerful PHP commands like glob()
be configured.
Firstly, there is no parameter to deal with flags:
http://php.net/manual/de/function.glob.php
Secondly, there is this telling comment of Gabriel S. Luraschi:
http://php.net/manual/de/function.glob.php#110510
He recommends exec('dir ... \A ...')
. But on commercial hostings (if they run on Windows), this will not be allowed.
To be sure: use the Linux style and ignore files that start with a dot, like here:
Exclude hidden files from scandir
How do I exclude hidden folders and files from readdir?
If you just want to exclude files starting with a dot, ".", you can do something like this:
$files = readdir('/path/to/folder');
$files = array_filter($files, create_function('$a','return ($a[0]!=".");'));
This will only return files that don't start with dot "."
On windows, hidden files work differently, I don't know how to find those out.
Ignore hidden files with php
I think you want to ignore any file that begins with a dot (.) and not just the filename.
<?php
if ($handle = opendir('assets/automotive')) {
$ignore = array( 'cgi-bin', '.', '..','._' );
while (false !== ($file = readdir($handle))) {
if (!in_array($file,$ignore) and substr($file, 0, 1) != '.') {
echo "$file\n";
}
}
closedir($handle);
}
?>
Ignore hidden files while recursively scanning directories
You can just replace if not recentry.path.startswith('.'):
with if not recentry.name.startswith('.'):
, so that it will ignore your .DS_Store
file.
How to ignore hidden files when using os.stat() results in Python?
Your desired outcome is impossible. The most recent modification time of all non-hidden files doesn't necessarily correspond to the virtual "last modified time of a directory ignoring hidden files". The problem is that directories are modified when files are moved in and out of them, but the file timestamps aren't changed (the file was moved, but not modified). So your proposed solution is at best a heuristic; you can hope it's correct, but there is no way to be sure.
In any event, no, there is no built-in that provides this heuristic. The concept of hidden vs. non-hidden files is OS and file system dependent, and Python provides no built-in API that cares about the distinction. If you want to make a "last_modified_guess" function, you'll have to write it yourself (I recommend basing it on os.scandir
for efficiency).
Something as simple as:
last_time = max(entry.stat().st_mtime for entry in os.scandir(somedir) if not entry.name.startswith('.'))
would get you the most recent last modified time (in seconds since the epoch) of your non-hidden directory entries.
Update: On further reflection, the glob
module does include a concept of .
prefix meaning "hidden", so you could use glob.glob
/glob.iglob
of os.path.join(somedir, '*')
to have it filter out the "hidden" files for you. That said, by doing so, you give up some of the potential benefits of os.scandir
(free or cached stat
results, free type checks, etc.), so if all you need is "hidden" filtering, a simple .startswith('.')
check is not worth giving that up.
Include JUST files in scandir array?
You can use array_filter.
$indir = array_filter(scandir('../pages'), function($item) {
return !is_dir('../pages/' . $item);
});
Note this filters out all directories and leaves only files and symlinks. If you really want to only exclude only files (and directories) starting with .
, then you could do something like:
$indir = array_filter(scandir('../pages'), function($item) {
return $item[0] !== '.';
});
How to ignore hidden files with opendir and readdir in C library
This is normal. If you do ls -a
(which shows all files, ls -A
will show all files except for .
and ..
), you will see the same output.
.
is a link referring to the directory it is in: foo/bar/.
is the same thing is foo/bar
.
..
is a link referring to the parent directory of the directory it is in: foo/bar/..
is the same thing as foo
.
Any other files beginning with .
are hidden files (by convention, it is not really enforced by anything; this is different from Windows, where there is a real, official hidden attribute). Files ending with ~
are probably backup files created by your text editor (again, this is convention, these really could be anything).
If you don't want to show these types of files, you have to explicitly check for them and ignore them.
How to ignore hidden files using os.listdir()?
You can write one yourself:
import os
def listdir_nohidden(path):
for f in os.listdir(path):
if not f.startswith('.'):
yield f
Or you can use a glob:
import glob
import os
def listdir_nohidden(path):
return glob.glob(os.path.join(path, '*'))
Either of these will ignore all filenames beginning with '.'
.
Related Topics
How to Match Accented Characters with PHP Preg
Using If(!Empty) with Multiple Variables Not in an Array
How to Implement a Decorator in PHP
How to Log All API Calls Using Guzzle 6
Woocommerce - Send Custom Email on Custom Order Status Change
"Class Xxx Is Not a Valid Entity or Mapped Super Class" After Moving the Class in the Filesystem
Which Method Is Preferred Strstr or Strpos
Rationale Behind Simplexmlelement's Handling of Text Values in Addchild and Addattribute
Base Table or View Not Found: 1146 Table Laravel 5
How to Determine If Pdo Is Enabled in PHP
Should Lock_Ex on Both Read & Write Be Atomic
How to Extend a Class Dynamically
How to Send Soap Xml via Curl and PHP