codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

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


By : , Category : 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.

ReLated :

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
*
!/.gitignore
!*.foo

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
Resources/*
!Resources/*/
!Resources/*.foo
Resources/*/*
!Resources/*/*/
!Resources/*/*.foo
Resources/*/*/*
!Resources/*/*/*/
!Resources/*/*/*.foo
Resources/*/*/*/*
!Resources/*/*/*/*/
!Resources/*/*/*/*.foo

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 "FILESPATH=C:"
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%"
            )
        )
    )
)
pause
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'

Here,

. - 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.

Update:

If you want to list out the directories,

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

Change in application.js

//= require_tree

to

//= require_directory .

or

//= require_tree ./useful

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

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .