473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File rename

1 New Member
Hello,

We have files ( named in the following format ) in one of our folders:

MV0001.XLS
MV0002.XLS
and so on

WV0001.XLS
WV0002.XLS
and so son

CV0001.XLS
CV0002.XLS
and so on

We need a script that will rename the files in the following way:

if filename starts with 'M', rename file to " XXXXX_SERVER1_YYYY_MM"
( where 'XXXXX' are the 2nd - 6th characters of the original file name e.g.V0001,
V0002 etc,
'AA' are constant texts,
'YYYY' is current year,
'MM' is previous month )

similarly, if filename starts with 'W', rename files to "XXXXX_SERVER2_YYYY_MM" , and if it starts with
'C', rename to "XXXXX_SERVER3_YYYY_MM".

Any help will be greatly appreciated.
Feb 21 '08 #1
2 1708
debasisdas
8,127 Recognized Expert Expert
Kindly post what you have tried so far.
Feb 21 '08 #2
jamesd0142
469 Contributor
I tried something similar: take a look and see if it gives you any idea where to start...

Expand|Select|Wrap|Line Numbers
  1. Private Sub RenameFiles(ByVal StartPath As String)
  2.         Dim myfolder As DirectoryInfo = New DirectoryInfo(StartPath)
  3.         Dim mySubfolders() As FileInfo = myfolder.GetFiles()
  4.         Dim strFiles() As FileInfo = myfolder.GetFiles()
  5.  
  6.         For Each myItem As FileInfo In strFiles
  7.             'myItem.Delete()
  8.             'open File
  9.             Dim a As String = StartPath & "\" & myItem.ToString
  10.  
  11.             'read File
  12.             Dim EntireLine1 As String
  13.             Dim oFile1 As System.IO.File
  14.             Dim oRead1 As System.IO.StreamReader
  15.             If System.IO.File.Exists(a) = True Then
  16.                 oRead1 = oFile1.OpenText(a)
  17.                 EntireLine1 = oRead1.ReadToEnd
  18.                 oRead1.Close() 'test line
  19.             End If
  20.             Dim b As String = "2008" 'vbCrLf
  21.             'find date
  22.             date1 = EntireLine1.Substring(EntireLine1.IndexOf("DATE"), EntireLine1.IndexOf(b)) ', EntireLine1.IndexOf("2008"))
  23.  
  24.             'function cuts of the end of the line where it finds a return character or line feed...
  25.             For i = 1 To Len(date1)
  26.                 j = Mid(date1, i, 1)
  27.                 If a = vbCr Or a = vbLf Then
  28.                     Exit For
  29.                 End If
  30.             Next
  31.             date1 = Mid(date1, 6, i - 7)
  32.  
  33.             'rename File
  34.             If System.IO.File.Exists(a) = True Then
  35.                 FileSystem.Rename(a, StartPath & "\" & RTrim(date1) & ".txt")
  36.                 'System.IO.File.Move(a, StartPath & "\" & RTrim(date1) & ".txt")
  37.                 'either works^^^^^^
  38.             End If
  39.  
  40.         Next
  41.  
  42.     End Sub
  43.  
Feb 21 '08 #3

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

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
3
by: Willem van Isselmuden | last post by:
Hello, i have this upload script, works fine, ik kan upload multiple files to my server, but i want them to be saved under a different namen on the server. My form has 4 file fields and i want...
5
by: Richard L Rosenheim | last post by:
Correct me if I'm wrong... It appears that out of the 1800+ classes in the .Net framework, there isn't a file rename method (discounting the rename function in the vb namespace). And that the...
3
by: Mullin Yu | last post by:
I have a file, and want to replace some bytes by other bytes e.g. Old: 1b 25 New: 1b 26 66 31 30 30 59 1b 26 66 58 How to implement? Thanks!
4
by: Patrick de Ridder | last post by:
This is rather elementary but I cannot find it anywhere. How do you rename a file from within a program in C# ? (In the directory I mean.) How do you delete a file from within a program in C# ?...
7
by: Lalasa | last post by:
Hi, Can anybody tell me how many cpu cycles File.copy would take and how many cpu cycles File.Move would take? CFile::Rename in C++ takes just one cpu cycle. As there is no File.Rename in C#,...
1
by: hardik | last post by:
can anyone tell me how i can change the filename which is going to uploaded here is a code for uploading a file <!-- #include file="clsUpload.asp" --> Set objUpload = New clsUpload If...
1
by: spacehopper_man | last post by:
no "rename" operation in C# !!! - this has been covered in this group before, but I can't find any good answers. what I am trying to do is refresh the content in a file with minimum...
1
by: vijayarl | last post by:
Hi All, Requirement : 1.read the content of the dir 2.seach for "prstat-Lvs" file 3.rename this file to *.txt (as this file doesn't have the file typeextension --raw file) 4.open this *.txt...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.