DOS TREE line drawing characters: How to print?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

DOS TREE line drawing characters: How to print?

#1 Post by Clueless in Seattle » 31 Aug 2014 13:17

I ran the TREE command in the XP command prompt box and redirected the output to a text file.

But when I looked at the file in Notepad, Wordpad, and Word, the line drawing characters were rendered as upper case "A" characters with little doodads over the top of them (I've pasted the text at the end of this post).

I'm hopeing someone reading this might be able to point me to a Windows program that will run under XP or Win 7 that will correctly render those old DOS line drawing characters.

Will in Seattle
a.k.a. "Clueless"

P.S. Here's what the file looks like when I open it in Notepad:

Folder PATH listing for volume Insprion_C
Volume serial number is 7C91B304 CC4C:3DB7
C:.
ÃÄÄÄDell Laptop MP3s
³ ÃÄÄÄPlaylists
³ ³ ÃÄÄÄOrphanned Playlists
³ ³ ÀÄÄÄPlaylists from Buffalo
³ ÀÄÄÄRadio & Spoken Word, Dell 1150
ÃÄÄÄHarry Simeone Chorale - The Wonderful Songs Of Christmas
ÃÄÄÄMP3 INBOX on INSPIRON 1150
³ ÃÄÄÄEverything Learn French
³ ÃÄÄÄMINDFULNESS
³ ÃÄÄÄVirus Check
³ ³ ÀÄÄÄNormalize
³ ³ ÀÄÄÄTags
³ ³ ÃÄÄÄ2010AiC
³ ³ ÃÄÄÄAudition
³ ³ ³ ÀÄÄÄTransfer
³ ³ ÀÄÄÄTag Repair
³ ÀÄÄÄ_LEFTOVERS
ÃÄÄÄStokes Field Guide to Bird Songs
ÀÄÄÄ_INBOX Dell Laptop WAV files

aGerman
Expert
Posts: 4745
Joined: 22 Jan 2010 18:01
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#2 Post by aGerman » 31 Aug 2014 13:34

It's because TREE and Notepad use different character encodings.

If you would include option /a then TREE wouldn't use the box characters but the default ASCII |+-\ . If that is not sufficient we could try to convert the output (e.g. with a Batch - JScript hybrid code). Just tell us ...

Regards
aGerman

EDIT:
Try something like that (save as *.bat)

Code: Select all

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

:: Batch part
@echo off &setlocal
set "outfile=tree.txt"

>"%outfile%" tree

for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
cscript //E:JScript //nologo "%~fs0" "%oemcp%" "%outfile%"

exit /b


:: JScript part */
var streamASCII = WScript.CreateObject("ADODB.Stream");
streamASCII.Type = 2;
streamASCII.CharSet = "cp" + WScript.Arguments(0);
streamASCII.Open();
streamASCII.LoadFromFile(WScript.Arguments(1));
var streamUTF8 = WScript.CreateObject("ADODB.Stream");
streamUTF8.Type = 2;
streamUTF8.Charset = "utf-8";
streamUTF8.Open();
streamUTF8.WriteText(streamASCII.ReadText(-1));
streamUTF8.SaveToFile(WScript.Arguments(1), 2);
streamUTF8.Close();
streamASCII.Close();

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS TREE line drawing characters: How to print?

#3 Post by foxidrive » 31 Aug 2014 20:59

aGerman wrote:Try something like that (save as *.bat)


It works well here. :thumbsup:

aGerman
Expert
Posts: 4745
Joined: 22 Jan 2010 18:01
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#4 Post by aGerman » 01 Sep 2014 09:55

Using ADO streams is at least the easiest way I know to convert character encodings. You could take any charset providing the target charset supports all characters of the source file. That's the reason why I had to use UTF-8 above. The box characters are not part of the default ANSI (Windows-1252).
You will find a lot of the alias names in the registry
HKEY_CLASSES_ROOT\MIME\Database\Charset
But my experiences are that this list is incomplete and for OEM- (ASCII-, Batch-) code pages you can always use their numbers with CP prepended (e.g. "CP437" or "CP850").

Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#5 Post by penpen » 01 Sep 2014 12:04

I think the pure batch solution is more easy (only 2 lines of code per conversion).

But if you want to store it in UTF-8 then you have to first convert it to UTF-16 LE.
In addition you need the BOM (byte order marks) for UTF-16 LE and UTF-8.

You could create these files manually (using notepad), or you could use http://www.dostips.com/forum/viewtopic.php?f=3&t=5326, to do this automatically (-> ":createBom").
You only have to create them once, so it is no big problem to create them manually (only my point of view):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "batchDir=%~dp0"
set "actualDir=%cd%"
set "Bom8File=%~dp0UTF-8 BOM.txt"
set "Bom16File=%~dp0UTF-16 LE BOM.txt"
for /F "tokens=2 delims=:." %%a in ('chcp') do set "cp=%%~a"

:: create bom file if needed
if not exist  "%Bom8File%" call :createBom  "%Bom8File%" 239 187 191
if not exist "%Bom16File%" call :createBom "%Bom16File%" 255 254


:: convert to UCS-2 (subset of UTF-16 LE)
>nul copy /Y "%Bom16File%" tree16.txt
cmd /D /Q /U /C "(for /F "tokens=* delims=" %%a in ('tree.com') do echo(%%~a) >> "tree16.txt""

:: convert to UTF-8
>nul copy /Y "%Bom8File%" tree8.txt
cmd /D /Q /A /C ">nul chcp 65001 & cmd /D /Q /A /C type "tree16.txt" >>tree8.txt & >nul chcp %cp%"

endlocal
goto :eof


:createBom
   setlocal enableExtensions enableDelayedExpansion
   set "target="
   set "params="
   for %%N in (
      %*
   ) do   if not defined target ( set "target=%~1"
   ) else if not defined params ( set "params="%%~N""
   ) else                       ( set "params=!params! "%%~N""
   )

   if not defined target exit /b 1
   if not defined params exit /b 1

   REM This part of the batch file uses the result of the Topic on "www.dostips.com":
   REM   Create nul and all ascii characters with only batch
   REM   http://www.dostips.com/forum/viewtopic.php?f=3&t=5326
   REM   Teamwork of carlos, penpen, aGerman, dbenham, einstein1969
   REM I only use the characters, needed: 0xEF, 0xBB, 0xBF, 0xFF, 0xFE
   >"t.tmp" type nul
   (
      for %%N in (%params%) do if not exist "%%~N.chr" (
         makecab /d compress=off /d reserveperfoldersize=%%~N /d reserveperdatablocksize=26 "t.tmp" "%%~N.chr"
         type "%%~N.chr" | ((for /l %%I in (1 1 38) do pause)&(findstr "^">"temp.tmp"))
         copy /y "temp.tmp" /a "%%~N.chr" /b
      )
   ) > nul

   >"%target%" type nul
   for %%N in (%params%) do >nul copy /b "%target%" /b + "%%~N.chr" /b "%target%" /b

   for %%N in (%params%) do if exist "%%~N.chr" del "%%~N.chr"
   del "t.tmp", "temp.tmp"
   endlocal
   goto :eof
(Tested on WinXp 32 home.)

penpen

Edit: Replaced the local files names "UTF-16 LE BOM.txt" and "UTF-8 LE BOM.txt" (<- wrong too) with the files in the batch path; thanks to aGerman.
Last edited by penpen on 01 Sep 2014 16:13, edited 1 time in total.

aGerman
Expert
Posts: 4745
Joined: 22 Jan 2010 18:01
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#6 Post by aGerman » 01 Sep 2014 12:52

Good idea penpen :)

Please correct line 19 where you try to copy "UTF-8 LE BOM.txt". You created "UTF-8 BOM.txt" though.

In case the editor saves the Batch code in Windows-1252 (which should be usually true for the western hemisphere) you could simplify the creation of the BOM files. E.g.

Code: Select all

:createBom
   >nul chcp 1252
   <nul >"%Bom16File%" set /p "=ÿþ"
   <nul >"%Bom8File%" set /p "="
   >nul chcp %cp%
   goto :eof

Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#7 Post by penpen » 01 Sep 2014 16:31

Nice :D .
I haven't even thought about that :oops: :

Code: Select all

:: Authors: aGerman, and penpen
:: Assumed: source codepage 1252
@echo off
setlocal enableExtensions disableDelayedExpansion
for /F "tokens=2 delims=:." %%a in ('chcp') do set "cp=%%~a"

:: create bom for "tree16.txt" and "tree8.txt"
>nul chcp 1252
<nul >"tree16.txt" set /p "=ÿþ"
<nul > "tree8.txt" set /p "="
>nul chcp 850

:: convert to UCS-2 (subset of UTF-16 LE)
cmd /D /Q /U /C "(for /F "tokens=* delims=" %%a in ('tree.com') do echo(%%~a) >> "tree16.txt""

:: convert to UTF-8
cmd /D /Q /A /C ">nul chcp 65001 & cmd /D /Q /A /C type "tree16.txt" >>tree8.txt & >nul chcp %cp%"

endlocal
goto :eof
I have shortened the source code a little bit.

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS TREE line drawing characters: How to print?

#8 Post by foxidrive » 01 Sep 2014 17:37

Seems to work fine too guys.

tree16.txt and tree8.txt appear to look the same in notepad, in a simple test.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: DOS TREE line drawing characters: How to print?

#9 Post by Samir » 01 Sep 2014 17:45

I've ran across this before.

If you want to preserve the nicer graphical characters, then you'll need to use a better text editor like notepad++ (or another one that supports displaying these characters. Or I believe you can try changing the font in Notepad to system or terminal or 8514oem if you have one of these and that might display them correctly.

If you just want to see the tree properly period, the /a switch is the easiest thing. 8)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#10 Post by penpen » 02 Sep 2014 10:30

It is not really a font issue: It is a character encoding issue.
Notepad++ indeed is able to display such characters in the right way,
because it is able to to change the codepage to (for example) OEM 850.

But:
Notepad supports displaying characters like '├' (U+251C), just open tree8.txt or tree16.txt.

The only font that displays the desired result is "Terminal" (Small Font File: VGAOEM.FON; Large Font File: 8514OEM.FON),
because it bypasses the unicode translation, and it is the font known as "raster font" used by the command shell.
(And that is the reason why this font displays the right glyphs.)
No other font (shipped with windows) will show that result.

penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: DOS TREE line drawing characters: How to print?

#11 Post by Samir » 07 Sep 2014 15:46

penpen wrote:It is not really a font issue: It is a character encoding issue.
Notepad++ indeed is able to display such characters in the right way,
because it is able to to change the codepage to (for example) OEM 850.

But:
Notepad supports displaying characters like '├' (U+251C), just open tree8.txt or tree16.txt.

The only font that displays the desired result is "Terminal" (Small Font File: VGAOEM.FON; Large Font File: 8514OEM.FON),
because it bypasses the unicode translation, and it is the font known as "raster font" used by the command shell.
(And that is the reason why this font displays the right glyphs.)
No other font (shipped with windows) will show that result.

penpen
The original notepad didn't have fonts, so it never had this problem. Like you said it is character encoding/decoding as notepad uses fonts to decode the characters.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#12 Post by penpen » 07 Sep 2014 17:14

The (original) notepad within xp is able to change fonts.
You may select it clicking: [Menubar ->] Format -> Fonts.

penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: DOS TREE line drawing characters: How to print?

#13 Post by Samir » 07 Sep 2014 20:53

penpen wrote:The (original) notepad within xp is able to change fonts.
You may select it clicking: [Menubar ->] Format -> Fonts.

penpen
The original notepad I was referring owas Windows 3.0/3.1. Notepad was basically just a graphical text editor like edit back in those days. 8)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS TREE line drawing characters: How to print?

#14 Post by foxidrive » 07 Sep 2014 22:46

Samir wrote:The original notepad I was referring owas Windows 3.0/3.1. Notepad was basically just a graphical text editor like edit back in those days. 8)


I opened the notepad.exe from Win 3.11 under Windows 8 and it's true that it doesn't have the ability to set any font, but it doesn't display the line drawing characters from tree

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: DOS TREE line drawing characters: How to print?

#15 Post by penpen » 08 Sep 2014 04:47

Sidenote: The win 3.11 notepad also uses fonts.

Actually i have no win 3.11 anymore (2 of the disks are damaged...),
but if i remember right, then it was possible to change the font used by notepad... and all other applications, that are using the "System" font.

But there is only one other possible (shipped with win 3.11): The "Terminal" font.

But this was not easy, and i don't remeber enough, maybe googling for this...:
You "only" have to copy "vga.drv" to "vgaT.drv" and hex edit "vgaT.drv".
I'm not sure anymore what and where to change exactly...
Maybe search null terminated String "System" and replace with null terminated String "Terminal"..., or was it a font number..., or both... couldn't remember anymore, sorry... .

And then you have to change the system.ini search the line with "vgasys.fon"
to "vga850.fon", and "vga.drv" to "vgaT.drv" :
I'm not sure with the number, the file should be within "windows\system".

penpen

Post Reply