How to implement the correct work with folders in Russian in Windows os.setlocale ('ISO-8859-5'), os.setlocale ("Russian_Russia.1251") and so on. The desired result did not lead ... enter image description here

I inserted the code you proposed, the result is the same: enter image description here

OS: WIN 7, Lua - LuaForWindows binary (lua 5.1).

I tried in the console, the same result ... enter image description here

    2 answers 2

    Try using the ex external library and the 1251-> 866 transcoding function to bypass directories:

    require 'ex' --[[ 1251: А - Я а - п 192 - 239 866: А - Я а - п 128 - 175 1251: р-я 240 - 255 866: р-я 224 - 239 1251: Ё- 168 ё - 184 866: Ё- 240 ё - 241 ]] local function from1251_to866(s) local str = '' for i=1, string.len(s) do local byte = s:byte(i) local b = byte if (byte >= 192 and byte <= 239) then b = byte-64 elseif (byte >= 240 and byte <= 255 ) then b = byte-16 elseif byte == 168 then b=240 --Ё elseif byte == 184 then b=241 --ё end str = str .. string.char(b) end return str end local function CheckDir(curpath,d) local fullname = (curpath.."\\"..d.name):gsub("\\$","") print(from1251_to866(fullname)) for entry in os.dir(fullname) do if entry["type"]=="directory" then CheckDir(fullname, entry) end if entry["type"]=="file" then print( from1251_to866(fullname .."\\" ..entry.name) ) end end end -- тестируем : local s = [[АБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ абвгдеёжзиклмнопрстуфхцчшщъыьэюя 0123456789 qwertyQWERTY !@#$%^&*()_+]] print(from1251_to866(s) ) local pathname = os.currentdir() local init = { ["type"]="directory", name="" } CheckDir( pathname, init ) 

    console output in 866 (DOS mode):

     АБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ абвгдеёжзиклмнопрстуфхцчшщъыьэюя 0123456789 qwertyQWERTY !@#$%^&*()_+ d:\Projects\Lua\tmp d:\Projects\Lua\tmp\include2 d:\Projects\Lua\tmp\test.lua d:\Projects\Lua\tmp\test.luac d:\Projects\Lua\tmp\test2.lua d:\Projects\Lua\tmp\test2.luac d:\Projects\Lua\tmp\папка на русском d:\Projects\Lua\tmp\папка на русском\inc_inc d:\Projects\Lua\tmp\папка на русском\inc_inc\meta.lua d:\Projects\Lua\tmp\папка на русском\meta.lua 
    • In which version of Lua is this library built in? - zed
    • My ex.dll in 5.1 lies in \ clibs you can take here the dll . More links: the project itself and GitHub - Mike V.
    • @zed forgot to address the comment above - Mike V.
    • Well, this means that it is not built. This is just some external module. - zed
    • one
      @ Vitaliy_123 well, so there is the encoding 866 most likely in your Far. Lua displays in 1251, to view in 1251 run the command chcp 1251 as in the screenshot - Mike V.

    Test case:

     local lfs = require("lfs") local function print_dir() for f in lfs.dir("b:\\") do print(f) end end print("Current locale: " .. os.setlocale(nil, "ctype")) os.execute("chcp") print("Кракозябры") print_dir() os.setlocale("", "ctype") print("Current locale: " .. os.setlocale(nil, "ctype")) os.execute("chcp") print("Нормальный текст") print_dir() 

    If you save it to a file with Win-1251 encoding and run it in the console, you’ll get this:

    enter image description here

    As you can see, after we specified Lua, which locale we are dealing with (i.e., what encoding of the file with the files and, accordingly, in what encoding of the line we have), the print function did some "under the hood" work and output In the console turned out in the right encoding. The magic is as follows:

    • if the locale is not set (i.e., the default "C" -local is used), then the text is displayed as is. And since the file encoding 1251, then it is in it and displayed.
    • if the locale is set, the text from the specified locale is translated into the current locale of the console (ie, from 1251 to 866).

    As for ZeroBrainStudio, there the samples need to be converted to UTF-8, since it perceives all I / O as UTF-8. But, for example, the lfs module lfs system-encoded strings (1251) and in order to get readable output in the ZeroBrainStudio console, the strings need to be manually converted from 1251 to UTF-8 using some external libraries.

    • Thank you for the detailed response. Currently received comprehensive answers. - Vitaliy_123