422,026 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,026 IT Pros & Developers. It's quick & easy.

File renaming using VBScript

tsopr
P: 2
Hello,

I've created a simple batch file (as listed below) for renaming multiple files in the same directory:

Expand|Select|Wrap|Line Numbers
  1.  
  2. @echo off
  3. SETLOCAL ENABLEDELAYEDEXPANSION
  4. SET old=EN
  5. SET new=_ENG
  6. for /f "tokens=*" %%f in ('dir /b *.890') do (
  7.   SET newname=%%f
  8.   SET newname=!newname:%old%=%new%!
  9.   move "%%f" "!newname!"
  10. )
  11.  
As you can see the script only changes "EN" to "_ENG".
My problem is - I need this script to work in a network directory (not a local one) and the .bat script does not working in mine network environment.
I understood that there is a simple VBScript that does the same as mine .bat but also works in the network environment.


Thanks for your attention. Iím looking forward to your reply.
Miki
1 Week Ago #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,712
You have posted your question in the VB.NET forum.

You can easily write a simple program (like a console application) that renames files in a directory like what you are looking for.

First you would have to download and install something like Visual Studio Express which will let implement (write) the code and then will compile the code into an application that you can run.

Then you would just have to check the Microsoft Documentation on the MSDN Library to learn about things like the FileSystem.RenameFile Method, the Directory.GetFiles Method, and the String Replace and Substring methods . The documentation in the MSDN library even contains code examples (see the links).

For example, this console application will rename all file names in the directory supplied:
Expand|Select|Wrap|Line Numbers
  1. Module Module1
  2.  
  3.     Sub Main(ByVal args As String())
  4.  
  5.         'Example from MSDN:
  6.         'My.Computer.FileSystem.RenameFile("C:\FilesToRename\Test.txt", "SecondTest.txt")
  7.  
  8.         ' You can supply arguments to the command line application through the command line when you execute the application
  9.         ' This application expects 3 arguments: the path to the folder that contain the files, 
  10.         ' the old part of the name (string) to replace and the new value that will replace the old one (also a string)
  11.         'If Not args.Count = 3 Then
  12.         '       Console.WriteLine("Please provide a path to the directory with the files in it, the text you want to replace, and the text you want to replace it with.")
  13.         'Else
  14.         '       do processing
  15.         'End If
  16.  
  17.         Dim path As String = String.Empty
  18.         Dim originalText As String = String.Empty
  19.         Dim replacementText As String = String.Empty
  20.  
  21.         If Not args.Count = 3 Then
  22.             path = "C:\FilesToRename\"
  23.             originalText = "en"
  24.             replacementText = "eng"
  25.         Else
  26.             path = args(0)
  27.             originalText = args(1)
  28.             replacementText = args(2)
  29.         End If
  30.  
  31.         Dim errorMessage As String = String.Empty
  32.  
  33.         If Not System.IO.Directory.Exists(path) Then
  34.             errorMessage = String.Format("'{0}' does not exist or is invalid.", path)
  35.         End If
  36.         If String.IsNullOrEmpty(originalText) Then
  37.             errorMessage = "Please provide text that you would like replaced"
  38.         End If
  39.  
  40.         If Not String.IsNullOrEmpty(errorMessage) Then
  41.             Console.WriteLine(errorMessage)
  42.         Else
  43.             ProcessDirectory(path, originalText, replacementText)
  44.         End If
  45.  
  46.  
  47.         Console.WriteLine("Please press enter to end the application")
  48.         Console.ReadLine()
  49.  
  50.     End Sub
  51.     Public Sub ProcessDirectory(ByVal targetDirectory As String, ByVal originalText As String, ByVal replacementText As String)
  52.         Dim fileEntries As String() = System.IO.Directory.GetFiles(targetDirectory)
  53.         ' Process the list of files found in the directory.
  54.         Dim fileName As String
  55.         For Each fileName In fileEntries
  56.             Dim lastDirSeparatorIndex = fileName.LastIndexOf("\"c)
  57.             Dim originalFileName = fileName.Substring(lastDirSeparatorIndex + 1, fileName.Length - lastDirSeparatorIndex - 1)
  58.             Dim newName = originalFileName.Replace(originalText, replacementText)
  59.             Console.WriteLine(String.Format("Renaming '{0}' to '{1}'", originalFileName, newName))
  60.             My.Computer.FileSystem.RenameFile(fileName, newName)
  61.         Next
  62.         ''The following uses recursion to loop through
  63.         ''each sub directory and rename all the files
  64.         ''using this same process
  65.         ''(recursion is a fancy way of saying the function calls itself again to process additional data)
  66.         'Dim subdirectoryEntries As String() = System.IO.Directory.GetDirectories(targetDirectory)
  67.         '' Recurse into subdirectories of this directory.
  68.         'Dim subdirectory As String
  69.         'For Each subdirectory In subdirectoryEntries
  70.         '    ProcessDirectory(subdirectory, originalText, replacementText)
  71.         'Next 
  72.     End Sub 'End of ProcessDirectory
  73. End Module
And if you have any problems or questions just post back here and we'll help you.
4 Days Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.