Find all directories in which a file exists, such that the file contains a search string

独自空忆成欢 提交于 2019-12-13 01:23:57

问题


I have a directory tree that I need to process as follows:

  • I have a certain file that needs to be copied to a select few sub directories
  • A sub directory of interest is one that contains a file within which I can regex match a known search string

Ideally I would like to:

  • Perform a regex match across all files within a directory
  • If the regex matches, copy the file to that directory

The trouble is that I am quite new to ANT and I'm having difficulties finding my way around. I can't find any tasks in the docs about per directory operations based on regex search. The closest thing I've found is a regex replace task (<replaceregexp>) that can search and replace patterns across files.

Is this even possible? I'd really appreciate a sample to get started with. I apologize for requesting code - I simply don't know how to begin composing the tasks together to achieve this.

Alternatively I have the option of hardcoding all the copy operations per directory, but it would mean manually keeping everything in sync as my project grows. Ideally I'd like to automate it based on the regex search/copy approach I described.

Thanks!


回答1:


Your requirement is a bit non-standard, so I've solved it using a custom Groovy task.

Here's a working example:

<project name="find-files" default="copy-files">

    <!--
    ======================
    Groovy task dependency
    ======================
    -->
    <path id="build.path">
        <pathelement location="jars/groovy-all-1.8.6.jar"/>
    </path>
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>


    <!--
    =========================
    Search for matching files
    =========================
    -->
    <target name="search-files">
        <fileset id="filesContainingSearchString" dir="src">
            <include name="**/*.txt"/>
            <containsregexp expression="[4-6]\.[0-9]"/>
        </fileset>
    </target>

    <!--
    ===================================
    Copy file into each directory found
    ===================================
    -->
    <target name="copy-files" depends="search-files">
        <groovy>
        project.references.filesContainingSearchString.each { file ->
            def dir = new File(file.toString()).parent

            ant.copy(file:"fileToBeCopied.txt", toDir:dir)
        }
        </groovy>
    </target>

</project>

Notes:

  • Groovy jar can be downloaded from Maven Central



回答2:


Use the copy task with a fileset and regular expression selector :

 <copy todir="your/target/dir">
  <fileset dir="rootdir/of/your/directorytree" includes="**/*.txt">
   <containsregexp expression="[4-6]\.[0-9]"/>
  </fileset>
 </copy>

This example is taken from the ant manual and slightly adapted.
Means select all files with .txt extension anywhere beyond rootdir/of/your/directorytree that match the regular expression (have a 4,5 or 6 followed by a period and a number from 0 to 9) and copy them to your/target/dir.
Just adapt it for your needs.



来源:https://stackoverflow.com/questions/9255127/find-all-directories-in-which-a-file-exists-such-that-the-file-contains-a-searc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!