Hi,
I have a folder with hundreds of text files. Each text file contains a row with a date. I want to rename the text file with the date contained in this text file. Example: Suppose I have 2 textfiles, then one text file would contain a row like this:
DATE 1 February 2008
and the other would contain a row like this:
DATE 2 February 2008
The rows are not necessarily at the same position (e.g. it's not always the third row) All text files have in common that the date line starts with "Date". Right now the textfiles are named by numbers. How can I rename each text file by the date given within this file? In the above example, I want textfile 1 to be "1February2008" and textfile 2 "2February2008"
Thank you very much for your help!
Alexandra
6 1736
You can follow these steps.
1.Read the content of the file
2.Create a file by that name.
3.Copy the content to the new file.
4.Delte the old file.
You have to use FileSystemObject for the purpose.
I fancied giving this a go also:
Came up with this which works:
sure you can edit it to suit: -
-
-
Imports System.IO
-
Public Class Form1
-
-
Dim date1 As String
-
Dim j As String
-
Dim i As Integer
-
Dim pos As Integer
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
DeleteAllSubFolders("c:\a")
-
End Sub
-
-
Private Sub DeleteAllSubFolders(ByVal StartPath As String)
-
Dim myfolder As DirectoryInfo = New DirectoryInfo(StartPath)
-
Dim mySubfolders() As FileInfo = myfolder.GetFiles()
-
Dim strFiles() As FileInfo = myfolder.GetFiles()
-
-
For Each myItem As FileInfo In strFiles
-
'myItem.Delete()
-
'open File
-
Dim a As String = StartPath & "\" & myItem.ToString
-
-
'read File
-
Dim EntireLine1 As String
-
Dim oFile1 As System.IO.File
-
Dim oRead1 As System.IO.StreamReader
-
If System.IO.File.Exists(a) = True Then
-
oRead1 = oFile1.OpenText(a)
-
EntireLine1 = oRead1.ReadToEnd
-
oRead1.Close() 'test line
-
End If
-
Dim b As String = "2008" 'vbCrLf
-
'find date
-
date1 = EntireLine1.Substring(EntireLine1.IndexOf("DATE"), EntireLine1.IndexOf(b)) ', EntireLine1.IndexOf("2008"))
-
-
'function cuts of the end of the line where it finds a return character or line feed...
-
For i = 1 To Len(date1)
-
j = Mid(date1, i, 1)
-
If a = vbCr Or a = vbLf Then
-
Exit For
-
End If
-
Next
-
date1 = Mid(date1, 6, i - 7)
-
-
'rename File
-
If System.IO.File.Exists(a) = True Then
-
System.IO.File.Move(a, StartPath & "\" & RTrim(date1) & ".txt")
-
End If
-
-
Next
-
-
End Sub
-
End Class
-
Backup before you do a test run. -
' VBA
-
-
Sub Test()
-
Dim source_path As String
-
-
source_path = "C:\Documents and Settings\WinblowsME\Desktop\Temp"
-
-
Call Rename_Files(source_path)
-
End Sub
-
-
Private Sub Rename_Files(source_path As String)
-
Dim regex As Object, file_name As String, curr_date As String
-
-
Set regex = CreateObject("VBScript.RegExp")
-
-
regex.Pattern = "\\*$"
-
source_path = regex.Replace(source_path, "\")
-
-
file_name = Dir(source_path & "*.txt")
-
-
Do While file_name <> ""
-
curr_date = Get_Date(source_path & file_name)
-
-
If curr_date <> "" Then
-
Debug.Print source_path & file_name & "->" & source_path & curr_date & ".txt"
-
-
'Name source_path & file_name As source_path & curr_date & ".txt"
-
End If
-
-
file_name = Dir
-
Loop
-
End Sub
-
-
Private Function Get_Date(file_name As String) As String
-
Dim regex As Object, curr_line As String, curr_date As String
-
-
Set regex = CreateObject("VBScript.RegExp")
-
-
regex.Pattern = "^ *DATE *[0-9]{1,2} *.* *[0-9]{4}"
-
curr_line = ""
-
curr_date = ""
-
-
Open file_name For Input As #1
-
Do While Not EOF(1)
-
Line Input #1, curr_line
-
-
If regex.Test(curr_line) Then
-
curr_date = curr_line
-
curr_date = Replace(curr_line, "DATE ", "")
-
curr_date = Replace(curr_line, " ", "")
-
Exit Do
-
End If
-
Loop
-
Close #1
-
-
Get_Date = curr_date
-
End Function
You can follow these steps. ...
- Far too wasteful to copy everything - just rename them. (What if the files are huge? What if you don't have enough space to copy?)
- You do not have to use FSO, there are a number of methods which can be used.
Alexandra, I don't think you told us what version of VB you're using.
Winnie, pretty sure you've got a bug in lines 48-50.
Winnie, pretty sure you've got a bug in lines 48-50.
Yes, curr_line on lines 49 and 50 should be curr_date (since curr_date became curr_line on line 48). Line 49 removes "DATE ", then line 50 removes any spaces, probably in case there was a space before "DATE " which he doesn't want to exist.
What sounds dodgy to me though is this removal of any and all spaces; what if there are meant to be spaces? If you want to remove only the space to the left of "DATE ", you could use Mid() to see if it's a space, and then combine 2 Mid()s to piece the first and second half of the line (of the text file) together, on either side of the space which you want to leave out.
Sorry, lines 49 and 50 should be curr_date. It was a last second change that I've overlooked.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: BjoernJackschina |
last post by:
Hello,
I just look for a capability to sort several words in view of
alphabet.
An example:
stop is 'opst'
reach is 'aechr'
This should read in a new file so that I can look for same letter...
|
by: Michael Schindler |
last post by:
Ich habe es nun erreicht, ein textfile einzulesen das nun zeile für zeile im
datagrid angezeigt wird.
Nun wäre es toll wenn ich das textfile zum beispiel nach jedem blank in eine
spalte...
|
by: Saradhi |
last post by:
Hi All,
Here I am facing a performance problem with the TreeView Node renaming.
I am displaying a hierarchy Data in a treeview in my Windows C# Application.
My tree view represents an...
|
by: chris |
last post by:
Hi there,
I am reading in a textfile which looks like this (there is no new line after
the last number)
03 98661881
0407 566453
The code to load the textfile looks like this:
|
by: Peter Proost |
last post by:
Hi, I'm creating an import module to read data from old textfiles, run some
calculations on them and save them to sql server. I've figured out how to do
a select on textfile using a schema.ini file...
| |
by: Rain |
last post by:
I have a textfile with multiple line. How do i get (for example) the 1st line
or the 3rd line then change the line to something i want in C#? How do i
delete a line in C#... or backspace in a...
|
by: lumo2000 |
last post by:
hello ng!
i wrote a little application in php which searches along a webpage for
a latin animal name and returns me the "normal" name.
so all i need to do now is:
o - load a textfile and read...
|
by: Arn |
last post by:
I just started to learn C++ and have some problem when I read a hole line from a textfile. I would be grateful if anyone can tell me what is wrong with my code.
I'm using Borland Developer Studio...
|
by: asedt |
last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile.
My problem is that i don't know how to...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |