M
M
Mithgol2011-11-25 08:52:21
Windows
Mithgol, 2011-11-25 08:52:21

How to accept the output of a windows command called from node.js?

Let's create an index.js file like this:

require('child_process').exec('dir', function(err, outstr){
   console.log(outstr);
});

After that, the output of the “ node ./ ” command, launched from Russian Windows, will be as follows:
??? ? ?????? U ????? ????? USBSTICK
 ?????? ????? ??: 467B-0CBE

 ???????? ????? U:\Fido\Soft\_Sources\node

24.11.2011  15:12    <DIR>          .
24.11.2011  15:12    <DIR>          ..
25.11.2011  09:36               522 index.js
               1 ????            522 ????
               2 ?????  17?412?767?744 ???? ??????

We will get approximately the same thing in the file if we write not to the console, but to the test file:
var fs = require('fs'); // file system

require('child_process').exec('dir', function(err, outstr){
   fs.createWriteStream('testfile.txt', {
      flags: 'w',
      encoding: 'binary'
   }).write(outstr);
});

The question is: how to be and what to do to get text in arbitrary encoding from the process? Obviously, Node.js doesn't expect the dir command to spew text into the CP866, and therefore fails unchildishly. We need to force Node.js to accept Russian text:
U:\Fido\Soft\_Sources\node>dir
 Том в устройстве U имеет метку USBSTICK
 Серийный номер тома: 467B-0CBE

 Содержимое папки U:\Fido\Soft\_Sources\node

24.11.2011  15:12    <DIR>          .
24.11.2011  15:12    <DIR>          ..
25.11.2011  09:37               522 index.js
               1 файлов            522 байт
               2 папок  17 412 767 744 байт свободно

How to achieve this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sdevalex, 2011-11-25
@Mithgol

There is a chcp command that changes the terminal encoding in Windows. In general, of course, it's better not to bathe, but to use good things in good server OS.

M
mark_ablov, 2011-11-25
@mark_ablov

node-iconv doesn't help?

F
Fedor Indutny, 2011-11-25
@donnerjack13589

github.com/joyent/node/issues/2190

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question