Rename File Question

volboy04

New member
Joined
Jul 16, 2008
Messages
4
Programming Experience
3-5
I am new to VB, and trying to implement a very simple procedure. I wish to rename all of the files in a given directory from *.txt extesions, to .csv extensions.

In a DOS command prompt, all I would need (after getting to the right directory) would be this line:

VB.NET:
rename *.txt *.

When the program is run, there will be a variable number of files in the directory.

What is the simplest way to do thi in VB.Net? For the life of me, I can't figure it out. This is the closest I've come:

VB.NET:
       Dim Process As New System.Diagnostics.Process
        Dim psi As ProcessStartInfo = New ProcessStartInfo

        psi.FileName = "cmd.exe"
        psi.WorkingDirectory = TextBox1.Text
        psi.Arguments = "rename *.txt *.csv "
        psi.CreateNoWindow = True

        Process.StartInfo = psi
        Process.Start()

but all this does is pop up the CMD prompt...the rename function doesn't run.

This is driving me crazy, because it should be (and probably is) so simple.

A many great thanks in advance for any help.
 
.Net has a nice set of file tools in the System.IO namespace, do check them out in documentation.
VB.NET:
For Each file As String In IO.Directory.GetFiles("c:\path", "*.txt")
    IO.File.Move(file, IO.Path.ChangeExtension(file, ".csv"))            
Next
As you see renaming a file is actually a move operation.

Also, the Cmd.exe application doesn't operate like that, refer to help (cmd /?).
 
Back
Top