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

Posted on 2008-11-14
Last Modified: 2013-11-05
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?
Question by:sbornstein2
    LVL 8

    Accepted 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


    Author Comment

    there is no File.Move?
    LVL 13

    Expert Comment

    Have you imported System.IO namespace in your project as suggested by OBonio
    I am trying to convert his code to  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

    Author Closing Comment

    perfect thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now