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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming File extensions in a directory

Status
Not open for further replies.

Yoxy99

MIS
Jul 28, 2007
23
US
Hello I am currently building a project that involves renaming file extensions, and what I am trying to do is to have the user input a directory into a textbox for example C:\new, and then from that information I want the files in that directory to be changed from .txt to .doc, I was wondering if anyone had any ideas on how to do this?
 
Hi, start by getting yourself a list of the files in folder in question.

FileList = My.Computer.FileSystem.GetFiles("PathFromTextBox", FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
'Iterate the list of files
For Each File In FileList
'Make a copy of the new file name
ToFile = File.Replace(".txt", ".doc")
'Rename the file
My.Computer.FileSystem.RenameFile(File, ToFile)
'Move on to the next file.
Next

I Hope this helps.

--



woogoo
 
the System.IO.File class may be of use to you as well.

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Thanks for the Start but what should I declare FileList and File as because they are coming up with undeclared errors?
 
go to msdn2.com and read about the System.IO.File class. A handout never helped anyone. You should be able to figure it out.

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Its not really changing the extensions that is giving me a problem, it is passing the directory from the textbox to the path in the code that is whats giving me problems.
 
My form has a textbox and a button and what I want it to do is when you enter a directory in the textbox I want all of the extensions from the files in that directory to go from .txt to .doc.
 
There are more elegant ways of doing this, there is plenty of code here and other places that will tell you how to present a treecontrol and combobox to display your drives, and directories, making it easier for the user to select.

Using this approach will greatly reduce the possibility of errors. But if you want to persist then:

FileList = My.Computer.FileSystem.GetFiles(Text1.Text(), FileIO.SearchOption.SearchTopLevelOnly, "*.txt")

As to what you declare FileList as I have to agree with AlexCuse, it will take you two minutes to find out, and as a result you will have learned something.
--

woogoo
 
Or create an old DOS batch file:
Code:
        Dim shlStr As String = "Rename C:\New\*.txt *.doc"
        Dim wrt As New IO.StreamWriter("C:\MyBatch.bat", False)
        wrt.WriteLine(shlStr)
        'wrt.WriteLine("pause")
        wrt.WriteLine("exit")
        wrt.Close()

        Dim p As New Process
        Dim pi As New ProcessStartInfo("C:\MyBatch.bat")
        p.StartInfo = pi
        p.Start()
        p.WaitForExit()
        p.Close()
        IO.File.Delete("C:\MyBatch.bat")
 
Thank you mansii, woogoo, AlexCuse I got this to work and I am going to use a treecontrol like you suggested to help prevent errors, so thanks for the help.
 
Going back to the previous example above it would be quicker to use :

Code:
Dim m_FSD As FolderBrowserDialog = New FolderBrowserDialog()
m_FSD.ShowDialog()
Dim m_Path As String = m_FSD.SelectedPath()
FileList = My.Computer.FileSystem.GetFiles(m_Path, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")

Just attach a variation of this to your Button's click event.
--


woogoo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top