6

Simplified issue:

I have a file called Bíblia sagrada.exe (note the í) on C:\ of my computer.

When I open a command prompt on C:\ and execute dir /b /on B*.*, it will return:

Bíblia sagrada.exe

i.e., no issues with diacritic symbols.

But when I do dir /b /on B*.* > c:\a.txt and open c:\a.txt with Notepad, it becomes:

B¡blia sagrada.exe

i.e., it replaces í with ¡.

And if I perform for /f "delims=" %i in ('dir /b /on B*.*') do %i > %i.txt it puts the correct name in the filename but inside it the text is still wrong, i.e.,

Filename:

Bíblia sagrada.exe.txt

Contents:

B¡blia sagrada.exe

What's going on?

PS:

Real issue

The issue for me is worse than I reported on first edit, because I read the contents of a txt file to create a .vbs script and then to execute it; when trying to execute, "it doesn't understand" that it points to "Bíblia sagrada.exe" and it does not execute the program... i.e., I created a .bat on d:\atalhizador\atalhizador.bat:

@echo off

::locais
set drvbak=
set drvprogrs=
set pathbak=%cd%
set pathabove=
set pathprogrs=%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

::identificador da pasta do programa no menu iniciar
set id=_pext1

::determina drives de acordo com paths
for /f "delims=" %%z in ("%pathbak%")    do set drvbak=%%~dz
for /f "delims=" %%z in ("%pathprogrs%") do set drvprogrs=%%~dz

::pega caminho superior
cd..
set pathabove=%cd%
cd %pathbak%

cls
echo.***************
echo.* ATALHIZADOR *
echo.***************
echo.
echo.Deseja realmente executar o atalhizador para a pasta:
cd..
echo.  %cd%
cd %pathbak%
echo. [s/n]:
set /p ___o=
if not "%___o%"=="s" if not "%___o%"=="S" goto end

echo.
echo.Criando os atalhos...
for /f "delims=" %%a in ('dir /b /ad /on ..') do (
  echo.- pasta "%%a"...
  echo.  ^|-- buscando arquivos atalhizaveis...
  if not exist "temp" mkdir "temp"
  dir /b /on "..\%%a\*.exe" 1>temp\busca.txt 2>temp\erros.txt
  for /f "delims=" %%b in (temp\busca.txt) do (
    echo.      ^|-- criando atalho para %%b

    for /l %%z in (1,1,1) do set __temp__=

    if not exist "%pathprogrs%\%id%" mkdir "%pathprogrs%\%id%"
    if not exist "%pathprogrs%\%id%\%%a" mkdir "%pathprogrs%\%id%\%%a"

    echo.Set oWS = WScript.CreateObject^("WScript.Shell"^) > temp\%%~na-%%~nb.vbs
    echo.sLinkFile = "%pathprogrs%\%id%\%%a\%%b.lnk" >> temp\%%~na-%%~nb.vbs
    echo.Set oLink = oWS.CreateShortcut^(sLinkFile^) >> temp\%%~na-%%~nb.vbs
    echo.oLink.TargetPath = "%pathabove%\%%a\%%b" >> temp\%%~na-%%~nb.vbs
    echo.oLink.Save >> temp\%%~na-%%~nb.vbs

    cscript /nologo temp\%%~na-%%~nb.vbs
  )
)

:end
%drvbak%
cd %pathbak%

pause
@echo off

The mentioned "Bíblia sagrada.exe" is in d:\Bíblia sagrada.

The generated .vbs file is below (suppose that there are only these two directories on d:), with filename D:\atalhizador\temp\Bíblia Sagrada-Bíblia Sagrada.vbs:

Set oWS = WScript.CreateObject("WScript.Shell") 
sLinkFile = "C:\Users\Ubirajara\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\_pext1\B¡blia Sagrada\B¡blia Sagrada.exe.lnk" 
Set oLink = oWS.CreateShortcut(sLinkFile) 
oLink.TargetPath = "D:\_pext1\B¡blia Sagrada\B¡blia Sagrada.exe" 
oLink.Save 

The issue? The shortcut isn't created because it does not recognize "sLinkFile = "C:\Users\Ubirajara\AppData\Roaming\Microsoft\Windows\Start Menu\Programs_pext1\B¡blia Sagrada\B¡blia Sagrada.exe.lnk"".

It doesn't happen if I don't use diacritics, but really I have a lot of folders and files with that signals...

How to correct it to allow correct writing on .vbs file?

PS2:

I tried following commands, alone and together, with no success:

cmd /u

chcp 860

chcp 1200

mode con cp select=860

mode con cp select=1200

Also I tried it with cmd raster font and with other available fonts...

kokbira
  • 5,429

4 Answers4

4

Notepad is opening your file with the wrong encoding. Try opening using the Open dialog box, and in the bottom selecting other encodings:

Encoding selection

Note that Microsoft uses non-standard names for the encodings. "Unicode" is actually UTF-16LE, and "Unicode big endian" is actually UTF-16BE.

Mechanical snail
  • 7,963
  • 5
  • 48
  • 67
3

Setting the codepage to UTF-8 should help :

chcp 65001

The dir result will then be encoded in UTF8.

If VBS cannot understand it as-is, for the file to be automatically recognized under Windows as UTF8 you will need to prefix it with the UTF8 BOM bytes :

0xEF, 0xBB, 0xBF

You can then start with a file containing the BOM and append to it rather than write :

chcp 65001
copy EFBBBF.txt a.txt
dir /b /on B*.* >> a.txt

Or you may use copy /b to concatenate the files.

harrymc
  • 498,455
1

You can circumvent the issue by adding /X to all DIR commands.

That will give you the 8.3 short name (DOS-name) of all files; all further operations are valid with this name, and it will not have any of the UTF issues.

Aganju
  • 10,161
1

To solve the part where wrong font is shown in Notepad you need to use codepage because Notepad use Ansi as default. A better solution is rather using Notepad++ where you can change to the codepage you like and on top of that you could even change to a better shell with support for unicode so that the error doesn't arise to begin with.