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

FileSystemObject problem...

Status
Not open for further replies.

dbtetlow

Programmer
Mar 21, 2001
5
US
Hi all,

I am having trouble using text files where I need to create/access a file in which the name resides in a variable. For example:

Set outFile = fso.CreateTextFile("d:\log0520.txt")

works fine, but

dim logName As String
.
.
logName = ("d:\log" + CStr(date4) + ".txt")
.
.
Set outFile = fso.CreateTextFile(logName)

gives an error : runtime error '13'/Type Mismatch

any help would be greatly appreciated.

Thanks
 
Try this:

logName = ("d:\log" & CStr(CDate(date4)) & ".txt")
 
thanks 1127, but that's not it, the problem seems to be if I use a string variable name in fso.createfile(xxx) I get the error...

any ideas?
 
One thing you may want to look at is the date4 variable. How is this date formatted? If it has slashes in it, then you may have a problem.

Secondly, how is outfile dimmed?
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Using a string variable should not cause a problem. My guess is that the date formatting may be using something that isn't a valid file name character. Have you checked the exact contents of the logName variable in the debugger before it's passed to CreateTextFile? If it's using a long date/time format, then the colons in the time, for example, could cause a problem. Make sure that Cstr(date4) contains only letters and numbers.
 
Hi,

Thanks for the suggestions...still no luck. date4 is a 4 digit int in the format mmdd (ie 0520). There are no other characters. outfile is dimmed as File
 
I tried the following code:

Dim Logfile As String
Logfile = "C:\testfile" + CStr(0520) + ".txt"
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(Logfile, True)
MyFile.WriteLine ("This is a test.")
MyFile.Close

,and it works just fine. Note that the CStr function will trim any leading 0's off of the date, so that a date of 0520 will come out as just 520. Otherwise it created the file just fine, as "testfile520.txt"

Robert
 
The problem is in the definition of outfile.

The CreateTextFile returns a TextStream object, not a file.

Change dim outfile as file to dim outfile as object or dim outfile as textstrem

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
My experience is that there may be no problem with
calling either function, CStr() and CDate(), SEPARATELY -
but when you combine them into a single line, they can
mess up the compiler.

My solution:
1) On one line, call CDate() and set it equal to
a variable, then
2) On a second line, call CStr() to convert that
variable into a string.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top