Suppose i have two files a_b_c_d.txt and e_f_g_h.png S,At runtime i.e., by using command prompt i have to create b folder inside that c folder inside that d folder inside that a.txt and same also for another file f->g->h->e.png and i have some text in a and image in epng . .So,how can I get values from those existing file into created files. .
You can find all the file system operations inside the fs module. http://nodejs.org/api/fs.html
But like tapan says if you need to do complex synchronous execution that manipulates the file system something like Bash will be a lot better suited for that.
So if I'm understanding you correctly you want to take a file named "a_b_c_d.txt" in some folder, and move that into a nested folder as:
./a_b_c_d.txt -> ./b/c/d/a.txt
The general solution would be:
process.argv
if it varies.
For example, if you supply the file as an argument to node, e.g.
node move.js "a_b_c_d.txt"
, the argument, "a_b_c_d.txt", will be in the argv array.__dirname
global variable
if you need it.split(...)
method.
For the above argument, split('.')
will result in the array ['a_b_c_d', 'txt']
'a_b_c_d'
using '_'
,
and use various array operations to pull the file name 'a'
out of the array, so that you're left with the path ['b', 'c', 'd']
and the file name and extension sitting in their own variables somewhere.fs.mkdirSync(...)
on the path array to make each nested folder,
starting with b
(e.g. using array's forEach(...)
method).
You could also use the async fs.mkdir(...)
and supply callbacks,
but the sync version is easier in this case.fs.renameSync(...)
to move ./a_b_c_d.txt
to ./b/c/d/a.txt
. As you can see, python or bash (as tapan suggested) would probably be simpler for this use case, but if for some reason you have to use node, the above advice will hopefully be enough to get you started.