Computer Hope

Microsoft => Microsoft DOS => Topic started by: DaveLembke on February 23, 2018, 01:06:26 PM

Title: unicode and batch append to file from tree command
Post by: DaveLembke on February 23, 2018, 01:06:26 PM
Curious if there is a way to get unicode to append to text file within a batch script.

Working on a batch script that selectively writes TREE information to text file and what I found so far is a way to manually copy paste by populating |CLIP buffer with the info and then CTRL + V but trying to find a method within batch itself that will pass unicode correctly to the text file.


Instead of getting the nice unicode lines that show the branching in the structure like the image example here: https://www.computerhope.com/treehlp.htm


I am getting:


Quote
Folder PATH listing for volume OS
Volume serial number is 603A-B09C
C:.
ÃÄÄÄ$WINDOWS.~BT
³   ÀÄÄÄSources
³       ÀÄÄÄPanther
ÃÄÄÄ7d24e41789f0f5d8f4326cc82fd9
ÃÄÄÄBackup
³   ÃÄÄÄAPWU
³   ³   ÃÄÄÄ2017
³   ³   ÀÄÄÄ2018
³   ÃÄÄÄAPWU CDs
³   ³   ÃÄÄÄ+++2010-2015 National Agreement
³   ³   ÃÄÄÄ+++ELM36
³   ³   ÃÄÄÄ+++Handbooks
³   ³   ³   ÃÄÄÄ+++ELM 17.14 (March 2006)
³   ³   ³   ³   ÃÄÄÄelmapxs
³   ³   ³   ³   ÃÄÄÄelmc4
³   ³   ³   ³   ÃÄÄÄelmc5
³   ³   ³   ³   ÃÄÄÄelmc6
³   ³   ³   ³   ÀÄÄÄelmc8

The only work around I have found to getting a text file with this in correct unicode format is to do a manual process of running:

tree /f /a |clip

And then pasting the information to notepad from the clipboard buffer. But this is a very manual process, and I was hoping to have a batch run and produce the output of multiple specific trees without having to manually run this for each tree and paste manually to notepad.  :-\

Maybe there is a trick after running this to dump the clipboard buffer to file without having to do this manual paste and save process?  :-\

Maybe vbscript would be able to access the buffer that DOS normally has no access to that I know of, but I'm not good at vbscript and just being suggestive of a method maybe.

Title: Re: unicode and batch append to file from tree command
Post by: BC_Programmer on February 23, 2018, 01:58:53 PM
Well, for starters- Part of the problem here is that the text that TREE outputs is not unicode. It's ASCII text that uses extended code page 437.

Code pages indicate what the extended ASCII set contains (eg characters from 128 upwards). You may be familiar with the smiley faces and blocks and shaded block characters on older MS-DOS systems, in addition to the line drawing characters- that was in Code page 437 as well.

UTF-8 and Unicode in general wasn't built upon that standard extended ASCII character set. Those characters were kind of useless and so it was built on top of a standardized international code page, ANSI Latin 1, I think.

in Code page 437, ASCII code 162 was └. But, in UTF-8, code 162 is À. You might see the problem. The text you output is being interpreted as UTF-8, rather than code-page 437 ASCII. But you cannot realistically fix that.

Notepad works because it seems that it detects the input text when you paste it. It converts └ to Unicode character E2 94 94 for example. That's why it warns you about loss of information if you try to save as ANSI, since it's no longer the same input ASCII text.

Anyway, onto finding a solution.

The easiest workaround is to simply use the /A switch on TREE so that it uses standard ASCII characters that are part of UTF-8.

If you really must have the special line-drawing characters, you need to figure out how to effectively force the Code page 437 characters to instead be their unicode equivalents. Basically you need to automatically do the conversion notepad does.

One way that seems to work is simply running it under powershell. if you run the command through powershell, the output appears to be proper unicode in the file:

Code: [Select]
powershell -command "tree > treeout.txt"


Title: Re: unicode and batch append to file from tree command
Post by: DaveLembke on February 24, 2018, 08:14:52 AM
Cool... Thanks BC I will give this a try. Also thanks for clarity on why this was happening due to UTF-8 interpretation.

I have a project where I need to look through trees of files and folders and check off on paper where I left off in doing an audit of data and there are paid for products out there that can print directory trees, but figured there is a free way of doing it in just printing out the tree from file.

The weird thing is like 25+ years ago I thought I was able to send tree output directly to my dot matrix printer but there is no switch to pass it to printer looking back now, so I cant remember how I printed out the tree. Maybe there was a DOS program I used that I forgot about that acted as a display redirection to printer or something echoing everything out to printer etc, but it printed to paper exactly as you see it command shell output.

 Thanks for your help.   8)
Title: Re: unicode and batch append to file from tree command
Post by: BC_Programmer on February 24, 2018, 09:58:08 AM
The weird thing is like 25+ years ago I thought I was able to send tree output directly to my dot matrix printer but there is no switch to pass it to printer looking back now, so I cant remember how I printed out the tree.

 Thanks for your help.   8)

That probably was:
Code: [Select]
tree > PRN
Not sure if it works now.
Title: Re: unicode and batch append to file from tree command
Post by: DaveLembke on February 25, 2018, 09:41:59 AM
Oh a redirection to Printer as PRN that probably was it.  8)

Just tried the solution you suggested and it works awesome. Thanks for your help. Now to start the audit.  :)


Quote
Folder PATH listing for volume OS
Volume serial number is 603A-B09C
C:.
├───.oracle_jre_usage
├───AppData
│   ├───Local
│   │   ├───Adobe
│   │   │   ├───Acrobat
│   │   │   │   └───10.0
│   │   │   └───Color
│   │   │       └───Profiles
│   │   ├───Avg
│   │   │   ├───Antivirus
│   │   │   └───log
│   │   │       ├───bav1
│   │   │       ├───fmw1
│   │   │       │   └───Dictionaries
│   │   │       ├───fmwlight
│   │   │       └───zen1
│   │   ├───AVG Web TuneUp
│   │   │   ├───cache
│   │   │   │   └───tmp
│   │   │   ├───Chrome
│   │   │   ├───DNT
│   │   │   ├───Firefox
│   │   │   ├───IE
│   │   │   │   ├───cef_cache
│   │   │   │   └───tmp
│   │   │   └───Statistics
│   │   ├───Avg2015
│   │   │   ├───dumps
│   │   │   ├───fet
│   │   │   ├───log
│   │   │   └───temp
Title: Re: unicode and batch append to file from tree command
Post by: Squashman on February 25, 2018, 06:53:46 PM
Notepad++ will display it correctly if you change the Encoding to OEM-US.