Get Folder Name

JohnLee

Registered User.
Local time
Today, 05:35
Joined
Mar 8, 2007
Messages
692
Hi,

I Need some help with writing code to get the Names of folders that sit in a specific Drive location.

I know from previous information provided on here that a Loop is required, but I don't know how to get a folder name:

The folder names are in 6 digit date format without the slashes for instance today date 24/05/2010 is shown as the folder name like this 240510.

Below is the code I have written so far with help from this forum, but I'm stuck on how to get the folder name

============
BEGIN CODE
============

Dim FS As FileSystemObject ' Declares the File System Object
Dim Folder As Folder 'Declares the Folder Object
Dim ImagePath 'Declares the ImagePath variable
Dim subFolder As Folder
Dim dtmDate As Date
Dim DateFormat As String
'Set the image path here [the location where images are be stored]
ImagePath = "\\prdfs01\images\"
'Set the File System Object
Set FS = New FileSystemObject
Set Folder = FS.GetFolder(ImagePath)
'Get the current date - 6 months

'Loops through the subfolders
For Each subFolder In Folder.Folders
'In here I believe I need some code that will get each folders name in turn, I will write additional code here also to turn the folder name in to a real date format and the If statement below will then do the rest
'If the folder name [date] is equal to or less then the current date - 6 months
If subFolder.Name <= Format(DateFormat, "yyyymmdd") Then
'Delete the folder
subFolder.Delete True
End If
DoEvents

'Indicates in the progress bar the folder being worked on
DoCmd.Echo True, "Deleting Historic Image Folders :" & Folder & " " & subFolder
Next
============
END CODE
============

Any assistance would be most appreciated.

John
 
Hi,

Thanks for your response,

I had a look at the Dir Function, I'm trying to ge the Folder Name not the File Name. I couldn't see anything in that function that obtained the Folder Name.

John
 
Code:
' Display the names in C:\ that represent directories.
MyPath = "c:\"    ' Set the path.
MyName = Dir(MyPath, vbDirectory)    ' Retrieve the first entry.
Do While MyName <> ""    ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Then
        ' Use bitwise comparison to make sure MyName is a directory.
        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
            Debug.Print MyName    ' Display entry only if it
        End If    ' it represents a directory.
    End If
    MyName = Dir    ' Get next entry.
Loop

This code will trawl though a folder list and return subfolder names. In this exampleit is using the root of c:
 
but note that DIR is not recursive, ,so you CANNOT use it to see what folders might exist in sub folders. UNFORTUNATELY!
 
but note that DIR is not recursive, ,so you CANNOT use it to see what folders might exist in sub folders. UNFORTUNATELY!

So I you would have to start by creating an array of the first level of subfolder names then loop through that with Dir() and keep working down the tree in a similar fashion.
 
Hi Guys,

Thanks for your help, I have come up with the code below from what you have provided, However I'm having problems getting my code to recognise the date format, which means it won't do the comparison I want in order for me to put in the required code to delete only folders that are equal to or less than the current date - 6 months, perhaps you could spare a moment to see where I'm going wrong:

You assistance would be most appreciated.

John

==============
BEGIN CODE
==============
Dim dtmDate As Date 'Declare the dtmDate Variable
Dim ImagePath 'Declare the ImagePath Variable
Dim ImageFolder 'Declare the ImageFolder Variable
Dim IFDateConv As Date 'Declare the IFDateConv Variable [IFDateConv = Image Folder Date Conversion]

'Get the current date - 6 months
dtmDate = DateSerial(Year(Date), Month(Date) - 6, Day(Date))

'Get the folder names in J:\ that represent directories.
ImagePath = "J:\" ' Set the path.
ImageFolder = Dir(ImagePath, vbDirectory) ' Retrieve the first entry.
Do While ImageFolder <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If ImageFolder <> "." And ImageFolder <> ".." Then
' Use bitwise comparison to make sure ImageFolder is a directory.
If (GetAttr(ImagePath & ImageFolder) And vbDirectory) = vbDirectory Then

IFDateConv = Right(ImageFolder, 2) & "/" & Mid(ImageFolder, 5, 2) & "/" & Left(ImageFolder, 4)

MsgBox ImageFolder & " = " & IFDateConv

If IFDateConv <= dtmDate Then

MsgBox "Folder " & IFDateConv & " Deleted"

End If

''Debug.Print MyFolder ' Display entry only if it
End If ' it represents a directory.
End If
ImageFolder = Dir ' Get next entry.
DoEvents

'Indicates in the progress bar the folder being worked on
DoCmd.Echo True, "Deleting Historic Image Folders :" & Folder & " " & subFolder
Loop

MsgBox "Historic Images Purged", , "Historic Images Purge"

==============
END CODE
==============
 
'Get the current date - 6 months
dtmDate = DateSerial(Year(Date), Month(Date) - 6, Day(Date))

What you need it

dtmDate = DateAdd("m",-6,Date())

In your code if it was month 3 (March) and you took 6 from it would it not be -3
 
Hi David,

this bit of code gives me what I want :

============
BEGIN CODE
============

dtmDate = DateSerial(Year(Date), Month(Date) - 6, Day(Date))

============
END CODE
============

Because it is exactly 6 months to the day, and not generically 6 months ago, so any folders that are older equal to or older than the 25th of Novemebr 2009 will be deleted.

Where I'm having a problem is getting my code recognise the conversion that I have done as a date, for some reason it doesn't work.

=============
BEGIN CODE
=============

IFDateConv = Right(ImageFolder, 2) & "/" & Mid(ImageFolder, 5, 2) & "/" & Left(ImageFolder, 4)

=============
END CODE
=============

Even though I've declared the IFDateConv variable of Date Type. When this bit of code runs it should stop after it finds a file with a date greater than the 25th of November 2009, but it doesn't it keeps going, which is where my problem is:

=============
BEGIN CODE
=============

If IFDateConv <= dtmDate Then

MsgBox "Folder " & IFDateConv & " Deleted"

End If

=============
END CODE
=============

Thanks

John
 
What is a typical name of an image folder?

also if you are extracting the yyyymmdd part of the folder name then you can treat this as a number 20100525.

Code:
Y = Left(Right(ImageFolder,8,4))
M = Left(Right(ImageFolder,4,2))
D = Right(Imagefolder,2)

ImageDate = CDate(D & "\" M & "\" Y)
 
Hi David,

Your code "dtmDate = DateAdd("m",-6,Date())" did exactly what the code I had written, so this being more efficient I changed mine to what you had provided.

An Image folder will always be of the following format:

yyyymmdd [20091125]

So for each day an image folder is produced it will be given a name with the above format, which is the date it was created.

I'll give your suggested code a try, thanks

John
 
Hi David,

Tried your code and when I ran it I got a wrong number of arguments message and the Right was highligted in the first line of the code:

=============
BEGIN CODE
=============

Y = Left(Right(ImageFolder, 8, 4))
M = Left(Right(ImageFolder, 4, 2))
D = Right(ImageFolder, 2)

============
END CODE
============
 
Sorry

Code:
Y = Left(Right(ImageFolder, 8), 4)
M = Left(Right(ImageFolder, 4), 2)
D = Right(ImageFolder, 2)

Aircode
 
can you show us the whole of your routine

go to advanced, (at the bottom of the posting) and you will see a# symbol, which will put all your text into a small "code" window.

its more readsble than the way you are presenting the code at present
 
Hi David,

Thanks for all your help, just one more thing, to delete a folder I have been reading the help file on the function FolderDelete, however I'm not getting how to use this function, the way it's written is not clear to me.

Can I use the "Kill" keyword to delete a folder in the same way it can be used to delete a text file?

John
 
Hi,

I read up on the RMDir as this was suggested, but according to the information on this function it will error if there are subfolders or files in a directory when trying to delete it, if I understand correctly the Directory or Folder to be deleted must be empty of any sub folders and files.

Is this the only way to delete a directory containing subfolders and files?

John
 
You can delete the files first in the directory that you want to delete. Just use \path\ & *.* in your kill statement to delete all the files in the directory that you want to delete. Ensure you have your code set right or else you will delete files you did not intend to delete!
 
Back in the days of DOS there was a command called DelTree this deleted folders even if they contained files and sub folders. Everything from that position downwards was zapped. Don't know if this still exists but you could give it a try.
 
Hi Ghudson,

Thanks for your suggestion, but I would prefer not to have to add more code to delete sub folders and files seperately, I have been reading up on the DeleteFolder Method, which says that a folder and all it's contents would be deleted, this sounds to me to be the best option. Now I've got to work out how to get the DeleteFolder method to work.

Any suggestions on how I can do this, below is the information in the help files, It doesn't provide an example so I'm somewhat stuck:

Description
Deletes a specified folder and its contents.
Syntax
object.DeleteFolder folderspec[, force]
The DeleteFolder method syntax has these parts:
PartDescriptionobjectRequired. Always the name of a FileSystemObject.folderspecRequired. The name of the folder to delete. The folderspec can contain wildcard characters in the last path component.forceOptional. Boolean value that is True if folders with the read-only attribute set are to be deleted; False (default) if they are not.


Remarks
The DeleteFolder method does not distinguish between folders that have contents and those that do not. The specified folder is deleted regardless of whether or not it has contents.
An error occurs if no matching folders are found. The DeleteFolder method stops on the first error it encounters. No attempt is made to roll back or undo any changes that were made before an error occurred.

Any assistance would be most appreciated.

John
 

Users who are viewing this thread

Back
Top Bottom