473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

File renaming using VBScript

tsopr
3
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
Feb 12 '18 #1

✓ answered by Frinavale

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.

2 1755
Frinavale
9,735 Expert Mod 8TB
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.
Feb 21 '18 #2
tsopr
3
Thank you for all your assistance!
Mar 13 '18 #3

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

Similar topics

1
by: Jon | last post by:
I need to generate an XML File dynamically using ASP and a SQL Database. I don't have time to learn php or any other programing language. I need a ASP page to either append information to an XML...
2
by: Scott | last post by:
Is there a way to check if a file exist using VBScript in an ASP page. So the code might go something like this: If Exists("C:\Junk\1.txt") then Do something Else Do something else End If ...
0
by: csgraham74 | last post by:
Hi guys, I am writing an application in asp.net but im attempting to write some client side validation. I have most of this working but i am having trouble reading cookies set in asp.net using...
0
by: neon123 | last post by:
i am using a sql loader to load data into oracle 8. i am using vbscript to run the sql loader. i need to pass username and password entered through vbscript ,,into the visual basic application . ...
0
by: uspramod | last post by:
hi the source is an excel file it is pivoted, i want to do unpivoting using vbscript regards Pramod
5
by: Rohit111111 | last post by:
Hello All, I new to ASP and i am using VBScript, I want to delete a file which is located on the remote machine hbow can i delete that file.plz help its urgent
5
by: meenu_susi | last post by:
hi all Could you please help me out i creating folder using vbscript.. I have used the following code... <SCRIPT LANGUAGE="VBScript"> <!-- Option Explicit
5
Soniad
by: Soniad | last post by:
Hello, I want to include asp file within vbscript, this file name i am taking in variable . and when i use this variable in the include tag outside delimiters(<%%>), it does not work, <%Dim...
1
by: redree | last post by:
I have an excel sheet "project.xlsm" with a macro "add" that update a cell value. i want to run the macro using vbscript externally. the important thing is i don't want to vbscript to open the...
0
tsopr
by: tsopr | last post by:
Hello, I've created a simple batch file (as listed below) for renaming multiple files in the same directory: @echo off SETLOCAL ENABLEDELAYEDEXPANSION SET old=EN SET new=_ENG
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.