473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I check for the existence of a file?

Greetings,

I'm experimenting with an ASP page that reads data from a file name
that is passed to it as a parameter, as in this page, which works,
because the "good" file exists.

http://mazes.com/asp-maze/customized...&firstname=jwk
(make "your name is an amazing asp.general programmer" mazes.

But when I try this page, with the "bad" file which does not exist, I'm
not succeeding:
http://mazes.com/asp-maze/customized...&firstname=jwk

I've tried several solutions that I found by googling.

When I tried:
If (ns.FileExists(zfil)<>true) then ...

but even when I give it a valid name, it returns "False"

I've tried using
On Error Resume Next
set nq=ns.OpenTextFile(Server.MapPath(zfil),1,true)
if Err.Number <> 0 Then ...
On Error Goto 0

I have found a workaround. Here is what finally worked:

On Error Resume Next
set nq=ns.OpenTextFile(Server.MapPath(zfil),1,true)
if nq.AtEndOfStream=True Then
zfil=custompath&"customdatadefault.asp"
set nq=ns.OpenTextFile(Server.MapPath(zfil),1,true)
end if

But meanwhile, I'd love to hear how to check for the existence of a
file.

John

Jan 30 '06 #1
2 2440
You've got the key. You used MapPath to open the file. Why didn't you use it
to check for the file's existence?

If not ns.FileExists(server.mappath(zfil)) then

www.MessageMazes.com wrote:
Greetings,

I'm experimenting with an ASP page that reads data from a file name
that is passed to it as a parameter, as in this page, which works,
because the "good" file exists.

http://mazes.com/asp-maze/customized...&firstname=jwk
(make "your name is an amazing asp.general programmer" mazes.

But when I try this page, with the "bad" file which does not exist,
I'm not succeeding:
http://mazes.com/asp-maze/customized...&firstname=jwk

I've tried several solutions that I found by googling.

When I tried:
If (ns.FileExists(zfil)<>true) then ...

but even when I give it a valid name, it returns "False"

I've tried using
On Error Resume Next
set nq=ns.OpenTextFile(Server.MapPath(zfil),1,true)
if Err.Number <> 0 Then ...
On Error Goto 0

I have found a workaround. Here is what finally worked:

On Error Resume Next
set nq=ns.OpenTextFile(Server.MapPath(zfil),1,true)
if nq.AtEndOfStream=True Then
zfil=custompath&"customdatadefault.asp"
set nq=ns.OpenTextFile(Server.MapPath(zfil),1,true)
end if

But meanwhile, I'd love to hear how to check for the existence of a
file.

John


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jan 30 '06 #2
Thanks, I'll try that.

Just did. It worked, even though I thought it didn't for a second. Had
to read the results twice.

Meanwhile, I figured out why the statement didn't generate an error. I
had:

set nq=ns.OpenTextFile(Server.MapPath(zfil),1,true)

It should have been

set nq=ns.OpenTextFile(Server.MapPath(zfil),1,false)

I had told it to create the file if it didn't already exist, so of
course, it didn't generate an error. Now it does.

Thanks for all the help. This group is wonderful.

John

Jan 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Matt | last post by:
Hello, I see other references in this newsgroup saying that the only standard C++ way to test for file existence is some variant of my code below; can someone please confirm...or offer...
4
by: Christopher Nelson | last post by:
I have a multi-page site that I want to be able to resume at the same page if the user leaves and comes back later (say, days later). I'm thinking that I'll set a cookie as each page is displayed...
4
by: puzzlecracker | last post by:
I am using cpp by the way? Thanks
2
by: David | last post by:
I'm using following code for checking a file existence. I's working fine for given folder. Is there a way to check a file exitance in subfolders? Thanks in advance, David Option Compare...
6
by: hectorlas | last post by:
I wasn't sure if this should go in a linux group or the C group, but here I am. How do I check for directory existence in C? It's gcc under Linux to be exact, and I don't have the option of...
5
by: Gary Wessle | last post by:
hi or is there a better way to check the existence of "py" in a string "s"? string s = "djrfpyfd"; string t = "py"; string r = s.substr(s.find("py"),2); cout << (t==r) << endl;
10
by: Dieter Pelz | last post by:
Hallo, what is the best way to check the installation of mfc80 and vcrt sidebyside assemblies? Best Regards, Dieter Pelz
3
by: trint | last post by:
How can I do this with my c# code with my website(because the file is there, but the code doesn't return it)?: if(File.Exists(String.Format("~/images/categories/{0}", sFileName)) return...
0
by: MIRRA | last post by:
Hi I have a UNIX script which creates a report. In that script I have to define the java classpath. Also I need to check if the java classpath specified exists or not. Below is the command i use ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.