[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

ASP.Net/VB.Net - How do I rename a file in a directory?

I have a directory where I will have one excel file with the XLS extension.  I need to check in the directory that there is only one file and that it is in XLS and then rename it.  I won't know what the actual file name will be.  Any help?
  • 2
1 Solution
Sorry this is C#, but it should be apparent what's needed..

using System.IO;
DirectoryInfo di = new DirectoryInfo(dirPath);
FileInfo[] fis = di.GetFiles("*.xls"); // get all the files in the dir
if(fis.Length==1) // make sure there is only one file
  string filePath = fis[0].FullName;
  File.Move(filePath, newFileNameAndPath);

Open in new window

sbornstein2Author Commented:
there is no File.Move?
Have you imported System.IO namespace in your project as suggested by OBonio
I am trying to convert his code to vb.net  below. I guess you can correct any syntax errors

import System.IO
dim di as DirectoryInfo = new DirectoryInfo(dirPath)
dim fis as FileInfo() = di.GetFiles("*.xls") ' get all the files in the dir
if(fis.Length=1) then ' make sure there is only one file
  dim filePath as string = fis(0).FullName
  File.Move(filePath, newFileNameAndPath) ' new file path should point to same directory in order to rename.
end if

Basically when you use File.Move("c:\files\file.xls","c:\files\renamedfile.xls") the application will rename your file from file.xls to renamedfile.xls
sbornstein2Author Commented:
perfect thanks

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now