[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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