diff --git a/includes/file.inc b/includes/file.inc
index 9b48cc136d93f0e6b9da146cd90ef33a6d17af2e..312d5f198c43deb4f4a04bdb0f1a71a16d426098 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -488,13 +488,19 @@ function file_download() {
  *   values are "filename", for the path starting with $dir,
  *   "basename", for the basename of the file, and "name" for the name
  *   of the file without an extension.
+ * @param $min_depth
+ *   Minimum depth of directories to return files from.
+ * @param $max_depth
+ *   Maximum recursion depth.
+ * @param $depth
+ *   Current depth of recursion. This parameter is only used interally and should not be passed.
  *
  * @return
  *   An associative array (keyed on the provided key) of objects with
  *   "path", "basename", and "name" members corresponding to the
  *   matching files.
  */
-function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename') {
+function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $max_depth = 9, $depth = 0) {
   $key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
   $files = array();
 
@@ -502,9 +508,11 @@ function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $ca
     while ($file = readdir($handle)) {
       if (!in_array($file, $nomask)) {
         if (is_dir("$dir/$file") && $recurse) {
-          $files = array_merge($files, file_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse, $key));
+          if ($depth < $max_depth) {
+            $files = array_merge($files, file_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse, $key, $min_depth, $max_depth, $depth + 1));
+          }
         }
-        elseif (ereg($mask, $file)) {
+        elseif ($depth >= $min_depth && ereg($mask, $file)) {
           $filename = "$dir/$file";
           $basename = basename($file);
           $name = substr($basename, 0, strrpos($basename, '.'));
diff --git a/modules/system.module b/modules/system.module
index 5f38a895cda214f69b5d983632a08cea8beb430f..3583100fe293b7b673ac63e2f8041eb617f590bf 100644
--- a/modules/system.module
+++ b/modules/system.module
@@ -395,11 +395,13 @@ function system_theme_data() {
  *   sites/somesite/modules/.
  * @param $key
  *   The key to be passed to file_scan_directory().
+ * @param $min_depth
+ *   Minimum depth of directories to return files from.
  *
  * @return
  *   An array of file objects of the specified type.
  */
-function system_listing($mask, $directory, $key = 'name') {
+function system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
   $config = conf_init();
   $searchdir = array($directory);
   $files = array();
@@ -410,7 +412,7 @@ function system_listing($mask, $directory, $key = 'name') {
 
   // Get current list of items
   foreach ($searchdir as $dir) {
-    $files = array_merge($files, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, $key));
+    $files = array_merge($files, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, $key, $min_depth));
   }
 
   return $files;
@@ -456,7 +458,7 @@ function system_theme_listing() {
  */
 function system_module_listing() {
   // Get current list of modules
-  $files = system_listing('\.module$', 'modules');
+  $files = system_listing('\.module$', 'modules', 'name', 0);
 
   // Extract current files from database.
   system_get_files_database($files, 'module');
diff --git a/modules/system/system.module b/modules/system/system.module
index 5f38a895cda214f69b5d983632a08cea8beb430f..3583100fe293b7b673ac63e2f8041eb617f590bf 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -395,11 +395,13 @@ function system_theme_data() {
  *   sites/somesite/modules/.
  * @param $key
  *   The key to be passed to file_scan_directory().
+ * @param $min_depth
+ *   Minimum depth of directories to return files from.
  *
  * @return
  *   An array of file objects of the specified type.
  */
-function system_listing($mask, $directory, $key = 'name') {
+function system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
   $config = conf_init();
   $searchdir = array($directory);
   $files = array();
@@ -410,7 +412,7 @@ function system_listing($mask, $directory, $key = 'name') {
 
   // Get current list of items
   foreach ($searchdir as $dir) {
-    $files = array_merge($files, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, $key));
+    $files = array_merge($files, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, $key, $min_depth));
   }
 
   return $files;
@@ -456,7 +458,7 @@ function system_theme_listing() {
  */
 function system_module_listing() {
   // Get current list of modules
-  $files = system_listing('\.module$', 'modules');
+  $files = system_listing('\.module$', 'modules', 'name', 0);
 
   // Extract current files from database.
   system_get_files_database($files, 'module');