Synchronous File Copy in Node.js

Sometimes, asynchronous operations can be a burden. Especially when you're writing small console utilities like to batch process files.

There are many asynchronous ways to copy a file. Here is a synchronous version (CoffeeScript):

copyFileSync = (srcFile, destFile) ->
  BUF_LENGTH = 64*1024
  buff = new Buffer(BUF_LENGTH)
  fdr = fs.openSync(srcFile, 'r')
  fdw = fs.openSync(destFile, 'w')
  bytesRead = 1
  pos = 0
  while bytesRead > 0
    bytesRead = fs.readSync(fdr, buff, 0, BUF_LENGTH, pos)
    fs.writeSync(fdw,buff,0,bytesRead)
    pos += bytesRead
  fs.closeSync(fdr)
  fs.closeSync(fdw)

You can view the converted version in JavaScript%20-%3E%0A%20%20BUF_LENGTH%20%3D%2064*1024%0A%20%20buff%20%3D%20new%20Buffer(BUF_LENGTH)%0A%20%20fdr%20%3D%20fs.openSync(srcFile%2C%20'r')%0A%20%20fdw%20%3D%20fs.openSync(destFile%2C%20'w')%0A%20%20bytesRead%20%3D%201%0A%20%20pos%20%3D%200%0A%20%20while%20bytesRead%20%3E%200%0A%20%20%20%20bytesRead%20%3D%20fs.readSync(fdr%2C%20buff%2C%200%2C%20BUF_LENGTH%2C%20pos)%0A%20%20%20%20fs.writeSync(fdw%2Cbuff%2C0%2CbytesRead)%0A%20%20%20%20pos%20%2B%3D%20bytesRead%0A%20%20fs.closeSync(fdr)%0A%20%20fs.closeSync(fdw)).

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

comments powered by Disqus
Proudly built with Sky.