Solved

Create Folder and File using API

Posted on 2002-06-04
21
681 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now