How to create a folder with unicode characters in its name
-
- Posts: 117
- Joined: Jun 21, 2018 11:14
How to create a folder with unicode characters in its name
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.
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.
-
- Posts: 117
- Joined: Jun 21, 2018 11:14
Re: How to create a folder with unicode characters in its name
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)
Re: How to create a folder with unicode characters in its name
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
-
- Posts: 117
- Joined: Jun 21, 2018 11:14
Re: How to create a folder with unicode characters in its name
@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...)
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...)
Re: How to create a folder with unicode characters in its name
sleep 100 or sleep 200 or whatever gives enough time.
I have noticed this myself in the past.
Any shell command needs some time.
I have noticed this myself in the past.
Any shell command needs some time.
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: How to create a folder with unicode characters in its name
With Windows, you can use the CreateDirectoryW API function.
See: https://docs.microsoft.com/en-us/window ... directorya
See: https://docs.microsoft.com/en-us/window ... directorya
Re: How to create a folder with unicode characters in its name
Josep roca
This works very well.
You don't need chr(34), it a straight forward method.
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
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: How to create a folder with unicode characters in its name
Just in case our perpetual newbie will ask, the alternative for RmDir would be RemoveDirectoryW, and for ChDir, SetCurrentDirectoryW.
-
- Posts: 4292
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: How to create a folder with unicode characters in its name
Query its existence in a Do/Loop Until.newbieforever wrote:How to wait in FB until the new folder is available?
I do this all the time when working with the Clipboard.
Re: How to create a folder with unicode characters in its name
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.
-
- Posts: 4292
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: How to create a folder with unicode characters in its name
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.
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.
Re: How to create a folder with unicode characters in its name
Pseudocode: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.
Code: Select all
MakeDir "أدخل النص هنا"
.if Zero?
Print "+" ; creation was successful
.else
Print "-" ; creation failed
.endif
.if IsFolder("أدخل النص هنا")
PrintLine "success"
.else
PrintLine "failure"
.endif
I can't see a good reason for a loop btw.
-
- Posts: 4292
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: How to create a folder with unicode characters in its name
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.jj2007 wrote:I can't see a good reason for a loop btw.
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.
Re: How to create a folder with unicode characters in its name
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?deltarho[1859] wrote:The reason for a loop is to "wait until the new folder is available".
Creation fails if the folder exists already.if the creation failed then we should not then test for existence - it cannot exist on a failed create.
The clipboard case sounds interesting. Any references?
-
- Posts: 4292
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: How to create a folder with unicode characters in its name
We need to look no further than this thread.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?
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.
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.jj2007 wrote:The clipboard case sounds interesting. Any references?
Code: Select all
Do
Clipboard Reset, ClipResult
Loop Until IsTrue ClipResult
Do
Clipboard Set Text InText, ClipResult
Loop Until IsTrue ClipResult
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.