Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Copying, moving, and deleting files and directories


Working with files and directories is a very common task. In this recipe, we will see how we can do this using CoffeeScript and Node.

Getting ready

In this recipe, we have a file named chinook.sqlite and a directory named src. Both are part of the source code from Chapter 9, Testing Our Applications, but you can use any file and directory renamed to match this setup.

How to do it...

Node provides the filesystem module to work with files and directories. It allows us to create, open, close, read, write, and rename files and directories. To see how we can perform these tasks, follow these steps:

  1. Using Node's filesystem module, we can copy a file by creating read and write streams and piping file contents from one to the other:

    fs = require 'fs'
    
    src = 'chinook.sqlite'
    dest = 'chinook2.sqlite'
    
    # copy from a read stream into a write stream
    fs.createReadStream(src).pipe  fs.createWriteStream(dest)
    
  2. We can also use the filesystem's rename() function...