UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Files And Folders, Office 2003    
 
   
ConorS
post Feb 27 2012, 05:20 AM
Post #1

UtterAccess Ruler
Posts: 1,265
From: Northern Ireland (Newry)



Hi

I have 3 questions regarding an access system looking into folders.

My access system, has a table called jobs, and jobs can have associated files and folder structures associated with them.

I'm going to simplify these examples down.

1. Does a file exist at a certain location. For example: c:\Jobs\0001\Test01.txt
How do you write this in vb? I'm looking it to return a boolean value.

2. List the file contents of a certain folder. For example:
Inside the following folder:

c:\Jobs\0001\

I have 3 files:

Test01.pdf
Test02.txt
Test03.xls

In vb, how do you get the code to list the following 3 file names?

3. List the folder contents of a folder. For example:
Inside the following folder:

c:\Jobs\

I have x amount of folders:

0001
0002
0003
0004
0005
...

In vb, how do you get the code to list the following folder names?

regards

Conor

Go to the top of the page
 
+
Alan_G
post Feb 27 2012, 05:27 AM
Post #2

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,880
From: Devon UK



Hi

First question is relatively easy -

CODE
Dim blnFileExists As Boolean

blnFileExists = Len(Dir("c:\Jobs\0001\Test01.txt")) > 0


For your other two, I normally use some recursive function calls. I'm literally just 'leaving the building' so I'm sure someone will have answered by the time I get back, but if not I'll post an example or two for you
Go to the top of the page
 
+
jleach
post Feb 27 2012, 06:07 AM
Post #3

UtterAccess Editor
Posts: 6,709
From: Capital District, NY, USA



Hi,

Dir() can do this for you. A lot of people prefer FSO (FileSystemObject) instead, though I personally like Dir() better, albeit a little tricky to use at times.

Have a look at the GetDirContents function in the Wiki Function Library. It'll return various information on a given path, some of it you don't require, but it'll do what you need to also.

After importing the code and creating the DIRECTORYCONTENTS type, you can use something like this:

CODE
Dim dc As DIRECTORYCONTENTS
dc = GetDirContents("C:\Jobs\0001")

'delimited list of files in the directory
Debug.Print dc.dcFiles

dc = GetDirContents("C:\Jobs")

'delimited list of folders in the directory
Debug.Print dc.dcDirectories


The Split() function can then be used to break apart the delimited lists into individual array entries that you can work with.

If nothing else, you can see the loop logic in the function library entry, and built your own. If you are using the function provided and have trouble setting it up to work correctly (it's a bit tricky) let me know.

hth
Go to the top of the page
 
+
ConorS
post Feb 27 2012, 06:24 AM
Post #4

UtterAccess Ruler
Posts: 1,265
From: Northern Ireland (Newry)



"After importing the code and creating the DIRECTORYCONTENTS type, you can use something like this:"

How do you create the DIRECTORYCONTENTS type?

Because the code which i copied and pasted over is not compiling for me.

Go to the top of the page
 
+
jleach
post Feb 27 2012, 06:37 AM
Post #5

UtterAccess Editor
Posts: 6,709
From: Capital District, NY, USA



The notes in the function aren't the clearest I guess. Here's what you need to do:

CODE
Option Compare Database
Option Explicit

Public Type DIRECTORYCONTENTS
  dcCount As Long             'number of files and directories
  dcDirectory As Long         'number of directories
  dcFiles As String           'delimited list of files
  dcDirectories As String     'delimited list of directories
  dcReadOnly As Long          'number of read only files
  dcHidden As Long            'number of hidden files
  dcSystem As Long            'number of system files
  dcArchive As Long           'number of files ready for archiving
End Type


' Code courtesy of UtterAccess Wiki
' http://www.UtterAccess.com/wiki/index.php/Category:FunctionLibrary
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
'============================================================================
==
' NAME: GetDirContents
'
' PURPOSE: Retrieves lists and information about contents of a directory
'
' RETURNS: DIRECTORYCONTENTS structure
'          This procedure does not raise an error on an invalid
'          directory parameter
'
' ARGUMENTS: sDirectory - Directory to evaluate
'            Delimiter (Optional, default ";") - list delimiter for return
'
'
'
' DEPENDANCIES:
'
'  The Split() and Replace() functions are required for this procedure.  If
'  you are using Access 97 or earlier you will need custom versions of these
'  for this procedure to work.
'
'  This structure is required in the declarations section of a standard module
'  ----------
'  Public Type DIRECTORYCONTENTS
'    dcCount As Long             'number of files and directories
'    dcDirectory As Long         'number of directories
'    dcFiles As String           'delimited list of files
'    dcDirectories As String     'delimited list of directories
'    dcReadOnly As Long          'number of read only files
'    dcHidden As Long            'number of hidden files
'    dcSystem As Long            'number of system files
'    dcArchive As Long           'number of files ready for archiving
'  End Type
'  ----------
'
'  This function includes a call to QSortInPlace.  Because directory contents
'  returned from the Dir() function are not sorted, this is used to return
'  a list that is sorted.
'
'  The QSortInPlace function is property of Chip Pearson and can be found at:
'     http://www.cpearson.com/excel/SortingArrays.aspx
'
'  The QSortInPlace call can be commented out with no consequence to the return
'  of the function other than having the lists of files and directories sorted
'  by numerically and alphabetically
'
'
'
' EXAMPLE USAGE:
'  ----------
'  Sub PrintDirInfo(sDir As String)
'    Dim dc As DIRECTORYCONTENTS
'    dc = GetDirContents(sDir)
'    With dc
'      Debug.Print "Directory List: " .dcDirectories & vbCrLf
'      Debug.Print "File List: " & .dcFiles & vbCrLf
'      Debug.Print "Item Count: " & .dcCount & vbCrLf
'      Debug.Print "Number of Directories: " & .dcDirectory & vbCrLf
'      Debug.Print "Number of Read Only: " & .dcReadOnly & vbCrLf
'      Debug.Print "Number of Hidden Files :" & .dcHidden & vbCrLf
'      Debug.Print "Number of System Files: " & .dcSystem & vbCrLf
'      Debug.Print "Number of Archive Ready Files: " & .dcArchive & vbCrLf
'    End With
'  End Sub
'  ----------
'
'
' REVISIONS:
'  REV |    DATE    | REV TYPE | DESCRIPTION
'------------------------------------------------------------------------------
'  R01   2010/09/30    INITIAL
'
'
'============================================================================
==
'ErrHandler V3.01
Public Function GetDirContents( _
   sDirectory As String, _
   Optional Delimiter As String = ";" _
   ) As DIRECTORYCONTENTS
On Error GoTo Error_Proc
Dim Ret As DIRECTORYCONTENTS
'=========================
Dim v As Variant  'variant array to hold returns
Dim s As String   'string to hold returns
Dim sT As String  'temp file placeholder
Dim l As Long     'counter/loop iterator
Dim lAttr As Long 'attributes placeholder
'=========================

If Right(sDirectory, 1) <> "\" Then sDirectory = sDirectory & "\"

'get the complete list
sT = Dir(sDirectory, 55)
While sT <> ""
   s = s & ";" & sT
   sT = Dir()
Wend

If s = "" Then GoTo Exit_Proc

'remove leading delimiter
s = Right(s, Len(s) - 1)

'remove the "." and ".."
s = Replace(s, ".;", "", 1, 1)
s = Replace(s, "..;", "", 1, 1)

'split into the array
v = Split(s, ";")

'sort the array
' QSortInPlace v


'start building the return structure

'get the total count
Ret.dcCount = UBound(v) + 1

For l = 0 To UBound(v)
   'get the attributes of the item
   lAttr = GetAttr(sDirectory & v(l))
   With Ret
  
     If lAttr And vbDirectory Then
       'this item is a directory
       .dcDirectories = .dcDirectories & Delimiter & v(l)
       .dcDirectory = .dcDirectory + 1
     Else
       'this item is a file
       .dcFiles = .dcFiles & Delimiter & v(l)
     End If
    
     'add counts to applicable file properties
     If lAttr And vbArchive Then .dcArchive = .dcArchive + 1
     If lAttr And vbSystem Then .dcSystem = .dcSystem + 1
     If lAttr And vbHidden Then .dcHidden = .dcHidden + 1
     If lAttr And vbReadOnly Then .dcReadOnly = .dcReadOnly + 1
    
   End With

Next

'cleanup the return structure
'(remove leading delimiter from list strings)
With Ret
   If .dcDirectories <> "" Then
     .dcDirectories = Right( _
                     .dcDirectories, Len(.dcDirectories) - Len(Delimiter))
   End If
   If .dcFiles <> "" Then
     .dcFiles = Right(.dcFiles, Len(.dcFiles) - Len(Delimiter))
   End If
End With

'=========================
Exit_Proc:
GetDirContents = Ret
Exit Function
Error_Proc:
Select Case Err.Number
   Case Else
     MsgBox "Error: " & Trim(str(Err.Number)) & vbCrLf & _
       "Desc: " & Err.Description & vbCrLf & vbCrLf & _
       "Module: modGetDirContents, Procedure: GetDirContents" _
       , vbCritical, "Error!"
End Select
Resume Exit_Proc
Resume
End Function


Copy & Paste that code into a new module and you should be able to compile it. (I've included the public type declaration and commented out the QSortInPlace (see comments in the function header), but otherwise there's no chages)

hth
Go to the top of the page
 
+
ConorS
post Feb 27 2012, 06:51 AM
Post #6

UtterAccess Ruler
Posts: 1,265
From: Northern Ireland (Newry)



Brilliant Jleach. Got it working now. Its very handy.
Go to the top of the page
 
+
ConorS
post Feb 27 2012, 07:04 AM
Post #7

UtterAccess Ruler
Posts: 1,265
From: Northern Ireland (Newry)



I need one last thing.

when i call: MsgBox GetDirContents(MyPath).dcFiles

It throws back one long string with all the names of the files in it separated by a ;

For example: "MyFile.txt;myphoto.jpg;mydoc.pdf"

How do i split this string into 3 file names? Parse through the string with a while loop? There is bound to be a function call somewhere for this?

Regards

Conor

Go to the top of the page
 
+
ConorS
post Feb 27 2012, 07:11 AM
Post #8

UtterAccess Ruler
Posts: 1,265
From: Northern Ireland (Newry)



ok, i've used the following:

call split(GetDirContents(MyPath).dcFiles, ";")

This throws the items into an array.

I now need to go through each item in the array. (i'm getting there)




Go to the top of the page
 
+
jleach
post Feb 27 2012, 07:20 AM
Post #9

UtterAccess Editor
Posts: 6,709
From: Capital District, NY, USA



Here's a common usage of Split:

CODE
Dim v As Variant  'the array to hold the elements
Dim i As Integer 'a counter to loop the elements
Dim s As String 'a string to hold the message being built

'split the string on the ";" character
v = Split(YourDelimitedString, ";")

'now the array has a lower and upper bound (generally 0 to ?)
'we'll loop through these bounds and create a string output with a vbCrLf for display:

'loop all elements
For i = 0 To UBound(v)

  'add this element to the string
  s = s & v(i) & vbCrLf

Next i

'add some leading text to the list of files
s = "The following files were found:" & vbCrLf & s

MsgBox s


That should give some ideas on how to proceed.

hth
Go to the top of the page
 
+
ConorS
post Feb 27 2012, 07:36 AM
Post #10

UtterAccess Ruler
Posts: 1,265
From: Northern Ireland (Newry)



Thank you very much. Thats it working fine for me now.

Regards

Go to the top of the page
 
+
jleach
post Feb 27 2012, 07:38 AM
Post #11

UtterAccess Editor
Posts: 6,709
From: Capital District, NY, USA



Glad to help (IMG:style_emoticons/default/thumbup.gif)
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 18th May 2013 - 05:02 PM