Solved

Create Folder and File using API

Posted on 2002-06-04
21
687 Views
Last Modified: 2012-05-04
Hi,
I would please like a clear explanation, with an example, of the process for creating a folder and creating a file using only the API. I hope this doesn't sound rude but I am not interested in using the file system object. I have been scouring the internet for about a day without any luck, most examples including one I found here and another at MSDN have syntax errors. I understand that the CreateFile method cannot creat a folder.Thank you for your time and have a great day programming,
D
0
Comment
Question by:dingo11
  • 9
  • 6
  • 2
  • +2
21 Comments
 
LVL 5

Expert Comment

by:rpai
ID: 7054080
Could you use the CreateDirectory and CreateFile  Win32API's ?

Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
0
 
LVL 5

Expert Comment

by:rpai
ID: 7054086
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7054156
>>I am not interested in using the file system object

And rightfully so...it adds much unwanted bulk to your project size for distribution.  However, what do you have against the native VB commands for performing these tasks?


Open strFile FOr Output As #lngFileNum
Close #lngFileNum
    -> This will create a file if it does not yet exist


MkDir strPath
    -> This will create a folder
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:dingo11
ID: 7054213
r,
I played with those examples but I am doing something wrong.
=============FORM=======
Private Sub CmdCreateFolder_Click()
Dim retval As Long  ' return value
Dim secattr As SECURITY_ATTRIBUTES
'
'    ' Set the desired security attributes
    secattr.nLength = Len(secattr)  ' size of the structure
    secattr.lpSecurityDescriptor = 0  ' default (normal) level of security
    secattr.bInheritHandle = 1  ' this is the default setting
'
 ' this is the default setting

    ' Create the directory.
    Dim path As String
    path = "C:\Dummy\NewDir"

    retval = CreateDirectory(path, secattr)

End Sub
--------------MOD---------
Public Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As _
String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

Public Type SECURITY_ATTRIBUTES
  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Boolean
End Type
0
 

Author Comment

by:dingo11
ID: 7054214
azra,
Nice to see you are still around, Mkdir generates error 76 path not found
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7054218
I'm with AzraSound except that I would check for the file's existence before creating:

if Dir$(strFile) = vbNullString then
  Open strFile FOr Output As #lngFileNum
  Close #lngFileNum
end if


Once reason that the intrinsic functions and FSO exist in VB is so that you can have the same functions as the API but without the inherent dangers associated with passing a bad value to an API function.  But in the case of FSO, you get very little relative to the overhead, so I always use the intrinsic functions.
0
 

Author Comment

by:dingo11
ID: 7054238
a,
MS says to Respecify the path to resolve the error.??
d
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7054261
I am with Azra too. I am 99% sure that you are trying to create more than 1 folder at the same time.
If yoy try to create otherfolder inside
mkdir c:\dummy\otherfolder
and c:\dummy doesn't exist you get the error.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7054298
MkDir "C:\Dummy\NewDir"

will have to be broken into

Mkdir "C:\Dummy"
MkDir "C:\Dummy\NewDir"

Because C:\Dummy does not exist until you create it.

--
This might be best served by a new routine:

...
  MakeDirectory "C:\Dummy\NewDir"
...

Private Sub MakeDirectory(PathName As String)
  Dim strSubPath As String
  Dim iSubPathPosition As Integer

  Const cPathDelimiter As String = "\"

  ' Get the first delimiter
  iSubPathPosition = InStr(PathName, cPathDelimiter)

  ' Search for subsequent delimiters; when not found, all subfolders were created
  Do While iSubPathPosition > 0
    iSubPathPosition = InStr(iSubPathPosition + 1, PathName, cPathDelimiter)
    If iSubPathPosition > 0 Then
      MkDir Left$(PathName, iSubPathPosition - 1)
    End If
  Loop

  MkDir PathName
End Sub
0
 

Author Comment

by:dingo11
ID: 7054308
Richie,
I was about to flatten the laptop, if I used logic instead of anger I would have realized that little nugget. Thank you!
d
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7054371
"...
if I used logic instead of anger I would have realized that little
nugget..."
Sorry, i didn't understand. Mine was just only for clarification.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7054385
Richie...I think it was a compliment.  I guess that dingo's comment roughly translates into a Homer Simpson "D'Oh!" :)

0
 

Author Comment

by:dingo11
ID: 7054395
richie,
It was a compliment. I was not thinking about the problem in a logical manner.
d
0
 

Author Comment

by:dingo11
ID: 7054475
Hey,
The folder part is working perfectly both via the api and mkdir. Thank you! The createfile part is not.
So...

hFile = CreateFile("C:\Dummy\test.txt", GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, 0)
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7054537
Make a new file:

dim strCommand as string
strCommand = Environ("ComSpec")
Shell strCommand & " /c copy nul " & Filename

' Filename is a variable with a valid filename.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 101 total points
ID: 7054546
Oh...filename can also be a full path:

Shell "cmd /c copy nul C:\Dummy\somefile.txt"

The above will work on NT/2000 machines; for 95/98/ME:

Shell "command /c copy nul C:\Dummy\somefile.txt"

...
or as I showed above, use Environ("ComSpec") to get the local machine's command.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7054574
CreateFile method using native VB:


Sub CreateFile(ByVal FilePath As String)
    Dim lngFileNum As Long

    lngFileNum = FreeFile
    Open FilePath For Output As #lngFileNum
    Close #lngFileNum
End Sub


As mentioned before, you can also check for existence of the file before creating if you feel it necessary for your requirements.
0
 

Author Comment

by:dingo11
ID: 7054582
r,
Thanks.    

The following api function is working

hFile = CreateFile("C:\Dummy\test6.txt", GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), 1, FILE_ATTRIBUTE_TEMPORARY, 0)
0
 

Author Comment

by:dingo11
ID: 7054701
It was a tough call, everyone helped out alot. But I decided on rspahitz.
d
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7054735
Thanks for the A-grade, and I appreciate your accepting my comment.  However, you really asked two questions and I think someone else deserves half the points for the Make-Direct portion.  (Of course, if you want to give the other half to me for my splendid subroutine, then I guess that's okay :)
0
 

Author Comment

by:dingo11
ID: 7055013
r,
I am goning to tinker with that sub to get it to work,
d
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 57
Microsoft Access combo box help 2 47
How to measure sizes and angles in scanned images ? 3 65
MS Access 03, TransferText, decimal places 8 72
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question