Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating directories within the program 4

Status
Not open for further replies.

draugen

Programmer
Nov 24, 2004
48
NO
I am stomped! is there a way to create a directory using code?
 
Yes, example code

My.Computer.FileSystem.CreateDirectory _
("C:\Documents and Settings\All Users\Documents\NewDirectory")

 
Or use,
MkDir "C:\Data\MyDirectory"

Notes, the code will bomb If MyDirectory already exists and If "Data" directory itself does not exist.

 
Or the quick and dirty method that I use:

On Error Resume Next
MkDir "C:\MyData"
MkDir "C:\MyData\My Directory"

I don't think you can create a directory called "C:\Data". I get Error 75 - Path/File access error.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Er...heather, that's a .Net solution. Error7, you get that error if you try to make a directory that already exists, not because there's a rule against making a directory called "C:\data". See VbSun's post. I'm sure you can verify this for yourself.

Draugen, you can also use the FileSystemObject. There are lots of posts on this.

HTH

Bob
 
Bob

I did verify this before posting. On my office W2k machine, the directory definately didn't and still doesn't exist. However, I just tried it at home on XP and I created the directory from within VB.

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Heather code is good. You can use the File System Object ato see if the directory does exist and then appropriate action of either creating or not re-creating the directory.

Thanks
Engi
 
<On my office W2k machine, the directory definately didn't and still doesn't exist.

Ok, then there's a reason for it I didn't think of. Perhaps in your office your SE has disallowed the creation of that directory. Easy way to find that out is to try to create it from a command line: md c:\temp.

Bob
 
On my office W2k machine, the directory definately didn't and still doesn't exist.

That was on the 14th. I go into the office this morning, double check to make sure it doesn't exist, try MkDir "C:\Data" and it created it ???

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thank you all,I read all of your comment, but desided on usig vbSun's solution - together with error7'saddition, so I could avoid a untimely crash of the program. I also checked out BobRodes FileSystemObjects, but didn't find anything pertaning to the project I am working on, But some that I can use on future projects.
Again Thanks. The stars go to vbSun, and error7 for providing me with a working solution for my dilemma, and to BobRodes for providing me with future solution to future projects.
 
>>Heather code is good
Just to be clear, this is the VB 5 & 6 forum. That code will not work in VB 5 or 6. Just want to make that distinction for anyone else who wanders into this thread and tries to use that code in VB6.

 
> ... checked out BobRodes FileSystemObjects, but didn't find anything pertaning to the project ...

On Error Resume Next
CreateObject("scripting.filesystemobject").CreateFolder "C:\Data\MyDirectory"

Additionally there's a pretty similar function in the Microsoft Shell Controls And Automation library, the main difference really being that it doesn't require the exception handling

Dim myShell As Object
Set myShell = CreateObject("Shell.Application")
myShell.NameSpace("c:\").NewFolder "C:\Data\MyDirectory"

And then there's an API call which will create intermediate folders for you if they are missing (and again doesn't require the exception handling):

Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" (ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long

Private Sub Command1_Click()
SHCreateDirectoryEx Me.hwnd, "C:\fso\data\example", ByVal 0&
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top