Making a New Windows Folder

mbamber

Registered User.
Local time
Today, 22:00
Joined
Jul 30, 2013
Messages
31
Hi all,

I currently have a button on a form which, when clicked, I would like to do several things:

1) Check if the folder, "folder name (1)" exists
2) If no, make it; if yes, check if the folder, "folder name (2)" exists
3) If no, make it; if yes, check if the folder, "folder name (3)" exists
4) repeat this process until a new folder is made

The code I currently have seems to not work, and makes an infinite loop

Code:
Private Sub Export_Click()

Dim checker As Integer
Dim projPath As String

checker = 1

projPath = CurrentProject.path & "\Exported Data " & Day(Date) & " " & MonthName(Month(Date)) & " " & Year(Date) & " (" & checker & ")"

If Len(Dir(projPath, vbDirectory)) = 0 Then
   MkDir projPath
Else
    Do While Len(Dir(projPath, vbDirectory)) <> 0
        checker = checker + 1
        If Len(Dir(projPath, vbDirectory)) = 0 Then
            MkDir projPath
        End If
    Loop
End If

End Sub

This probably won't take you very long, but I've been puzzling over it for ages so any help is appreciated!

TIA
 
I'm not sure I follow, but you build projPath before the loop so it always has the same value.
 
In your DO - LOOP, there is nothing referencing the "Checker" that you are setting. You need to add this to the end of "projPath" inside the loop.
 
Not sure if this helps, but can you just add an Exit Loop after the if?
---------------------------
msdn dot microsoft dot com/en-us/library/t2at9t47.aspx


Here is some sample code that may help
Code:
'This function Exports the Staging table as a Hobsons Load File with an increment to prevent Overwiting
Public Function exportHobsonsLoad(chosenName As String)
    hobsonsExportLocation = "C:\Imported\"
    hobsonsChosenFileName = chosenName
    hobsonsCurrentDate = Month(Date) & "-" & Day(Date) & "-" & Year(Date)
    hobsonsExportFileName = hobsonsExportLocation & hobsonsCurrentDate & " " & hobsonsChosenFileName & " "
    getMinFileIncrement
    'MsgBox chosenName
    
    hobsonsExportFileName = hobsonsExportFileName & CStr(currentHobsonsExportIncrement) & ".txt"
    DoCmd.TransferText acExportDelim, "", "Staging", hobsonsExportFileName, True
    MsgBox "Exported As " & hobsonsExportFileName
End Function





'This function Exports Stagin Table asa HobsonsLoad and Overwrites Existing Files

'This function sets the current increment to the first lowest available number in order to name the file appropriately
Public Function getMinFileIncrement()
    currentHobsonsExportIncrement = 0

    For increment = 1 To 100 Step 1
        If File_Exists(hobsonsExportFileName & increment & ".txt") = False Then
            currentHobsonsExportIncrement = increment
            Exit For
        End If
    Next increment


End Function
 
I'm not sure I follow, but you build projPath before the loop so it always has the same value.

I thought by changing the variable, "checker", this would update "projPath"
 
No, that variable is static once it's set. You need to set the variable inside the loop.
 
Oh OK. I think this works now:

Code:
Private Sub Export_Click()

Dim projPath As String
Dim checker As Integer
Dim bool As Boolean

bool = True
checker = 1
projPath = CurrentProject.path & "\Exported Data " & Day(Date) & " " & MonthName(Month(Date)) & " " & Year(Date) & " (" & checker & ")"

If Len(Dir(projPath, vbDirectory)) = 0 Then
    MkDir projPath
Else
    Do While bool = True
        checker = checker + 1
        projPath = CurrentProject.path & "\Exported Data " & Day(Date) & " " & MonthName(Month(Date)) & " " & Year(Date) & " (" & checker & ")"
        If Len(Dir(projPath, vbDirectory)) = 0 Then
            MkDir projPath
            bool = False
            pathFinal = projPath
        End If
    Loop
End If
MsgBox "Export Successful!", vbOKOnly, "Alumni System"


End Sub
 

Users who are viewing this thread

Back
Top Bottom