open extended ascii filename (unicode)

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

open extended ascii filename (unicode)

Postby mark_c » Thu Jan 17, 2019 12:21 am

Hello,
how to solve the problem with extended ascii file names (unicode) such as accented characters or with dieresis (German language) or symbols (Chinese language) or Korean language, files that windows can not open by reporting errors.


example: "c:\\mydir\\srçelik" generate error

thank you
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 163
Joined: Thu Jun 21, 2012 1:13 am

Re: open extended ascii filename (unicode)

Postby rlebeau » Thu Jan 17, 2019 11:59 am

Windows can handle such filenames just fine. If you are having errors, it is because of a bug in your own code. Please show the actual code you are having trouble with.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1597
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: open extended ascii filename (unicode)

Postby mark_c » Fri Jan 18, 2019 4:17 am

my mistake,
I'm creating a list of images through the command dir c:\mydir\*. jpg / s / b> mylist.txt and I did not think he could not handle unicode characters, as I said.

I'm leaving the dir command.
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 163
Joined: Thu Jun 21, 2012 1:13 am

Re: open extended ascii filename (unicode)

Postby rlebeau » Fri Jan 18, 2019 10:51 am

mark_c wrote:I'm creating a list of images through the command dir c:\mydir\*. jpg / s / b> mylist.txt and I did not think he could not handle unicode characters, as I said.


Why are you using the command line for that, instead of using a FindFirst/Next() loop in code?
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1597
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: open extended ascii filename (unicode)

Postby mark_c » Fri Jan 18, 2019 11:12 am

Remy, you caught me.
I used the dir command for a strangeness that had happened to me and that only today I solved. I tried using FindFirst / Next () but I could not access mapped network drives, I could only do this if I used the UNC path and I did not understand why.

Unfortunately, using the UNC paths I got too long paths and in some cases I got the "too long path" error.

Going deeper into the question, I found that windows behaves differently if the compiler runs as a normal user or as an administrator, in practice, the problem was due to a policy problem.

In practice: I ran the compiler as an administrator but I mapped the units as users and for this reason the administrator did not see the mapped disks; strange but it is so.

Fixed this problem, I left the dir command that causes the problem of non-ascii characters (Chinese) and I started using FindFirst / Next (), a version a little dated but that works.


Code: Select all
void TForm1::FindMyFile(char *s)
{
        struct ffblk ffblk;
        char temp[MAXPATH]="";
        int done;

        TCHAR szBuf[MAX_PATH];

        strcpy(temp, s);
        strcat(temp, "*.*");
        done=findfirst(temp,&ffblk,_A_SUBDIR | _A_RDONLY | _A_HIDDEN |_A_SYSTEM);

        FILE *fp;
        fp=fopen("list.txt","a+");

        while(!done)
        {
                /* ignore special directories */
                if (strcmp(ffblk.ff_name, ".") && strcmp(ffblk.ff_name, ".."))
                {
                        if (ffblk.ff_attrib & _A_SUBDIR)
                        {
                                strcpy(temp, s);
                                strcat(temp, ffblk.ff_name);
                                strcat(temp, "\\");
                                FindMyFile(temp);
                        }
                        else
                        {
                                AnsiString path=String(s) + String(ffblk.ff_name);

                                if(fp && (strstr(path.c_str(),".jpg") != 0 ||
                                         strstr(path.c_str(),".JPG") != 0)
                                  )
                                {
                                        fprintf(fp,"%s\n", path.c_str());
                                        StatusBar1->Panels->Items[2]->Text="Research in progress...." + IntToStr(found++);
                                }
                        }
                }
                done = findnext(&ffblk);

                Application->ProcessMessages();
        }

        fclose(fp);
}
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 163
Joined: Thu Jun 21, 2012 1:13 am

Re: open extended ascii filename (unicode)

Postby rlebeau » Fri Jan 18, 2019 6:19 pm

mark_c wrote:I used the dir command for a strangeness that had happened to me and that only today I solved. I tried using FindFirst / Next () but I could not access mapped network drives, I could only do this if I used the UNC path and I did not understand why.


FindFirst/Next() work just fine with mapped drives.

mark_c wrote:In practice: I ran the compiler as an administrator but I mapped the units as users and for this reason the administrator did not see the mapped disks; strange but it is so.


Mapped drives are configured on a per-user basis. One user cannot access another user's drive mappings.

mark_c wrote:Fixed this problem, I left the dir command that causes the problem of non-ascii characters (Chinese) and I started using FindFirst / Next (), a version a little dated but that works.


You are using the C runtime library's findfirst()/findnext() functions (note the lower-casing). I was referring to the RTL's FindFirst/FindNext() functions instead (note the camel-casing), for example:

Code: Select all
#include <System.SysUtils.hpp>

void TForm1::FindJpgFiles(String folder, TStrings *files, bool recursive)
{
    folder = IncludeTrailingPathDelimiter(folder);

    TSearchRec sr;
    if (FindFirst(folder + _D("*.*"), faAnyFile, sr) == 0)
    {
        do
        {
            /* ignore special directories */
            if ((sr.Name != _D(".")) && (sr.Name != _D("..")))
            {
                String path = folder + sr.Name;
                if (sr.Attr & faDirectory)
                {
                    if (recursive)
                        FindJpgFiles(path, files, true);
                }
                else
                {
                    if (SameText(ExtractFileExt(sr.Name), _D(".jpg")))
                    {
                        if (files) files->Add(path);
                        StatusBar1->Panels->Items[2]->Text = _D("Research in progress....") + IntToStr(found++);
                    }
                }
            }

            //Application->ProcessMessages();
            Update();
        }
        while (FindNext(sr) == 0);

        FindClose(sr);
    }
}


Code: Select all
TStringList *files = new TStringList;
FindJpgFiles(_D("C:\\some folder\\"), files, true);
// use files as needed...
files->SaveToFile(_D("C:\\some other folder\\list.txt"));
delete files;
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1597
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: open extended ascii filename (unicode)

Postby mark_c » Mon Jan 21, 2019 2:09 pm

thanks Remy,
you are an excellent teacher
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 163
Joined: Thu Jun 21, 2012 1:13 am


Return to Technical

Who is online

Users browsing this forum: Google [Bot] and 13 guests