Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 925
  • Last Modified:

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

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
PBLack
Asked:
PBLack
  • 3
  • 2
  • 2
  • +3
1 Solution
 
Rey Obrero (Capricorn1)Commented:
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
 
PBLackAuthor Commented:
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
 
PBLackAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
McDougallCommented:
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
 
shanesuebsahakarnCommented:
Try this:

If Dir("C:\SENB\USERS" & Me!txtUser,vbDirectory)="" Then
   'Folder does not exist
Else
   'Folder does exist
End If
0
 
shanesuebsahakarnCommented:
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
 
nico5038Commented:
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
 
PaurthsCommented:
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
 
PaurthsCommented:
btw, hi Nico! ;-)
0
 
nico5038Commented:
<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
 
PaurthsCommented:
<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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now