# Moving batches of files with Rake

**URL:** <https://www.ruby-forum.com/t/moving-batches-of-files-with-rake/90662>\
**Category:** Ruby\
**Created:** [March 30, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/moving-batches-of-files-with-rake/90662 "2007-03-30T20:21:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [March 30, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/moving-batches-of-files-with-rake/90662/1 "2007-03-30T20:21:36Z")

</div>

I’m comparing Rake and NAnt for our pseudo-build process needs. I’ve  
written the NAnt build file, and now I’m trying to port it (directly  
at first) to Rake.

NAnt has a ‘move’ task for moving batches of files to a new directory.  
By default, if you list specific files that don’t exist, they are  
ignored (not errored). Here’s an example of the NAnt section I’m  
trying to recreate:

```
<move todir="${path.intermediate}">
  <fileset>
    <include name="${dae}" />
    <include name="${presentation_name}_org.bgf" />
  </fileset>
</move>
<move todir="${path.data}">
  <fileset basedir="${path.dae}">
    <include name="${presentation_name}.bgf" />
    <include name="${presentation_name}.nif" />
    <include name="*.bvs" />
    <include name="*.lua" />
  </fileset>
</move>

```

Is there a built-in method for doing something like this (using  
FileSet perhaps) that I’m not seeing in Rake? If not, does someone  
else have a pre-built method that does this?

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [March 31, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/moving-batches-of-files-with-rake/90662/2 "2007-03-31T14:04:26Z")

</div>

Gavin K. wrote:

> I’m comparing Rake and NAnt for our pseudo-build process needs. I’ve  
> written the NAnt build file, and now I’m trying to port it (directly  
> at first) to Rake.
> 
> NAnt has a ‘move’ task for moving batches of files to a new directory.  
> By default, if you list specific files that don’t exist, they are  
> ignored (not errored). Here’s an example of the NAnt section I’m  
> trying to recreate:
> 
> ```
> <move todir="${path.intermediate}">
> <fileset>
> <include name="${dae}" />
> <include name="${presentation_name}_org.bgf" />
> </fileset>
> </move>
> <move todir="${path.data}">
> <fileset basedir="${path.dae}">
> <include name="${presentation_name}.bgf" />
> <include name="${presentation_name}.nif" />
> <include name="*.bvs" />
> <include name="*.lua" />
> </fileset>
> </move>
> 
> ```
> 
> Is there a built-in method for doing something like this (using  
> FileSet perhaps) that I’m not seeing in Rake? If not, does someone  
> else have a pre-built method that does this?

Use a file list to build up your list of files. Eg.

DAE\_FILES = FileList[DAE, “#{PRESENTATION\_NAME}\_org.pgf”]

Then just use a mv command. Unfortunately, using mv with a file list  
seems to require an explicit ‘to\_a’ call. That shouldn’t be the case  
(I’ll see if I can fix that in an update).

Example:

task :move\_files do  
mv DAE\_FILES.to\_a, PATH\_INTERMEDIATE  
end

If you don’t want errors on non-existent files, you can filter them out:

task :move\_files do  
mv DAE\_FILES.select { |fn| File.exist?(fn) }.to\_a,  
PATH\_INTERMEDIATE  
end

Wordy, but it works.

– Jim W.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [April 1, 2007, 3:19am UTC](https://www.ruby-forum.com/t/moving-batches-of-files-with-rake/90662/3 "2007-04-01T03:19:52Z")

</div>

Jim W. wrote:  
[…]

> (I’ll see if I can fix that in an update).\

I just created a beta version of Rake that supports this. You can now  
say:

task :move\_files do  
mv DAE\_FILES.existing, PATH\_INTERMEDIATE  
end

FileLists now support an ‘existing’ method which returns a filelist  
containing on ly existing file names. (There is also an existing!  
method that does the same thing to the existing file list). Also,  
FileLists can now be passed directly to FileUtils methods just like  
arrays.

If you want to try the beta version, do:

gem install rake --source [http://onestepback.org/betagems](http://onestepback.org/betagems)

– Jim W.
