How to create a folder with unicode characters in its name

New to FreeBASIC? Post your questions here.
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

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

Post by Josep Roca »

It is a very bad practice to no check return values. I know one guy that has often adapted some of my PowerBasic examples making them more "compact" by removing error checking and then has asked me why it sometimes crashed.

In the cases in which you have to do many checks, multiple nested IF / END IF can be annoying. In these cases I sometimes use a little trick:

Code: Select all

DO
   Call a function
   IF error THEN do something if needed: EXIT DO
   Call another function
   IF error THEN do something if needed: EXIT DO
   ...
   ...
   ...
   EXIT DO   ' this ensures that the code inside the loop is executed only once
LOOP
It has the same effect that using IF error THEN GOTO xxx, but is does not offend to the enemies of GOTO.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

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

Post by deltarho[1859] »

I use something like this:

Code: Select all

If dwStatus <> STATUS_SUCCESS Then lError = 10 : Goto ErrorTrap
and I don't care who I offend. <smile>
lError tells me which API failed.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

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

Post by jj2007 »

deltarho[1859] wrote:
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...)
Just tested this, see Create a hundred folders. No such problem under Windows 7-64 and Windows 10 (and I would have been utterly surprised if there was any problem with it). Creating 100 folders and writing text to them takes less than 0.2 seconds on my Core i5.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Post by dodicat »

The C runtime is quite fast even with two wide shell instructions.

Code: Select all

 

  
#include "crt.bi"

declare function wsystem_ cdecl alias "_wsystem" ( as wstring ptr) as long 'not in crt.bi

function MakeFolder(folder as wstring) as long
  return _wmkdir(@folder)
end function

function RemoveFolder(folder as wstring) as long
     return _wrmdir(@folder)
    end function
    
function savefile(filename as wstring,txt as wstring="" ) as long
dim as wstring * 20 r="w,ccs=UNICODE"
 dim as file ptr fp=_wfopen(@filename,@r)
   fputws(@txt,fp)
   function=iif(*cast(long ptr,fp),0,-1)
   fclose(fp)
end function

function removefile(filename as wstring) as long
    return _wremove(@filename)
    end function

Dim As Wstring * 100 folder = "aFolder ž\"

 
for n as long=1 to 100
    locate 5,,0
    print n;" of 100"
    
if MakeFolder(folder)<>0 then print "Failed to make  ";folder
if savefile(folder + "/new file.txt","HELLO is written inside this text file")<>0 then print "Failed to save  "

dim as string path= chr(34)+folder+chr(34) + """\new file.txt""" 'double quotes for shell instructions incase of a space in names

wsystem_("dir/B "+ path)  'unicode shell 
wsystem_("type " + path)  'unicode shell
print

if removefile(folder + "/new file.txt") <>0 then print "Failed to remove" 'remove the file first
if RemoveFolder(folder)<>0 then print "Failed to remove  ";folder         'remove the folder

next


sleep 
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

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

Post by deltarho[1859] »

jj2007 wrote:No such problem under Windows 7-64 and Windows 10
I haven't experienced this issue myself. I am only going on what newbieforever and dodicat wrote and trying to fathom out what may be happening.

One thing is for sure and that is Windows does not give us API return values for the fun of it.
Just tested this,
One hundred positives do not imply that a negative cannot exist. What you are doing there is 'Proof by non-contradiction' and that is false unless an infinite number of tests are carried out - I will put the kettle on. <smile>
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

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

Post by jj2007 »

deltarho[1859] wrote:One hundred positives do not imply that a negative cannot exist.
Sure. But Windows cannot tolerate errors here - that would be bugs indeed. So, let's wait for the proof that Windows fails by those who claim that it fails...?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Post by dodicat »

deltarho[]
So you have jj2007's masm installed on Win 10?

jj2007
If you could provide a .zip for your masm then I am sure many win 10 users here would try it out.

But an installer and potential windows warnings I fear put many off.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

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

Post by deltarho[1859] »

dodicat wrote:deltarho[]
So you have jj2007's masm installed on Win 10?
No.

Of course, newbieforever may be assuming a failure is because the new folder is not available when, in fact, the failure has nothing to do with a new folder. It won't be the first time that someone asks for a 'fact' to be solved and the fact subsequently turns out to be an assumption and the assumption was wrong leading everyone up a blind alley. "So, let's wait for the proof that Windows fails by those who claim that it fails...?". Good idea. <smile>
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

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

Post by jj2007 »

dodicat wrote:jj2007
If you could provide a .zip for your masm then I am sure many win 10 users here would try it out.

But an installer and potential windows warnings I fear put many off.
The file attached to the Create a hundred folders post is a zip archive containing the exe. Extract it to a tmp folder and run it from a DOS prompt; but warning, it does create 100 folders with 100 files in them - you better know how do delete them ;-)

The *.asc file in the archive is the source in RTF format. You can open it in WordPad to see which Windows API calls I used (towards the end. MakeDir macro).

For installing Masm32 & MasmBasic, see Masm32: What you absolutely need to get started. Re "installer and potential windows warnings": See my post count over there. It's a question of trust; most assembler projects trigger some warnings or get quarantined etc, simply because the AV heuristic scanners "detect" that the code is different from what they expect. For example, I have almost no CRT calls in the whole library - very suspicious!

And in any case, I don't want to make publicity for Masm & friends in a FreeBasic forum. It's an entirely different baby, although sometimes knowledge of assembler may help to understand better the quirks of FreeBasic.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Post by dodicat »

Don't you delete them for us?"

Have you 100 files in each folder?

Here I put only one file in each folder.
Enough I think.


Please put the code into it's own folder to run (incase of a hiccup)

Code: Select all

  

 

  
#include "crt.bi"

declare function wsystem_ cdecl alias "_wsystem" ( as wstring ptr) as long 'not in crt.bi

function MakeFolder(folder as wstring) as long
  return _wmkdir(@folder)
end function

function RemoveFolder(folder as wstring) as long
     return _wrmdir(@folder)
    end function
    
function savefile(filename as wstring,txt as wstring="" ) as long
dim as wstring * 20 r="w,ccs=UNICODE"
 dim as file ptr fp=_wfopen(@filename,@r)
   fputws(@txt,fp)
   function=iif(*cast(long ptr,fp),0,-1)
   fclose(fp)
end function

function removefile(filename as wstring) as long
    return _wremove(@filename)
    end function

Dim As Wstring * 50 folder,nextfolder

 folder="Folder"
 dim as double t=timer
for n as long=1 to 100
    locate 5,,0
    print n;" of 100"
  nextfolder = folder+ "  " +right("0000" +str(n),4) 

if MakeFolder(nextfolder)<>0 then print "Failed to make  ";folder
if savefile(nextfolder + "/new file.txt","HELLO is written inside this text file")<>0 then print "Failed to save file  "
next

wsystem_("dir/B ")

print "Time taken for creation ";timer-t
print "press a key"
sleep
t=timer
for n as long=1 to 100
    nextfolder = folder+ "  " +right("0000" +str(n),4) 
    if removefile(nextfolder + "/new file.txt") <>0 then print "Failed to remove text" 'remove the file first
    if RemoveFolder(nextfolder)<>0 then print "Failed to remove  ";folder
next

print "Time taken for deletion ";timer-t

print "done"
wsystem_("dir/B ") 


sleep   
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

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

Post by jj2007 »

dodicat wrote:Don't you delete them for us?
No.
Have you 100 files in each folder?
No, just one.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

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

Post by deltarho[1859] »

@newbieforever

Bit late to be asking this, but
SaveFile() applied immediately after creating a folder fails, there seems to be a time problem.
What is SaveFile()? It is not a FreeBASIC keyword and it is not a Windows API.

Whatever it is, how is it failing?
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

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

Post by newbieforever »

deltarho[1859]:
Sorry, I am unable at the moment to follow this discussion...

SaveFile() (and the structure of my code):

Code: Select all

#Define unicode  
#Include once "windows.bi"
#Include once "win/shellapi.bi"
' ==================================================
Declare Sub SaveFile(file As Wstring, o_p As String)
'...
If Len(fold) Then CreateDirectoryW(fold, 0)
Sleep 100    
'...   just a simple string manipulation here...
SaveFile(fold + laun, lau)
' ==================================================
Sub SaveFile(file As Wstring, o_p As String)
  Dim As Any ptr   n, pstr
  Dim As Integer   byteswritten
  n = CreateFileW(file, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0)
  If n <> -1 Then
    WriteFile(n, Peek(any ptr, VarPtr(o_p)), Len(o_p), @byteswritten, 0)
    CloseHandle(n)
  End If
End Sub
A subfolder is created by CreateDirectoryW(). In this subfolder a file should be written by SaveFile(). Without Sleep no file is created in the subfolder. That's all.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

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

Post by deltarho[1859] »

As I suspected, we have been trying to answer a question that has nothing to do with the problem and the question was based upon an erroneous assumption. We then get code where variables are not declared.

Sorry, but I have lost interest.
Post Reply