VB.NET - Command Prompt to rename file

Posted on 2009-05-01
Last Modified: 2012-05-06
Hello all,

I am curious if anyone has done this before.  I would like to create a small application that would search through users network folder and rename a particular file.  I was thinking about using VB.Net to do the search and than command prompt to do the renaming.  

Anyone can hep me out or might have something similar?  Basically I am looking for a file name Config.ini and rename that to Config.ini_old so that the old configuration can't be use.  This config file is stored on the network drive H.
Question by:holemania
    LVL 4

    Assisted Solution

    There are a number of ways of doing this.
    Check out the System.IO Namespace.
    You could also run a process such as....

    System.Diagnostics.Process.Start("cmd.exe rename " & "config.ini config.ini_old")
    include the path as needed.
    LVL 25

    Assisted Solution

    Why wouldn't you just use:

    LVL 4

    Expert Comment

    Good point Story :)
    LVL 9

    Accepted Solution

    Greetings :)

    You are probably looking for a function like below, usage would be


    returns the number of files renamed as integer, so you can verify that like

    dim x as integer = renameFiles("H:\","Config.ini","Config_old.ini")

    if it is 0 (zero) no files have been renamed

    hope this helps,

    Imports System.Collections.ObjectModel
    Public Function renameFiles(ByVal path As String, ByVal oldFilename As String, ByVal newFilename As String) As Integer
            Dim ctr As Integer = 0
                Dim foundfiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String) _
                 = My.Computer.FileSystem.GetFiles(path, FileIO.SearchOption.SearchAllSubDirectories, oldFilename)
                For Each file As String In foundfiles
                    My.Computer.FileSystem.RenameFile(file, newFilename)
                    If System.IO.File.Exists(System.IO.Path.GetDirectoryName(file) & "\" & newFilename) Then ctr = ctr + 1
                Return ctr
            Catch ex As Exception
                Return ctr
            End Try
    End Function

    Open in new window

    LVL 25

    Expert Comment

    If you have local access to the server, then a simple batch file would do the job.

    Author Comment

    Wow thanks.  Didn't know it was that simple.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    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…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now