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
Printable View
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
The easiest way I know of is:
Name "c:\test.txt" As "c:\changed.txt"
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
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
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.
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