Click to See Complete Forum and Search --> : directory


vin
September 11th, 2001, 04:43 AM
OK, here is an easy question:
How can I create a directory using Visual Basic.

Thanks

Valery Iskarov Nikolov
Software Dynamics

Cimperiali
September 11th, 2001, 04:51 AM
if dir("c:\newDir", vbdirectory)= "" then
mkdir "c:\newDir"
end if

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

Cimperiali
September 11th, 2001, 04:53 AM
'Another way:
'In general section
private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (byval lpPathName as string, lpSecurityAttributes as SECURITY_ATTRIBUTES) as Long
private Type SECURITY_ATTRIBUTES
nLength as Long
lpSecurityDescriptor as Long
bInheritHandle as Long
End Type

private Sub Command1_Click()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim Security as SECURITY_ATTRIBUTES
'Create a directory
Ret& = CreateDirectory("C:\Directory", Security)
'If CreateDirectory returns 0, the function has failed
If Ret& = 0 then MsgBox "error : Couldn't create directory !", vbCritical + vbOKOnly
End Sub





Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

vin
September 11th, 2001, 04:53 AM
10X

Valery Iskarov Nikolov
Software Dynamics

Iouri
September 11th, 2001, 10:24 AM
using FSO

' Inputs:sPath: Fully-qualified absolute or relative path you wish to create.
'Example 1: \\NetworkVolume\NetworkShare\ExistingDir\NewDir\NewSubdir\NewSubDir\
'Example 2: C:\Program Files\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z

'Side Effects:Number 70, Permission Denied error will occur is write access or directory create access is
' not allowed for the drive for that user.

'Reference "Microsoft Scripting Runtime" (scrrun.dll) available at http://www.microsoft.com/scripting or with VB6.

Public Sub CreateFolders(ByVal sPath As String)
Dim oFileSystem As New Scripting.FileSystemObject
'or late-bind with:
'Dim oFileSystem As Object
'Set oFileSystem = CreateObject("Scripting.FileSystemObject")

On Error Goto ErrorHandler
With oFileSystem
' Is this drive valid and ready?
If .DriveExists(.GetDriveName(sPath)) Then
' Is this folder not yet valid?
If Not .FolderExists(sPath) Then
' Recurse back in to this method until a parent folder is valid.
CreateFolders .GetParentFolderName(sPath)
' Create only a nonexistant folder before exiting the method.
.CreateFolder sPath
End If
End If
End With

Set oFileSystem = Nothing
ExitMethod:
Exit Sub
ErrorHandler:
App.LogEvent "CreateFolders Error in " & Err.Source & _
": Could not create " & sPath & ".", vbLogEventTypeInformation
End Sub




Iouri Boutchkine
iouri@hotsheet.com

vin
September 11th, 2001, 10:48 AM
10x

Valery Iskarov Nikolov
Software Dynamics

John G Duffy
September 11th, 2001, 12:11 PM
Here is the simplest and most powerful way I found to create a Directory and a Directory tree. You do not need to worry about whether the parent exists or not. One statewment will create a multi-level folder tree if that is what you need.
The little sample creates a directory tree 5 levels deep.
[vbcode]
Option Explicit

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'create the directory "c:\this\is\a\test\directory\", if it doesn't exist already
MakeSureDirectoryPathExists "c:\this\is\a\test\directory\"
End Sub

[/vbcode

John G