如何删除另一个目录中不存在的文件? [英] How to delete files that are not present in another directory?

查看:31
本文介绍了如何删除另一个目录中不存在的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个目录,我们称它们为srcbuild.我的构建系统可以工作,因此对于 src 中比 build 中 mtime 更近的所有文件,它将文件从 src 复制到 buid 并进行一些转换(缩小、版本控制等).否则跳过,因为文件被认为是最新的.

I have two directories, let's call them src and build. My build system works so that for all files with mtime more recent in src than in build it copies the file from src to buid and does some transformations (minification, versioning, etc.). Otherwise skips as the file is considered up to date.

然而,当源文件被删除时,这会带来一个问题,因为它的构建版本仍然存在于 build 中,并进入之后生成的映射文件.

This however poses a problem when source file is deleted, as its built version is still present in build and gets into map file generated afterwards.

$ ls src
example1.js
example2.js

$ ant do-the-stuff
...

$ ls build
example1.js
example1-12345.min.js
example2.js
example2-23456.min.js
.map

$ cat .map
example1.js=example1-12345.min.js
example2.js=example2-23456.min.js

$ rm src/example2.js
$ ant do-the-stuff
...

$ cat .map
example1.js=example1-12345.min.js
example2.js=example2-23456.min.js

有没有办法用 Ant 删除另一个目录中不存在的文件?从集合论的角度来看,这是一个简单的 A\B 操作.

Is there a way to delete files not present in another directory with Ant? From set theory point of view it's a simple A\B operation.

这是我已经尝试过但没有奏效的方法:

This is what I have tried already but didn't work:

<delete dir="build">
    <exclude name="src/*" />
</delete>

<delete dir="build">
    <exclude>
        <fileset name="src" />
    </exclude>
</delete>

<delete dir="build">
    <fileset dir="build/*">
        <not>
            <present targetdir="src" />
        </not>
    </fileset>
</delete>

推荐答案

有没有办法用 Ant 删除另一个目录中不存在的文件"
是的,使用 删除任务fileset 使用 presentselector, fe

"Is there a way to delete files not present in another directory with Ant"
yes, use delete task with a fileset using a presentselector, f.e.

<fileset dir="/home/rosebud/temp/dir1" includes="*.jar" id="srcfileset">
 <present present="srconly" targetdir="/home/rosebud/temp/dir2"/>
</fileset>
<echo>Files only in /home/rosebud/temp/dir1 => ${toString:srcfileset}</echo>
<delete>
 <fileset refid="srcfileset"/>
</delete>

将删除仅存在于/home/rosebud/temp/dir1 中的所有文件
反过来使用:

would delete all files only present in /home/rosebud/temp/dir1
for the other way around use :

...
 <not>
  <present present="srconly" targetdir="/home/rosebud/temp/dir2"/>
 </not>
...

另见 https://stackoverflow.com/a/12847012/130683 使用当前选择器的另一个示例

see also https://stackoverflow.com/a/12847012/130683 for another example using the present selector

这篇关于如何删除另一个目录中不存在的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆