Write CMD output to file as UTF-8

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Automation Wizard
Posts: 1048
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden

Write CMD output to file as UTF-8

Post by Grovkillen » Mon Oct 05, 2020 6:56 am

I have had a long time nag with my command line output to file being not UTF-8. I have now found the solution to my problem.

Code: Select all

  RunProgram>cmd /c chcp 65001 > nul & cmd /c dir "%FILE_ROOT_PATH%\*.*" /s /b /o:gn /A-D > "%FILE_ROOT_PATH%\results.txt"
To break it down...
cmd /c chcp 65001 > nul
cmd /c dir "%FILE_ROOT_PATH%\*.*" /s /b /o:gn /A-D > "%FILE_ROOT_PATH%\results.txt"
The first part is to set the output format to UTF-8 (chcp 65001)
The & tells the command prompt to execute the next part in the same run as the first
The third part is the standard file dir command with the following switches:
/s = list files in subfolders as well
/b = remove the header and footer part of the output
/o:gn = sort the list alphabetically
/A-D = only list files

Running: 15.0.24
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts