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!

Calling for a input file which doesn't exist. 1

Status
Not open for further replies.

BadCook

Technical User
Sep 7, 2009
71
US
I have written a program that calls for a non existing (as yet) input text file as soon as it opens. later on it will save data to that sane file. But on opening i causes an error.
How can it establish the file on opening? This would happen only once the first time the program is used.
I wouldn't want to have the user manually set up the file.
 
assuming you are using fso to handle the file, use something like this before your ".OpenTextFile" method:
Code:
Dim fso As FileSystemObject, ts As TextStream
[COLOR=#4E9A06]'...
'...[/color]
If Not fso.FileExists(yourfile) Then
  Set ts = fso.CreateTextFile(yourfile, True, Unicode:=False)
  ts.Close
End If

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
You can do this if you use native to VB Open file:

Code:
Option Explicit
Private Const MY_FILE As String = "C:\Temp\SomeTextFile.txt"

Private Sub Command1_Click()

On Error GoTo MyErrHandlr
Open MY_FILE For Input As #1[green]
    'some code here[/green]
Close #1

Exit Sub
MyErrHandlr:

If Err.Number = 53 Then
    Open MY_FILE For Append As #3
    Close #3
    Resume Next
End If

End Sub

Have fun.

---- Andy
 
Yes, many ways to skin a cat. :)

faq222-3657

Swi
 
>assuming you are using fso

Well, if using FSO, then the capability being asked for already exists:

Code:
[blue]Dim fso As FileSystemObject
Dim ts As TextStream

Set fso = New FileSystemObject
Set ts = fso.OpenTextFile("c:\example.txt", ForAppending, True) [green]' creates the named file if it does not already exist, assuming that the path exists (which could be handled cleanly using SHCreateDirectory API)[/green][/blue]

In fact, here's a version of a function including the API call mentioned in the above example that returns an appendable textstream:

Code:
[blue]Private Declare Function SHCreateDirectoryEx Lib "shell32.dll" Alias "SHCreateDirectoryExA" (ByVal hwnd As Long, ByVal strPath As String, ByVal unused As Any) As Long

Public Function vbOpenTextFile(strPath As String) As TextStream
    Dim fso As FileSystemObject
    Dim ts As TextStream
    
    Set fso = New FileSystemObject
    
    SHCreateDirectoryEx 0&, fso.GetParentFolderName(strPath), 0& [green]' Should really check return value[/green]
    Set vbOpenTextFile = fso.OpenTextFile(strPath, ForAppending, True) [green]' creates the named file if it does not already exist[/green]

End Function[/blue]




 
MakeItSo,
I used yor suggstion and got an error message telling me that "fso as filesystemobjecy" is "Objectdefinedsystem Not Defined"
So I guess I dont fso, What is it and how can I get it?
BadCook
 
Make a reference to Microsoft Scripting Runtime.

Thanks.

Swi
 
Also, non-FSO alternatives have also been provided and you do not need a reference for them.

Thanks.

Swi
 
Hey guys
I found a solution that fits my needs.
Instead of trying to open an input file that doesn't exist, I open an append file.
It opens a file that the progtam uses just fine.
Thanks Andy, Swi, and Strongm for your interest.
 
>I open an append file

Which is (part of) what my solution offered ... ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top