Solved

Status Of A file

Posted on 2001-08-03
6
145 Views
Last Modified: 2010-05-02
Hi, Is there anyway of telling if a file exists? I.E

if "c:\windows\desktop\newfile.text" = "" then
.....
else
.....

Thanks.
0
Comment
Question by:elabiv
6 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 6347888
Set a reference to Microsoft Scripting Runtime

Dim fso as Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
If(fso.FileExists("c:\windows\desktop\newfile.txt")) Then
  ..
Else
  ..
End If

or instead of the first 2 lines you can use:
Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")

regards,
CJ
0
 
LVL 6

Expert Comment

by:Nitin Sontakke
ID: 6347910
You can also, use:

If Dir("c:\windows\desktop\newfile.text") <> "" Then
   '...
End If
0
 
LVL 6

Accepted Solution

by:
Nitin Sontakke earned 15 total points
ID: 6347913
You can also, use:

If Dir("c:\windows\desktop\newfile.text") <> "" Then
   '--File exists code goes here...
Else
   '--File doesn't exist code goes here...  
End If
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:elabiv
ID: 6347939
My code looks like this;

If Dir(Form1.Text3 & text2.text) <> "" Then
Open Form1.Text3 & text2.text For Append As #newfile
Print #newfile, ; "This is appending"
Close #newfile

Else

Open Form1.Text3 & text2.text For Append As #newfile
Print #newfile, ; "This is writing from scratch"
Close #newfile

But I keep getting an error that says

"File Already Open"

Any ideas?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6347954
Are you assigning #newfile = freefile() and ensuring that you close each file handle before attempting the next use thereof. "File Already Open" sounds to me more that it is a problem with the file handles than the file itself.
0
 

Author Comment

by:elabiv
ID: 6347964
Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Write data from SQL to CSV file - position 1 53
Adding to a VBA? 6 63
Modifying Conditional Format from VBA code 3 49
Problem to With line 4 43
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now