I am running a virussoftware scan via command line and the output will list me some informations in german that has umlaute (ö, ä, ü). The command is something like "C:\Program Files\Antivirus\antivirus.exe" --console --scan C:\path\to\file
Running the command via cmd directly will display them correctly. When running it via exec those will be displayed as �.
I found the question wrong encoding when using child_process spawn or exec under Windows where the answer was to use cmd /c chcp 65001>nul && together with my command but that did nothing for me.
In another question How to force UTF-8 in node js with exec process? the answer was something similar but with @chcp 65001 >nul & cmd /d/s/c . But using this will error out the message that "C:\Program is no command to run.
Code used:
var code = "@chcp 65001 >nul & cmd /d/s/c \"C:\\Program Files\\Antivirus\\antivirus.exe\" --console --scan C:\\path\\to\\file"
exec(code, { encoding: "utf-8" }, (err, stdout, stderr) => {
console.log(stdout)
.... // some other code that writes in a file
})
Running @chcp 65001 >nul & cmd /d/s/c dir with filenames that have umlaute will work correctly. Just my command seems to be broken.
So I tried the second answer to use iconv-lite. The answer states that I need to insert the encoding string used by the cmd that I can find on the code page:
replace your encoding string rather than 'CP936'. You can found it in cmd.exe > Properties > codepage.
The codepage my cmd uses is 65001 (utf8) and providing CP65001 will error out with the message that this encoding is unknown. utf8 or utf-8 doesn't error out but also won't display the umlaute correctly..
I am in a bit of lost here. What can I do?
exec()code that you were using.chcp, specify the encoding as inexec("\"C:\\Program Files\\Antivirus\\antivirus.exe\" --console --scan C:\\path\\to\\file", {encoding: "latin1"}, ...)