Alby11
Alby11

Reputation: 645

Weird output from batch script

I'm trying to log the output of the tree command from many remote PCs.

Here's my script

set myFile=multiPsexecList.txt

pushd %~dp0

mkdir RESULTS

for /F "tokens=*" %%i in (%myFile%) do (call :process %%i)
popd
goto :EOF

:process
 set thecomputer=%1
 echo %thecomputer%
 ping -n 1 %thecomputer% | find "TTL=" >nul 
 if errorlevel 1 ( 
     echo offline > RESULTS\OFFLINE_%thecomputer%.log
 ) else (
     net use t: \\%thecomputer%\c$
     tree t:\directory > RESULTS\%thecomputer%.log
     net use t: /delete
 )

The problem is that i get a weird output, as you can see below:

³   ÃÄÄÄDbTransactions
³   ³   ÃÄÄÄ2018-08-07
³   ³   ÃÄÄÄ2018-08-08
³   ³   ÃÄÄÄ2018-08-09
³   ³   ÃÄÄÄ2018-08-10
³   ³   ÃÄÄÄ2018-08-11
³   ³   ÃÄÄÄ2018-08-16
³   ³   ÃÄÄÄ2018-08-17
³   ³   ÃÄÄÄ2018-08-18
³   ³   ÃÄÄÄ2018-08-21
³   ³   ÃÄÄÄ2018-08-22
³   ³   ÃÄÄÄ2018-08-23
³   ³   ÃÄÄÄ2018-08-24
³   ³   ÃÄÄÄ2018-08-25
³   ³   ÃÄÄÄ2018-08-28
³   ³   ÃÄÄÄ2018-08-29

Which is partially correct, except for these "ÃÄÄÄ".

Thanks for your help!

Upvotes: 1

Views: 627

Answers (1)

Mofi
Mofi

Reputation: 49187

The output is absolutely correct because it is:

│   ├───DbTransactions
│   │   ├───2018-08-07
│   │   ├───2018-08-08
│   │   ├───2018-08-09
│   │   ├───2018-08-10
│   │   ├───2018-08-11
│   │   ├───2018-08-16
│   │   ├───2018-08-17
│   │   ├───2018-08-18
│   │   ├───2018-08-21
│   │   ├───2018-08-22
│   │   ├───2018-08-23
│   │   ├───2018-08-24
│   │   ├───2018-08-25
│   │   ├───2018-08-28
│   │   ├───2018-08-29

What you don't know is how characters are encoded which every programmer should really know.

In console environment the character encoding is usually an OEM character encoding which means there is one byte used per character. So the number of characters is limited to 2^8 = 256 characters.

Open a command prompt window and run the command chcp. This command outputs the code page being set by default for console applications depending on the country configured for the used user account in Windows Region and Language settings. For example the code page OEM 850 is used by default in Western European countries and similar code page OEM 437 in North American countries.

But the code page used in GUI applications like Windows Notepad for text files with a character encoding using just one byte per character depending also on country setting is for example Windows-1252 in North American and Western European countries.

Look on the character sets of the referenced code pages:

  • Byte with decimal value 179 is character ³ in Windows-1252 and character in OEM 437/850.
  • Byte with decimal value 195 is character à in Windows-1252 and character in OEM 437/850.
  • Byte with decimal value 196 is character Ä in Windows-1252 and character in OEM 437/850.

So the output is not weird. You just view the text file with using a different code page as used for creating the text file.

The command chcp can be used in a console window with an argument to change code page. But the usage of chcp 1252 at top of the batch file before running tree t:\directory > RESULTS\%thecomputer%.log is of no help because Windows-1252 has no box-drawing characters in its character set. That is the reason why command tree has the parameter /A as output in help on running tree /? to use ASCII characters instead of characters from extended character set of active code page.

Upvotes: 4

Related Questions