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

VB.NET - Command Prompt to rename file

Hello all,

I am curious if anyone has done this before.  I would like to create a small vb.net 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.
0
holemania
Asked:
holemania
3 Solutions
 
Thunder724Commented:
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.
0
 
SStoryCommented:
Why wouldn't you just use:

        My.Computer.FileSystem.RenameFile("H:\config.ini","h"config.ini_old")
0
 
Thunder724Commented:
Good point Story :)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
CyberLexCommented:
Greetings :)

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

renameFiles("H:\","Config.ini","Config_old.ini")

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,
cheers
CyberLex



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
 
        Try
 
            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
 
            Next
 
            Return ctr
 
        Catch ex As Exception
 
            MsgBox(ex.ToString)
            Return ctr
 
        End Try
 
End Function

Open in new window

0
 
SStoryCommented:
If you have local access to the server, then a simple batch file would do the job.
0
 
holemaniaAuthor Commented:
Wow thanks.  Didn't know it was that simple.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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