How can I recursively include all files with a specific name in a folder using PHP?

Although it sounds like a bad design to include all files, it is possible:

$directory = new RecursiveDirectoryIterator('path/to/project/');
$recIterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($recIterator, '//module.php$/i');

foreach($regex as $item) {
    include $item->getPathname();

Btw, this example is derived from a comment in the PHP manual. To make it working, make sure that all sub folders are readable by PHP in that folder. If cannot make this sure you will have to write a custom recursive function for that (but this is unlikely).

Again, what you are doing is not a good design and will lead to problems (earlier than you might think). If you are following the OOP style, a better approach would be to use the autload mechanism of PHP.

The best answer is to add a Resources/.gitignore file under Resources containing:

# Ignore any file in this directory except for this file and *.foo files

If you are unwilling or unable to add that .gitignore file, there is an inelegant solution:

# Ignore any file but *.foo under Resources. Update this if we add deeper directories

You will need to edit that pattern if you add directories deeper than specified.

If I got it right, the following code should do what you want:

@echo off

set "LISTFOLDER=C:vbk	xt	est"
set "DESTPATH=C:vbkdest	est"

for %%F in ("%LISTFOLDER%*.txt") do (
    for /F "usebackq delims=" %%L in ("%%~F") do (
        for /R "%FILESPATH%" %%I in ("%%~L*") do (
            if /I "%%~nxL"=="%%~nxI" (
                ECHO xcopy /Q /V "%%~fI" "%DESTPATH%"
exit /B

This is what I did:

  • a subroutine (:COPY_FILES) is not needed here, so I omitted it;
  • for /F does not accept wildcards (like * or ?), so I put over a standard for;
  • the for /F loop is intended to read the found text file(s) line by line (at least upon my interpretation of your code), so there is no need for dir /B;
  • now comes the tricky part: for /R walks through a directory tree recursively, searching for matching files given in its set (that is the part after in within ()); but if a dedicated file is provided there without any wildcards (like * or ?), it does not actually search the file system for the file but merely returns all directories in the tree with the file appended; to force for /R to really search the file you need to use a wildcard; so that's why I appended a *; but this of course might return unwanted files, namely such that contains the desired name at the beginning; so I placed an if statement inside of the loop to filter out the respective items (the ~nx modifier of the compared for variables expand to the pure file names plus extensions only);
  • since for /R already does the job of walking through a directory tree recursively, you do not need to specify the switch /S for xcopy; (remove the upper-case ECHO to actually copy files!)
  • instead of exit, you should use exit /B within batch files, because this quits the batch file only while exit terminates the containing command prompt instance (cmd) too; (here exit /B is superfluous actually, but I put it here just to be mentioned...)
  • the set syntax has been improved so that there are quotation marks around the entire assignment expression; this avoids trouble with some special characters like ^ and &, for example; note that the "" do not become part of the variable value here;
  • some quotation-mark-related issues have been fixed throughout the script, all paths are enclosed within "", and the ~ modifier is used for several for variables to remove potential surrounding "";

I think you need find command. You can use like this,

find . -name '*.txt'


. - dot(.) is current directory. You can also specify the path where to start.

-name - file name to find.

You can also use -maxdepth option to limit the depth of recursive finding. Normally, find will find the file recursively.


If you want to list out the directories,

find . -name '*.txt' -exec dirname {} ;

Change in application.js

//= require_tree


//= require_directory .


//= require_tree ./useful

So that, app/assets/javascripts/* files will be included and app/assets/javascripts/mobile/* wont be included.


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © All Rights Reserved .