?
Solved

How do I loop through a folder to see if another folder exists?

Posted on 2005-03-30
11
Medium Priority
?
920 Views
Last Modified: 2012-06-21
Hi

This is an Access 97 question.

I have a simple piece of code that adds a new folder and some sub folders within the main folder called: SENB. A textbox called txtUser will be added to the SENB folder. However if that folder already exists then the process should abort. I do not know how to read the SENB folder to see if a folder matching txtUser exists. Please help.

Thanks

PB

Here is what I have:

Private Sub cboUser_Click()

Beep
If MsgBox("You are about to create a new user account called '" & Me!txtUser & "'. Are you sure?", vbInformation + vbYesNo) = vbNo Then
    Beep
    MsgBox "Process aborted by user.", vbInformation
    Exit Sub
End If

'I need code here to loop through SENB directory to see if directory exists with same name as txtUser. If it does warn user and exit sub.

MkDir ("C:\SENB\USERS\" & Me!txtUser)  ' Make new directory or folder.
MkDir ("C:\SENB\USERS\" & Me!txtUser & "\proc")
MkDir ("C:\SENB\USERS\" & Me!txtUser & "\unproc")
MkDir ("C:\SENB\USERS\" & Me!txtUser & "\added")
End Sub
0
Comment
Question by:PBLack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 13661490
try this

if Len(Dir("C:\SENB\USERS\" & Me!txtUser)=0 then
   MkDir ("C:\SENB\USERS\" & Me!txtUser)  ' Make new directory or folder.
   MkDir ("C:\SENB\USERS\" & Me!txtUser & "\proc")
   MkDir ("C:\SENB\USERS\" & Me!txtUser & "\unproc")
   MkDir ("C:\SENB\USERS\" & Me!txtUser & "\added")
else
   MsgBox "Directory exist"
   exit sub

end if

end sub
0
 

Author Comment

by:PBLack
ID: 13661527
Hi

Gives me a runtime error 75
Path/File access error.

Occurs when the folder does exist. First time through it works.

FYI there was a bracket missing in your if statement.
0
 

Author Comment

by:PBLack
ID: 13661545
I changed this if Len(Dir("C:\SENB\USERS\" & Me!txtUser)=0 then to if Len(Dir("C:\SENB\USERS\" & Me!txtUser))=0 then

And this causes the error
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.

 
LVL 4

Expert Comment

by:McDougall
ID: 13661546
Here is a function which you can pass a path to and get a true or false:

test like this:
msgbox CheckForFolder("c:\figlet")

Function CheckForFolder(path)

    On Error Resume Next

    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")

    Set f = fso.GetFolder(path)

    If Err Then
        CheckForFolder = False
    Else
        CheckForFolder = True
    End If

    Set fso = Nothing
    Set f = Nothing

End Function
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 13661590
Try this:

If Dir("C:\SENB\USERS" & Me!txtUser,vbDirectory)="" Then
   'Folder does not exist
Else
   'Folder does exist
End If
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 2000 total points
ID: 13661592
Oops, missed a \:
Try this:

If Dir("C:\SENB\USERS\" & Me!txtUser,vbDirectory)="" Then
   'Folder does not exist
Else
   'Folder does exist
End If
0
 
LVL 54

Expert Comment

by:nico5038
ID: 13661614
Try to use:

if Len(NZ(Dir("C:\SENB\USERS\" & Me!txtUser))) > 0 then
   MsgBox "Directory exist"
   exit sub
else
   MkDir ("C:\SENB\USERS\" & Me!txtUser)  ' Make new directory or folder.
   MkDir ("C:\SENB\USERS\" & Me!txtUser & "\proc")
   MkDir ("C:\SENB\USERS\" & Me!txtUser & "\unproc")
   MkDir ("C:\SENB\USERS\" & Me!txtUser & "\added")
end if

Nic;o)
0
 
LVL 12

Expert Comment

by:Paurths
ID: 13661703
here's an API function

you can use it like this, assuming a folderpath is stored in the variable strPath (or another variable as you choose...)

If FolderExists(strPath) then
   'ok it exists
else
   'bugger, this folder does not yet exists...
end if


there are 2 other functions also, to check if a file exists or a drive exists

Copy it all to a new module

'******************************

'*********** Check to see if a drive exists **************
Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
   Alias "GetLogicalDriveStringsA" _
  (ByVal nBufferLength As Long, _
   ByVal lpBuffer As String) As Long
'******** END CHECK IF DRIVE EXISTS *********************


'*********** Check to see if a file exists **************
Public Const INVALID_HANDLE_VALUE = -1
Public Const MAX_PATH = 260

Public Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
   dwFileAttributes As Long
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As Long
   nFileSizeLow As Long
   dwReserved0 As Long
   dwReserved1 As Long
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type

Public Declare Function FindFirstFile Lib "kernel32" _
   Alias "FindFirstFileA" _
  (ByVal lpFileName As String, _
   lpFindFileData As WIN32_FIND_DATA) As Long

Public Declare Function FindClose Lib "kernel32" _
  (ByVal hFindFile As Long) As Long
 
  '******** END CHECK IF FILE EXISTS *********************
 
 
'*********** Check to see if a folder exists *************
Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10


'***************** END CHECK IF FOLDER EXISTS ************
 


'*********** Check to see if a drive exists **************
Public Function DriveExists(SDrive As String) As Boolean

   Dim tmp As String
   Dim nBuffersize As Long
   
  'Call the API with a buffer size of 0.
  'The call fails, and the required size
  'is returned as the result.
   nBuffersize = GetLogicalDriveStrings(0&, tmp)

  'pad a string to hold the results
   tmp = Space$(nBuffersize)
   nBuffersize = Len(tmp)
   
  'and call again
   If GetLogicalDriveStrings(nBuffersize, tmp) Then
   
     'if the drive letter passed is in
     'the returned logical drive string,
     'return True. Use vbTextCompare for
     'a case-insensitive match (remembering
     'that when a compare method is specified,
     'the start position must also be specified.
      DriveExists = InStr(1, tmp, SDrive, vbTextCompare)

   End If
   
End Function
'******** END CHECK IF DRIVE EXISTS *********************


'*********** Check to see if a file exists **************
Public Function FileExists(sSource As String) As Boolean

   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
   
   hFile = FindFirstFile(sSource, WFD)
   FileExists = hFile <> INVALID_HANDLE_VALUE
   
   Call FindClose(hFile)
   
End Function

'******** END CHECK IF FILE EXISTS *********************


'*********** Check to see if a folder exists **************
Public Function FolderExists(sFolder As String) As Boolean

   Dim hFile As Long
   Dim WFD As WIN32_FIND_DATA
   
  'remove training slash before verifying
   sFolder = UnQualifyPath(sFolder)

  'call the API pasing the folder
   hFile = FindFirstFile(sFolder, WFD)
   
  'if a valid file handle was returned,
  'and the directory attribute is set
  'the folder exists
   FolderExists = (hFile <> INVALID_HANDLE_VALUE) And _
                  (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY)
   
  'clean up
   Call FindClose(hFile)
   
End Function


Private Function UnQualifyPath(ByVal sFolder As String) As String

  'trim and remove any trailing slash
   sFolder = Trim$(sFolder)
   
   If Right$(sFolder, 1) = "\" Then
         UnQualifyPath = Left$(sFolder, Len(sFolder) - 1)
   Else: UnQualifyPath = sFolder
   End If
   
End Function
'******** END CHECK IF FOLDER EXISTS *********************
0
 
LVL 12

Expert Comment

by:Paurths
ID: 13661739
btw, hi Nico! ;-)
0
 
LVL 54

Expert Comment

by:nico5038
ID: 13664794
<off topic>
Hi Paurths, long time no see :-)
BTW, already subscribed to: http://www.experts-exchange.com/Databases/MS_Access/Q_20917681.html

Nic;o)
0
 
LVL 12

Expert Comment

by:Paurths
ID: 13667082
<way off topic>
idd, its been quite a few years now....

will subscribe (i think, that is, if i stay long enough...)

</way off topic>

3 626 495 points????
hello???

;-)

G's
Ricky
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

800 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