Reputation: 23
After searching for quite sometime now, one cannot just find a simple answer. So here is my question:
I am writing in Visual Basic from Visual Studio 2010.
(Another question that I need answered but is not relevant: Is this language referred to as VB .NET ?; I am new to this language but not to programming as I am a web designer.)
I have a file in this directory: C:\users\Debbie
This file's name and extension are as followed: backup01.zip
What I need to know is: How do I take this file that I have created and quickly change the extension without moving it to another directory or making a copy. I just want my program to find the file backup01.zip and quickly change it to backup01.nbu
.nbu will be the file extensions that my software will be able to open (when I get there).
Thanks..
(P.S.) If anyone is curious as to why I am already writing files it would be because I am jumping into this and trying my best to create simple backup software for my Mother whose original software that she uses frequently on her computer does not have backup capabilities. Keep in mind that I am an aspired programmer familiar with languages such as HTML, CSS, PHP etc. and I am trying to pick this one up by myself as well.
Upvotes: 1
Views: 6579
Reputation: 1
I have just recently made my first program in Visual Studio 2013 and I have no technical programming skills, so bear over with me here. What you want to achieve is easy in command line and as far as I know this should work in Visual Studio:
Process.Start("cmd.exe", "/c ren C:\users\Debbie\backup01.zip backup01.nbu")
It starts the command line /c ends it when it's done and the rest is self explanatory. It is a little "workaround", but it will get the job done.
Upvotes: -1
Reputation: 331
Seems like even I'm using Visual Basic 2010!
I would recommend you to use this:
My.Computer.FileSystem.RenameFile("C:\users\Debbie\backup01.zip", "C:\users\Debbie\backup01.nbu")
That's all. Hope it helps!
Cheers, Sreenikethan
Upvotes: 0
Reputation: 148
First the simple question - yes, it is called vb.net (2002 it became a .NET Framework language, before that the last version was called VB 6.0)
As to the renaming issue, here is the code:
Dim goodFileName As String = "C:\mydir\myfile.com.extension"
Dim result As String= Path.ChangeExtension(goodFileName, ".old")
File.Move(goodFileName ,result)
The ChangeExtension
only returns the new full path, but does not edit the file itself in any way.
Upvotes: 0
Reputation: 164341
Use the File.Move
method:
System.IO.File.Move("C:\users\Debbie\backup01.zip", "C:\users\Debbie\backup01.nbu")
You likely have the file name in a variable already, so Path.ChangeExtension
will be handy:
Dim originalFile As String = "C:\users\Debbie\backup01.zip"
Dim newName As String = Path.ChangeExtension(originalFile, "nbu")
File.Move(originalFile,newName)
Upvotes: 4