Get Folder Name

This is a usefull one for getting a "random" folder name, needs so redoing in that case but...
Code:
Function myFolder()
    myFolder = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\"))
End Function
 
Hi namliam,

Thanks for your response, but I'm past the get folder stage, I've got code that does that, I looking to workout how to delete a folder using the DeleteFolder method.

Thanks for your time

John
 
in dos (cmd)

rmdir /s seems to delete an entire tree

so just
"shell rmdir .... " should work, shouldnt it
 
Hi folks,

Thanks for all the help, I think this may help below is the code that I have put together with your help so far

Code:
Public Function Delete_Folders()
    On Error Resume Next
    
    Dim FS As FileSystemObject ' Declares the File System Object
    
    'Set the File System Object
    Set FS = CreateObject("Scripting.FileSystemObject")
    
    Dim dtmDate As Date 'Declare the dtmDate variable
    Dim ImagePath 'Declare the ImagePath variable
    Dim ImageFolder 'Declare the ImageFolder variable
    Dim ImageFolderDate ' Declare the ImageFolderDate variable
    Dim strDay 'Declare the strDay variable
    Dim strMonth 'Declare the strMonth variable
    Dim strYear 'Declare the strYear variable
        
    'Get the current date - 6 months [182 days]
    dtmDate = DateAdd("m", -6, Date)
    
    'Get the folder names in J:\ that represent directories.
    ImagePath = "J:\"    'Set the path to the Image Folders.
    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
            'Get the Name of the ImageFolder then
            If (GetAttr(ImagePath & ImageFolder) And vbDirectory) = vbDirectory Then
                
                strYear = Left(ImageFolder, 4) 'Get the left four characters of the ImageFolder Name
                strMonth = Mid(ImageFolder, 5, 2) 'Get the 5th and 6th characters of the ImageFolder Name
                strDay = Right(ImageFolder, 2) 'Get the 7th and 8th characters of the ImageFolder Name
               
                'Convert the ImageFolderDate to true date data type
                ImageFolderDate = CDate(strDay & "/" & strMonth & "/" & strYear)
                
                'If the ImageFolderDate is equal to or less than the dtmDate variable then
                If ImageFolderDate <= dtmDate Then
                    
                    'Delete the Folder
                    [COLOR=red]FS.DeleteFolder (ImageFolder)
[/COLOR]                    
                    MsgBox "Folder " & ImageFolderDate & " Deleted"
                    
                End If
            End If
        End If
        ImageFolder = Dir 'Get the next folder.
        DoEvents
        
        'Indicates in the progress bar the folder being worked on
        DoCmd.Echo True, "Deleting Historic Image Folders :" & ImageFolder
    Loop
    
    MsgBox "Historic Images Folders Purged", , "Historic Images Folders Purge Process"
    
End Function

The code in red for some reason is not deleting the folders that fall into my required criteria.

Have a look and let me know your thoughts,

Thanks once again

John
 
put MsgBox ImageFolder above your FS.DeleteFolder (ImageFolder) line and see what the value of ImageFolder is to ensure you have a valid \path\folder\ string to delete.

I am guessing that you need ... FS.DeleteFolder (ImagePath & ImageFolder)
 
Hi Ghudson,

Done as you suggested and the correct folder names popped up in the message box i.e.
20080807
20080810
20080811 etc etc

looking at your post, do you mean that the full file path should appear in the message box i.e. J:\20080807 etc etc

thanks

John
 
Hi Ghudson,

looking at your post, do you mean that the full file path should appear in the message box i.e. J:\20080807 etc etc

John

Yes! You need to give the full path \drive\folder\
 
Hi GHudson,

Thanks for that, that worked.

I would also like to thank everybody else who has helped me here today, fantastic support from all of you, pushing me in the right direction.

Just in case anyone else needs to do something along these lines here's what my complete code looks like:

Code:
Public Function Delete_Folders()
    On Error Resume Next
    
    Dim FS As FileSystemObject ' Declares the File System Object
    
    'Set the File System Object
    Set FS = CreateObject("Scripting.FileSystemObject")
    
    Dim dtmDate As Date 'Declare the dtmDate variable
    Dim ImagePath 'Declare the ImagePath variable
    Dim ImageFolder 'Declare the ImageFolder variable
    Dim ImageFolderDate ' Declare the ImageFolderDate variable
    Dim strDay 'Declare the strDay variable
    Dim strMonth 'Declare the strMonth variable
    Dim strYear 'Declare the strYear variable
        
    'Get the current date - 6 months [182 days]
    dtmDate = DateAdd("m", -9, Date)
    
    'Get the folder names in J:\ that represent directories.
    ImagePath = "J:\"    'Set the path to the Image Folders.
    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
            'Get the Name of the ImageFolder then
            If (GetAttr(ImagePath & ImageFolder) And vbDirectory) = vbDirectory Then
                
                strYear = Left(ImageFolder, 4) 'Get the left four characters of the ImageFolder Name
                strMonth = Mid(ImageFolder, 5, 2) 'Get the 5th and 6th characters of the ImageFolder Name
                strDay = Right(ImageFolder, 2) 'Get the 7th and 8th characters of the ImageFolder Name
               
                'Convert the ImageFolderDate variable to true date data type
                ImageFolderDate = CDate(strDay & "/" & strMonth & "/" & strYear)
                
                'If the ImageFolderDate variable is equal to or less than the dtmDate variable then
                If ImageFolderDate <= dtmDate Then
                    
                    'Delete the Folder
                    FS.DeleteFolder ("J:\" & ImageFolder)
                    
                End If
            End If
        End If
        ImageFolder = Dir 'Get the next folder.
        DoEvents
        
        'Indicates in the progress bar the folder being worked on
        DoCmd.Echo True, "Deleting Historic Image Folders :" & ImageFolder
    Loop
    
    MsgBox "Historic Images Folders Purged", , "Historic Images Folders Purge Process"
    
End Function

Once again a big thankyou to you all

John
 
I would use FS.DeleteFolder (ImagePath & ImageFolder) instead of FS.DeleteFolder ("J:\" & ImageFolder)
 
Hi Ghudson,

Yes, your right, I'll change it now.

thanks once again

John
 

Users who are viewing this thread

Back
Top Bottom