Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert files using vb

Status
Not open for further replies.

thewishla

MIS
Jan 17, 2002
12
GB
I would like to open a .txt file and save it with the same name but with an .xml extension. I think that that bit is fairly straightforwad but what I can't do is to do this for all the .txt files in a directory/folder. Please help.
 
One way to process all of the files in a directory (which I believe you are trying to do), is to use the dir function to create a text file which lists all files you require. Then use this newly created text file to open & process the correct files:

sub Process Files
Dim strFileName as String

Open "c:\list1.txt" For Input As #1

Do
Line Input #1, strFileName
'Add processing code here

Loop While Not EOF(1)
Close #1

HTH James Goodman
 
sorry this is above my level any chance you could simplify or refer me somewhere else.

tw
 
When you say:
'open a .txt file and save it with the same name but with an .xml extension'

Do you mean convert it from text to xml, or simlpy rename it?

James Goodman
 
rename it but because there are lots of files I dont want to do it manually
 
In that case the process should be quite simple.

Perhaps the quickest way of creating the list is to goto the dos prompt, & use the Dir command as follows. I generally navigate through to the source folder from the command prompt, rather than creating a more complicated Dir command.
e.g.
I want a list of files in c:\James\TextFilesI would open a command prompt, then use the cd command to navigate to c:\James\TextFiles\:

cd james\textfiles

At this point I would run the following Dir command:

Dir *.txt /b >c:\list1.txt


This should create a text file, (list1.txt).
You can then use the following sub to rename all of the files listed in the list1.txt file:
(Be sure to change the const declaration to your folder name)


Sub ProcessFiles()
On Error GoTo Process_Err
Dim strFileName As String
Dim pos As Integer, dFileName As String
Const SourceDir As String = "c:\James\TextFiles\"

'Open the list file
Open "c:\list1.txt" For Input As #1

'Setup a loop
Do
Line Input #1, strFileName
'Get the position of the file extension (only where it is .txt)
pos = InStr(strFileName, ".txt")
'If the file is a .txt file, process it
If pos > 0 Then
'Construct the new filename
dFileName = Left(strFileName, pos - 1) & ".xml"
'Rename the old file
Name (SourceDir & strFileName) As (SourceDir & dFileName)
Else
'Perhaps not a text file, do nothing
End If

Loop While Not EOF(1)
Close #1


Process_Exit:
Exit Sub

Process_Err:
MsgBox Error$
Resume Next

End Sub

James Goodman
 
I need to convert Word files to something and automaticaly put it on Access db. Any ideas??
Thanks!!!
 
porben,
you need to start a new thread if you want answers to this question. This thread is old and not really relevant to what you want to know.
When you start it, you will need to give more information too. What do you want to convert your word files to? What do you mean put it on a dB? Just load the filenames or copy data from a word file into your database?
A good question is one that has enough information to work out what you want without having to go back and ask for more information.
Remember a good question is more likely to get answered.

Ben ----------------------------------
Ben O'Hara
bo104@westyorkshire.police.uk
----------------------------------
 
how do i start a new thread on here?, I have done it b4 but forgotten, what a muppet
 
Go into your forum & at the top of the list of threads there is "Start A New Thread" or something similar.
It's not the most obvious link in the world, but it's there!

B ----------------------------------
Ben O'Hara
bo104@westyorkshire.police.uk
----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top