none
Renaming files with VBA

    Question

  •  

    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)

    c:\test\regsynch_01_20070601_8716871000002_8717154940015_E_LV_v14.txt

    c:\test\regsynch_01_20070601_8716871000002_8717154940015_G_LV_v14.txt

    etc.

     

    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?

     

    Thanx

    Friday, July 13, 2007 9:51 AM

Answers

  • Hi,

    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
        Loop
       
    End Sub



    Friday, July 13, 2007 11:47 AM

All replies

  • Hi,

    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
        Loop
       
    End Sub



    Friday, July 13, 2007 11:47 AM
  •  

    Thanks a lot. It works perfectly.

    Friday, July 13, 2007 12:01 PM