Results 1 to 6 of 6

Thread: Whats the best way to rename a text file ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300
    What is the best method of renaming a text file

    code would be great, I am looking for the quickest way possible.

    Many thanks
    Locutus
    Resistance is futile

  2. #2
    Guest
    The easiest way I know of is:

    Name "c:\test.txt" As "c:\changed.txt"

  3. #3
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    More control using this?

    Code:
    Private Sub RenameFile(strDest As String, strOld As String)
        Dim fs, f
        
        'Example files:
        '
        'strOld = "C:\WINDOWS\Desktop\TestFiles\OldFile.txt"
        'strDest = "C:\WINDOWS\Desktop\TestFiles\NewFile.txt"
        
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFile(strOld)
        f.Move strDest
       
        Set f = Nothing
        Set fs = Nothing
    End Sub

  4. #4
    Guest
    I ran a test of Steves code and found that the "name f1 as f2" method was faster if your dealing with many files.

    I renamed 150 files.

    Name As : 1332 ticks
    RenameFile : 1552 ticks

  5. #5
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184

    Smile

    ah...... but did you close/open the filesystem object each time?

    Keep this bit running:
    Set fs = CreateObject("Scripting.FileSystemObject")

    Only reset this bit.
    Set f = fs.GetFile(strOld)

    No need to set x = nothing each time, just re-allocate the
    variable, clean up at the end.


  6. #6
    Guest
    No I did not. I just used your function as you wrote it. Do you suggest that I test it with passing a filesystem object to your function so the overhead of creating a new object each time is remved. I dont think that is good programming, but it sertanly will speed up the process...

    This was my test code:

    c = GetTickCount
    For i = 1 To 150
    oldf = "c:\test\Kopia (" & i & ") av test.txt"
    newf = "c:\test\Changed (" & i & "} av test.txt"
    Name oldf As newf
    Next i
    d = GetTickCount
    nt = d - c

    c = GetTickCount
    For i = 1 To 150
    newf = "c:\test\Kopia (" & i & ") av test.txt"
    oldf = "c:\test\Changed (" & i & "} av test.txt"
    RenameFile newf, oldf
    Next i
    d = GetTickCount
    rt = d - c


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width