473,898 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.OpenTextF ile(Server.MapP ath(zfil),1,tru e)
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.OpenTextF ile(Server.MapP ath(zfil),1,tru e)
if nq.AtEndOfStrea m=True Then
zfil=custompath &"customdatadef ault.asp"
set nq=ns.OpenTextF ile(Server.MapP ath(zfil),1,tru e)
end if

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

John

Jan 30 '06 #1
2 2466
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(s erver.mappath(z fil)) 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.OpenTextF ile(Server.MapP ath(zfil),1,tru e)
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.OpenTextF ile(Server.MapP ath(zfil),1,tru e)
if nq.AtEndOfStrea m=True Then
zfil=custompath &"customdatadef ault.asp"
set nq=ns.OpenTextF ile(Server.MapP ath(zfil),1,tru e)
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.OpenTextF ile(Server.MapP ath(zfil),1,tru e)

It should have been

set nq=ns.OpenTextF ile(Server.MapP ath(zfil),1,fal se)

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
7714
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 alternatives? Additionally, might there be cross-platform alternatives, say in a library like Boost, or something else? -Matt
4
2435
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 and when the main/first page displays, I'll look for the cookie and open the page it names. If a new version of the site omits a page that used to exist or if the cookie somehow becomes corrupt, I don't want to display an error when I try to...
4
1679
by: puzzlecracker | last post by:
I am using cpp by the way? Thanks
2
5371
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 Database Public Function adhFileExists(strName As String) As Boolean On Error Resume Next
6
13630
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 just making it to ensure that it's there. Thanks. Hector
5
3317
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
8047
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
4171
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 String.Format("~/images/categories/{0}", sFileName); else if(File.Exists(String.Format("~/images/products/{0}", sFileName)) return String.Format("~/images/products/{0}", sFileName); else return "";
0
3602
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 XY=./abc/cde:./bgh/nkl # check if path defined in $jXY exists IFS=: for i in $XY do if
0
10857
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10948
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5880
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4706
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3305
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.