Question : VBA - Access/Excel:  Scrolling through files in folder, and archiving

I have a dir() function that will tell me all the pdfs found in a particular folder.  Then within the While, I pull out the date that is found in the name of the pdf and then check to see if an archive folder has been created for that date.  If not, then a folder is created.  Then it wends to the next pdf and keeps checking and archiving as necessary.  

I've determined that you can't have a dir() nested inside another dir.  Therefore, I'm looking for ways to fix this so that I can still check the path to see if it exists, and create the folder if it doesn't, while not messing up the while that is searching through all the pdfs. I tried pulling the second dir() into a different sub process and then just calling it in the middle of the first dir(), but it still doesn't work.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
strNextFile = Dir$(strSchedulesStaffFolder & "*.pdf")

While strNextFile <> ""
            strTempFile = strNextFile 
     
'Create a temp variable to do calculations to determine folder to archive
            x = InStr(strTempFile, " - ") 
'Get the date on the document
            y = Len(strTempFile)
            strTempFile = Right(strTempFile, y - x - 2)
            x = InStr(strTempFile, ".pdf")
            strTempFile = Left(strTempFile, x - 1)
            strSchedulesStaffFolderArchive = strSchedulesStaffFolderArchive & "\" & strTempFile & "\" 'Change the archive according to date
            
            If Dir(strSchedulesStaffFolderArchive, vbDirectory) > vbNullString Then
                'Don't create anything
            Else
                MkDir strSchedulesStaffFolderArchive
            End If
            
            FileCopy strSchedulesStaffFolder & strNextFile, strSchedulesStaffFolderArchive & strNextFile
            Kill strSchedulesStaffFolder & strNextFile
            strTempFile = vbNullString
            x = 0
            y = 0
            strNextFile = Dir$
        Wend

Answer : VBA - Access/Excel:  Scrolling through files in folder, and archiving

Don't try to test for the existence.
Just create the folder anyway.
Use an error handler to test for error 75 - which would indicate the folder already exists- and resume next if you get that error.
Random Solutions  
 
programming4us programming4us