             (cb) =>
                 if opts.filename?
                     dbg("open output file #{opts.filename}")
-                    fs.open opts.filename, 'w', (err, _fd) =>
-                        fd = _fd
-                        cb(err)
+                    misc_node.ensure_containing_directory_exists process.cwd()+'/'+opts.filename, (err) =>
+                        if err
+                            cb(err)
+                        else
+                            fs.open opts.filename, 'w', (err, _fd) =>
+                                fd = _fd
+                                cb(err)
                 else
                     cb()
             (cb) =>

                            dbg("chunk ids=#{misc.to_json(chunk_ids)}")
                            cb()
            (cb) =>
                dbg("get chunks")
                f = (i, c) =>
                    t = misc.walltime()

                        else
                            if result.length > 0
                                chunk_ids = result[0][0]
                                if not chunk_ids?
                                    chunk_ids = []
                                dbg("chunk ids=#{misc.to_json(chunk_ids)}")
                            else
                                dbg("nothing there")
