How to create a folder with unicode characters in its name

New to FreeBASIC? Post your questions here.
newbieforever
Posts: 84
Joined: Jun 21, 2018 11:14

How to create a folder with unicode characters in its name

Postby newbieforever » Jul 10, 2018 13:48

Hopefully my last problem of this kind:

I know how to read from and to write to files with unicode characters in their names, etc.

But how to create a folder with such a name? MkDir() would work, of course.
newbieforever
Posts: 84
Joined: Jun 21, 2018 11:14

Re: How to create a folder with unicode characters in its name

Postby newbieforever » Jul 10, 2018 14:12

My God, how confused I am (or should I say stupid?), everything already solved here (Josep Roca):

Code: Select all

#Define unicode
#Include once "windows.bi"
#Include once "win/shellapi.bi"

Dim As Wstring * 4   dq     = wchr(34)
Dim As Wstring * 100 folder = "aFolder ž\"
folder = dq & folder & dq

ShellExecuteW(0, "open", "cmd.exe", "/u /c mkdir " & folder, 0, SW_HIDE)
dodicat
Posts: 5254
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to create a folder with unicode characters in its name

Postby dodicat » Jul 10, 2018 15:30

Where there is shellapi there also is msvcrt.dll

Code: Select all

 


declare function wsystem_ cdecl alias "_wsystem" ( as wstring ptr) as long
sub mk(filename as wstring)
   wsystem_("mkdir " + filename)
end sub

Dim As Wstring * 100 folder = "aFolder ž\"
folder = chr(34) & folder & chr(34)

mk(folder)
sleep
newbieforever
Posts: 84
Joined: Jun 21, 2018 11:14

Re: How to create a folder with unicode characters in its name

Postby newbieforever » Jul 10, 2018 15:49

@dodicat:
Oh, fine!
(But exe size or speed would be the same, probably?)

Another subsequent question (not directly associated to the above one):

SaveFile() applied immediately after creating a folder fails, there seems to be a time problem. How to wait in FB until the new folder is available?

(It took me half a day to identify this specific cause of the failure...)
dodicat
Posts: 5254
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to create a folder with unicode characters in its name

Postby dodicat » Jul 10, 2018 16:12

sleep 100 or sleep 200 or whatever gives enough time.
I have noticed this myself in the past.
Any shell command needs some time.
Josep Roca
Posts: 392
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: How to create a folder with unicode characters in its name

Postby Josep Roca » Jul 10, 2018 16:36

With Windows, you can use the CreateDirectoryW API function.
See: https://docs.microsoft.com/en-us/window ... directorya
dodicat
Posts: 5254
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to create a folder with unicode characters in its name

Postby dodicat » Jul 10, 2018 17:55

Josep roca
This works very well.
You don't need chr(34), it a straight forward method.

Code: Select all

 #include "windows.bi"

createdirectoryW("Сергeй Сергeевич",0)
sleep 
Josep Roca
Posts: 392
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: How to create a folder with unicode characters in its name

Postby Josep Roca » Jul 10, 2018 18:03

Just in case our perpetual newbie will ask, the alternative for RmDir would be RemoveDirectoryW, and for ChDir, SetCurrentDirectoryW.
deltarho[1859]
Posts: 1651
Joined: Jan 02, 2017 0:34
Location: UK

Re: How to create a folder with unicode characters in its name

Postby deltarho[1859] » Jul 11, 2018 0:30

newbieforever wrote:How to wait in FB until the new folder is available?

Query its existence in a Do/Loop Until.

I do this all the time when working with the Clipboard.
caseih
Posts: 1300
Joined: Feb 26, 2007 5:32

Re: How to create a folder with unicode characters in its name

Postby caseih » Jul 11, 2018 1:12

A check existence loop has some potential issues, though. What if you asked the shell to create a directory somewhere where you didn't have permission? It will fail silently, and your loop will wait indefinitely for a directory that will never exist. Far better to use the Win32 API calls Josep has referred to, checking for any errors they return. Then later on when you are using this directory you created, check for errors as you go because there could be still be permission errors that crop up (the directory could also theoretically be removed out from under you), locking issues, etc.
deltarho[1859]
Posts: 1651
Joined: Jan 02, 2017 0:34
Location: UK

Re: How to create a folder with unicode characters in its name

Postby deltarho[1859] » Jul 11, 2018 4:38

The 'Do/Loop Until' should not be entered if anything untoward occurred during a directory creation. If there is no evidence of a failed creation, that is when an API returns a success, then without a check existence loop we could still fall foul of a SaveFile() failure.

I am reminded of:
Patient: Doctor, it hurts when I do this.
Doctor: Well, don't do it then.

Of course, a 'Do/Loop Until' can have two exits: One when it is OK to proceed and another when we have been in there too long; although with an API success the latter should not be required.

In the old days with DOS, disk operations were synchronous so a SaveFile() couldn't fail because that would not be executed until any outstanding disk operations had completed.
jj2007
Posts: 897
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to create a folder with unicode characters in its name

Postby jj2007 » Jul 11, 2018 6:57

Pseudocode:

Code: Select all

  MakeDir "أدخل النص هنا"
  .if Zero?
       Print "+"   ; creation was successful
  .else
       Print "-"   ; creation failed
  .endif
  .if IsFolder("أدخل النص هنا")
       PrintLine "success"
  .else
       PrintLine "failure"
  .endif
Creation may fail simply because the folder exists already. So what really counts is the check for existence. Unless, of course, you add a check for the file attributes to the MakeDir routine... it's not that trivial but it can be done. One (exotic) reason for failure could be that a file with the same name exists.

I can't see a good reason for a loop btw.
deltarho[1859]
Posts: 1651
Joined: Jan 02, 2017 0:34
Location: UK

Re: How to create a folder with unicode characters in its name

Postby deltarho[1859] » Jul 11, 2018 8:04

jj2007 wrote:I can't see a good reason for a loop btw.

The success of an API is not necessarily an indication that a task request has completed - it simply means that there is no reason to halt progress.

The reason for a loop is to "wait until the new folder is available".

In the pseudocode, if the creation failed then we should not then test for existence - it cannot exist on a failed create. A failed 'IsFolder' does not mean a success cannot happen - it simply means that it hasn't happened yet.

We have a similar situation with the clipboard functions. We cannot just 'barge through' with one task after another assuming that each is executed at the speed of light. They too have return values.
jj2007
Posts: 897
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to create a folder with unicode characters in its name

Postby jj2007 » Jul 11, 2018 8:19

deltarho[1859] wrote:The reason for a loop is to "wait until the new folder is available".
I am not sure whether that is a real world scenario under Windows: a folder created or existing but not "available". Do you have a link to discussions of such a problem?

if the creation failed then we should not then test for existence - it cannot exist on a failed create.
Creation fails if the folder exists already.

The clipboard case sounds interesting. Any references?
deltarho[1859]
Posts: 1651
Joined: Jan 02, 2017 0:34
Location: UK

Re: How to create a folder with unicode characters in its name

Postby deltarho[1859] » Jul 11, 2018 9:23

jj2007 wrote:I am not sure whether that is a real world scenario under Windows: a folder created or existing but not "available". Do you have a link to discussions of such a problem?

We need to look no further than this thread.
newbieforever wrote:SaveFile() applied immediately after creating a folder fails, there seems to be a time problem. How to wait in FB until the new folder is available?

(It took me half a day to identify this specific cause of the failure...)

dodicat wrote:sleep 100 or sleep 200 or whatever gives enough time.
I have noticed this myself in the past.

jj2007 wrote:The clipboard case sounds interesting. Any references?

No. Many years I wrote an application which sends a string of characters to another application's in focus edit box. The string is broken into small packets and the 'sending' alternates between the clipboard functions and the keyboard functions. I got the idea from the password manager KeePass. However, I needed to slow things down because not all of the string actually made it. It then dawned on me that I may be pasteing before a reset had completed. So on a reset, I waited until I got a success before pasteing. That solved the problem. I should add that I was not using the Clipboard APIs but PowerBASIC's built-in clipboard functions as in the following.

Code: Select all

Do
  Clipboard Reset, ClipResult
Loop Until IsTrue ClipResult
Do
  Clipboard Set Text InText, ClipResult
Loop Until IsTrue ClipResult

Come to think of it caseih may be right. If we test ShellExecute's return value it may take a while to get a return value but if we get a successful return then we may assume that "the new folder is available". If we do not test the return value then SaveFile() may fail because a return value from ShellExecute has not been processed yet, that is the new folder is not available yet. dodicat wrote: "Any shell command needs some time." In which case, we may not get a return value anytime soon.

Of course, the moral of this story is to check API return values. This is especially true of the cryptographic APIs. Get them wrong and you may well get a GPF. On the other hand, an application may complete without anything apparently untoward happening until we check the results and find garbage because an API actually 'went down' and we did not check it.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest