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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you copy a READ files name to a WRTIE file? 1

Status
Not open for further replies.

londonkiwi

Programmer
Sep 25, 2000
83
NZ
Well guys, this is more a common dialog question: YES, I have looked through the posted replies first!. Would appreciate your input: The code is below.

I want to pass the read files name eg: if the file is kiwi.eco, then the "kiwi" string, to sNewFile eg the file that is being written to, giving it a new extension eg write a file named kiwi.txt

SO, I open kiwi.eco to read from, and I open kiwi.txt to write to. Of course "kiwi" could be anything.

cheers cheers cheers


Private Sub procMC_Click()

Dim sLine As String
Dim FileNumWrite As Integer
Dim lData1, lData2, lData3, lData4 As String

Dim strFilter As String 'Common dialog filter string
Dim strFileName As String 'String of file to open
Dim FileHandleRead% ' Variable to hold file handle

strFilter = "MC500(*.eco)|*.eco|Text File(*.txt)|*.txt|All files(*.*)|*.*" 'Set the common dialog filter
cdMain.Filter = strFilter

cdMain.ShowOpen ' Open the common dialog

If cdMain.FileName <> &quot;&quot; Then ' Make sure the retrieved filename is not a blank string
strFileName = cdMain.FileName ' if it is not blank open the file
End If

Dim sNewFile As String
sNewFile = App.Path & &quot;\MC5500 Pre_Proc.txt&quot;
FileHandleRead% = FreeFile 'Get a free file handle and assign it to the file handle variable
Open strFileName For Input As #FileHandleRead% 'Open the file
FileNumWrite = FreeFile
Open sNewFile For Output As #FileNumWrite

Do Until UCase(Left(sLine, 2)) = &quot;HH&quot; Or EOF(FileHandleRead%) ' want to find the end of file
Line Input #FileHandleRead%, sLine
Loop

Screen.MousePointer = vbHourglass ' Change mouse pointer to hourglass.

Do Until EOF(FileHandleRead%) ' code here to process line of data

etc......................................


[sig][/sig]
 
When I had to do something similar for a previous project, I set up a loop that started at the back of the filename and moved forward until I found a dot, presuming that everything after the last dot in the filename is the extension. I just took everything in front of this dot as the name of the file.

Function GetFileName(strFileName as String) as String

Dim iCurrChar as integer

iCurrChar = Len(strFileName)
Do Until (Mid$(strFileName, iCurrChar, 1) = &quot;.&quot;)
iCurrChar = iCurrChar - 1

If (iCurrChar = 1) Then
Exit Do
End If
Loop

If (iCurrChar > 1) Then
GetFileName = Left$(strFileName, iCurrChar - 1)
Else
GetFileName = &quot;&quot;
End If

End Function

This will handle file names such as eat.at.joes.now -- the file name returned will be eat.at.joes

Hopefully this will do the trick.

Steve [sig][/sig]
 
Good, but as a newbie, how would you suggest I incorporate that into the code i.e. to set sNewFile as &quot;xyz.txt&quot;

sNewFile = App.Path & &quot;\MC5500 Pre_Proc.txt&quot;

thanks for the help in advance

lk
[sig][/sig]
 
In your function, after you have gotten the file name from the common dialogue, have something like

sNewFile = GetFileName(strFileName) & &quot;.txt&quot;

Or, you could do

Dim strShortFileName as String
strShortFileName = GetFileName(strFileName)

sNewFile = strShortFileName & &quot;.txt&quot;

The second way is longer, but easier to read. I think most people would prefer to see it this way.

Steve
[sig][/sig]
 
There is another way which is slightly volatile and prone to give errors...

If you ASSUME that the original file has a three character extension - which you can safely do if you remove the option 'All Files' from the dialog box - then you can set the new file name by:

sNewFileName = Left(sOldFileName, Len(sOldFileName) - 4) & &quot;.txt&quot; [sig]<p>Simon<br>[/sig]
 
Or, you could use the FileSystemObject to do some of the work for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top