Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create Folder and File using API

Posted on 2002-06-04
21
Medium Priority
?
714 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 404 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses

971 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