Click to See Complete Forum and Search --> : How to create a Directory
joseluisbz
July 3rd, 2001, 01:10 PM
Hello Everyone
I need to create a directory, verify if exist first... Later to erase someone other directory...
Please help me...
joseluisbz@bigfoot.com
Ying8292
July 3rd, 2001, 01:28 PM
http://www.codeguru.com/cgi-bin/bbs/wt/showpost.pl?Board=vb&Number=47240&page=&view=&sb=
Iouri
July 3rd, 2001, 01:32 PM
' this code will create the directory 'c:\Testing\Hello\How\Are\You\'
Public Sub rMkDir(ByVal mdir As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.GetParentFolderName(mdir) <> "" Then rMkDir FSO.GetParentFolderName(mdir)
On Local Error Resume Next
MkDir mdir
End Sub
Private Sub Form_Load()
rMkDir "c:\Testing\Hello\How\Are\You\"
End Sub
'===another example===============================================================
It uses the CreateDirectory API and the Dir() function to quickly create all the directories in the path.
Declarations
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Declare Function CreateDirectory Lib "kernel32" _
Alias "CreateDirectoryA" (ByVal lpPathName As String, _
lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Code
Public Sub CreateNewDirectory(NewDirectory As String)
Dim sDirTest As String
Dim SecAttrib As SECURITY_ATTRIBUTES
Dim bSuccess As Boolean
Dim sPath As String
Dim iCounter As Integer
Dim sTempDir As String
iFlag = 0
sPath = NewDirectory
If Right(sPath, Len(sPath)) <> "\" Then
sPath = sPath & "\"
End If
iCounter = 1
Do Until InStr(iCounter, sPath, "\") = 0
iCounter = InStr(iCounter, sPath, "\")
sTempDir = Left(sPath, iCounter)
sDirTest = Dir(sTempDir)
iCounter = iCounter + 1
'create directory
SecAttrib.lpSecurityDescriptor = &O0
SecAttrib.bInheritHandle = False
SecAttrib.nLength = Len(SecAttrib)
bSuccess = CreateDirectory(sTempDir, SecAttrib)
Loop
End Sub
Use
Call CreateNewDirectory("c:\test\directory\vb\tips\")
Iouri Boutchkine
iouri@hotsheet.com
Iouri
July 3rd, 2001, 01:33 PM
' Delete this directory and all the files it contains.
Private Sub DeleteDirectory(ByVal dir_name As String)
Dim file_name As String
Dim files As Collection
Dim i As Integer
' Get a list of files it contains.
Set files = New Collection
file_name = Dir$(dir_name & "\*.*", vbReadOnly + vbHidden + vbSystem + vbDirectory)
Do While Len(file_name) > 0
If (file_name <> "..") And (file_name <> ".") Then
files.Add dir_name & "\" & file_name
End If
file_name = Dir$()
Loop
' Delete the files.
For i = 1 To files.Count
file_name = files(i)
' See if it is a directory.
If GetAttr(file_name) And vbDirectory Then
' It is a directory. Delete it.
DeleteDirectory file_name
Else
' It's a file. Delete it.
lblStatus.Caption = file_name
lblStatus.Refresh
SetAttr file_name, vbNormal
Kill file_name
End If
Next i
' The directory is now empty. Delete it.
lblStatus.Caption = dir_name
lblStatus.Refresh
RmDir dir_name
End Sub
Private Sub cmdDelete_Click()
DeleteDirectory txtDir.Text
lblStatus.Caption = "Files deleted."
End Sub
Private Sub Form_Load()
Dim txt As String
txt = App.Path
If Right$(txt, 1) <> "\" Then txt = txt & "\"
txt = txt & "Target"
txtDir.Text = txt
End Sub
Iouri Boutchkine
iouri@hotsheet.com
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.