Renaming Files

  • Thread starter Thread starter Edward Bird
  • Start date Start date
E

Edward Bird

Greetings,

I'm writing an application that will rename any files on my harddrive that
contain the underscore character _ and replace it with a space character '
'.

However I've looked in the FileInfo class, and can't find a method to rename
files. The closest thing I can find is

Public Sub MoveTo( _
ByVal destFileName As String _
)

which can specify a different file name. Are there any other ways of doing
it?

regards,

Edward.
 
August 7, 2004

You can use the following code. The code will take the
filename
parameter and use the String.Split("_") method to put the name into
an
array. Then it will use the String.Join(" ") method to recreate the
file name
with the correct whitespace character.

Imports System.IO

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles Button1.Click

If ChangeName(txtFile.Text) = True Then
MsgBox("Changed")
Else
MsgBox("Unchanged")
End If

End Sub

Private Function ChangeName(ByVal filename As String) As Boolean

If File.Exists(filename) Then

Try
Dim oldfile As String = filename
Dim fileparts(1) As String
Dim iFile As New FileInfo(oldfile)

fileparts = filename.Split("_")
filename = filename.Join(" ", fileparts)

iFile.CopyTo(filename)
iFile.Delete()
Return True

Catch ex As Exception
Return False
End Try

Else
Return False
End If

End Function

This function assumes that the file ONLY contains one underscore.
You could easily add another parameter that the function uses to rejoin
the
fileparts! Good luck!


Joseph MCP
 
Online help says
Rename or move a file.

so I guess that is how you rename a file in .net.

Doesn't it work when you use it?
File.Move or FileInfo.MoveTo
 
Really copying and the deleting shouldn't be doing more than what move will
do for you...I would think.

Shane
 
how about executing the 'rename' or 'ren' command using
System.Diagnostics.Process?

Imran.
 
Yeah, lol I just searched for rename in the index and it came up with the
rename function:

Public Sub Rename( _
ByVal OldPath As String, _
ByVal NewPath As String _
)

which is exactly what I wanted,

Sometimes its as easy as it sounds..most times its not though...

regards,

Edward.
 
Joseph said:
Dim oldfile As String = filename
Dim fileparts(1) As String

fileparts = filename.Split("_")
filename = filename.Join(" ", fileparts)

What's wrong with just:

filename = filename.Replace("_", " ")

Much easier to write and to read.
 
What's wrong with just:

filename = filename.Replace("_", " ")

Because that just changes the string. It does not actually rename the
file.

The Move method is the correct one to rename a file.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Chris said:
Because that just changes the string. It does not actually rename the
file.

I know that, I was referring to the convoluted string manipulation code
(which I quoted in my reply), not the actual renaming of the file (which I
didn't quote).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top