Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename all *.txt file extensions to *.csv in a directory

Status
Not open for further replies.

JustATheory

IS-IT--Management
Feb 27, 2003
115
0
0
US
Greetings,

I'm trying to rename all files with a *.txt file extension to a *.csv extension within a directory. The file number will change for each time I run this. I have code that will loop through all files in a directory count them run different routines on with the files. But I'm having trouble with this operation. Is there a wild card that I can use? I've tried using SaveAs to no avail. Any help is greatly appreciated.

Thanks,
Andy
 



Hi,

Check out the Name statement. Also check out the FileSystem object and the Folder filter.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
akakuris,
Because I don't think [tt]Name[/tt] is documented in the help file:
Code:
Name [i]OldFilename.ext[/i] As [i]NewFilename.ext[/i]

CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
It is indeed available in VBA help - just an F1 away ;-)

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Howto get help about the Name instruction:
in the debug window (Ctrl-G) type either as or vba.name and press the F1 key.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You could try something like this.

Code:
' Add a reference to Microsoft Scripting Runtime
' Add a reference to Microsoft VBScript Regular Expressions
Dim fso As FileSystemObject
Dim FolderName As String
Dim objFile As File
Dim objFolder As Folder
Dim objFileCol As Variant
Dim objRegEx As RegExp
Dim NewFileName As String
Set fso = New FileSystemObject
Set objRegEx = New RegExp

FolderName = "C:\Test\"
Set objFolder = fso.GetFolder(FolderName)
Set objFileCol = objFolder.Files
objRegEx.Pattern = "\.txt$"
objRegEx.IgnoreCase = True

For Each objFile In objFileCol
    If objRegEx.Test(objFile.Name) Then
        NewFileName = objRegEx.Replace(objFile.Name, ".csv")
        fso.MoveFile objFile, FolderName & NewFileName
    End If
Next

MsgBox "Done!"

Swi
 
Andy,

Did you get this working?

Swi
 
Try this

Call Shell("Ren C:\SomeFolder\*.txt *.csv")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top