Hey there,

I'm a rookie in VBA programming and I've got a problem that I want to solve with a VBA script. I've been trying to rename files I receive and after this import them in Access. I have a script that imports the files but the renaming is a problem.

The filenames change every month. Further the number of files I receive varries between 70 and 80 a month. They have long filenames and I just want to rename them as 1.txt, 2.txt, 3.txt etc. At the moment I use a batchfile to do this but because the number of files varries this causes errors when renaming because sometimes a file can not be found.

This is the format of the files I receive (the only constant factor in the filenames is regsynch)




I want to create a script to rename all files in the specified folder regardless of the number of files present. This way it doesn't matter whetter there are 70 or 80 files.

Does anybody have an idea how I can solve this problem


Re: Renaming files with VBA

Andy Pope


Untested but should work in Access VBA.

Code Snippet

Sub X()

Dim strName As String
Dim strFile As String
Dim strPath As String
Dim lngCount As Long

strPath = "C:\temp\"
strFile = Dir(strPath & "regsynch*.txt")
Do While Len(strFile) > 0
lngCount = lngCount + 1
strName = CStr(lngCount) & ".txt"

Name strPath & strFile As strPath & strName

strFile = Dir

End Sub

Re: Renaming files with VBA


Thanks a lot. It works perfectly.