A couple of days ago I needed a procedure to create a whole directory tree (like C:\dir1\dir2\dir3\) and I found Ultrano's BuildPath function (http://www.asmcommunity.net/board/index.php?topic=9030) and I have used it until today when I got an idea of how to do it in a much easier way. My version scans the path for the \-character and if found, it replaces it by a 0h to null-terminate it. Then it calls CreateDirectory and then changes it back to a \. After that it repeats those steps until it reaches the end of the string. This way no extra stack-space is needed.

BuildPath PROC lpDir:PTR DWORD

mov EDI,lpDir
cmp b[EDI],5Ch ;Skip \ if it's the first character
jz Foo
cmp b[EDI+1],':' ;Skip X:\
jnz Again
add EDI,3 ;

movzx ESI,b[EDI]

.if ESI==0 || ESI==5Ch

mov b[EDI],0 ;Nullterminate where \ is found
invoke CreateDirectory,lpDir,NULL
invoke GetFileAttributes,lpDir
mov b[EDI],5Ch

inc eax ;Does the current directory exist?
or eax,eax
jnz @F
jmp Exit ;Error: Return 0 and exit

or ESI,ESI ;End of path? :-P
jnz @F
mov b[EDI],0 ;Put the 0h back at the end
mov eax,1
jmp Exit ;Success: Return 1 and exit


inc EDI
jmp Again

BuildPath ENDP

I've included a RadASM example of it in action.
Posted on 2003-02-20 05:48:58 by Delight
Posted on 2003-02-22 08:11:55 by Ultrano
DbgHelp.lib has a function to do this - MakeSureDirectoryPathExists
Posted on 2003-02-22 09:46:52 by gfalen

or eax,eax
jnz @F
jmp Exit;Error: Return 0 and exit

Strange code :)
Replace it with:

or eax,eax
jz Exit
Posted on 2003-02-28 19:01:23 by The Svin