|
 |
asp_web_howto thread: If file exists
Message #1 by "Matthew Lohr" <mlohr@t...> on Mon, 16 Apr 2001 10:41:10 -0400
|
|
I have a page that displays links and some information about the file that
is linked to. This totally depends on someone else uploading a file
correctly to the proper directory. I have used the File Sytem Object to get
the date of ths file to show when it was last updated. Recently I have had
a problem where this file was deleted by the person responsible for it. I
need to know how I can check to see if this file exists before I display the
link (so I can just omit it or state report is unavailable) How can I
implement that into the following code.
<%
' Start of FSO code to get dates
' The following code is repeated throughout the rest of the page and will be
explained only once
' This code is used to get the date the file denoted in the hyperlink was
last updated
set objFSO= server.CreateObject("scripting.FileSystemObject") ' sets the
filesystemobject
PathToFile="C:\inetpub\wwwroot\intranet\admin\reports\currentmonth1.pdf" '
this is the path to the file listed in the hyperlink
If objFSO.GetFile(PathToFile) <> "" then
set TheFile= objFSO.GetFile(PathToFile)
' sets a variable for the file
' The next lines are html/javascript to display hyperlink and text popup
description
%>
<A href="currentmonth.pdf" onmouseout=kill() onmouseover="popup('Current
Calendar month report.','lightgreen')" target=new ;>Current Month</A>
</TD><TD>
<%
dte = thefile.datelastmodified ' variable set to modifed dated of the file
i = instr(dte , " ") ' looks for space in variable as datelastmodified is
displayed as mm/dd/yyyy hh:mm:ss(am/pm)
dte = left(dte, i) ' we only want the date so we pick everything to the left
of the space
response.write dte & "<BR>" & vbcrlf ' this writes the variable dte which
contains properly formatted date
Else
set TheFile=nothing ' close variable
set objFSO=nothing ' close filesystemobject
' End of code to get dates
End if
%>
Message #2 by "Matthew Lohr" <mlohr@t...> on Mon, 16 Apr 2001 15:36:14 -0400
|
|
I fixed this problem but I have now confused myself on the server.execute
command. Is it not supposed to pass variables autmatically to the next
page? My variable have no value in the server.execute page but they pass
continuall through the original page
-----Original Message-----
From: Matthew Lohr [mailto:mlohr@t...]
Sent: Monday, April 16, 2001 10:41 AM
To: ASP Web HowTo
Subject: [asp_web_howto] If file exists
I have a page that displays links and some information about the file that
is linked to. This totally depends on someone else uploading a file
correctly to the proper directory. I have used the File Sytem Object to get
the date of ths file to show when it was last updated. Recently I have had
a problem where this file was deleted by the person responsible for it. I
need to know how I can check to see if this file exists before I display the
link (so I can just omit it or state report is unavailable) How can I
implement that into the following code.
<%
' Start of FSO code to get dates
' The following code is repeated throughout the rest of the page and will be
explained only once
' This code is used to get the date the file denoted in the hyperlink was
last updated
set objFSO= server.CreateObject("scripting.FileSystemObject") ' sets the
filesystemobject
PathToFile="C:\inetpub\wwwroot\intranet\admin\reports\currentmonth1.pdf" '
this is the path to the file listed in the hyperlink
If objFSO.GetFile(PathToFile) <> "" then
set TheFile= objFSO.GetFile(PathToFile)
' sets a variable for the file
' The next lines are html/javascript to display hyperlink and text popup
description
%>
<A href="currentmonth.pdf" onmouseout=kill() onmouseover="popup('Current
Calendar month report.','lightgreen')" target=new ;>Current Month</A>
</TD><TD>
<%
dte = thefile.datelastmodified ' variable set to modifed dated of the file
i = instr(dte , " ") ' looks for space in variable as datelastmodified is
displayed as mm/dd/yyyy hh:mm:ss(am/pm)
dte = left(dte, i) ' we only want the date so we pick everything to the left
of the space
response.write dte & "<BR>" & vbcrlf ' this writes the variable dte which
contains properly formatted date
Else
set TheFile=nothing ' close variable
set objFSO=nothing ' close filesystemobject
' End of code to get dates
End if
%>
Message #3 by "Peter Lanoie" <planoie@e...> on Tue, 17 Apr 2001 08:17:12 -0400
|
|
Matt,
Looks like you understand it correctly.
With Server.Execute and Server.Transfer, the executed or transferred to file
has access to the original request's HTTP headers, which include and POST
and GET information. However, it seems that script scope variables do not
carry over in the way they do with a #include. It kind of makes sense given
what's actually happening. When you use a #include, you are just taking the
script from that included file and placing it in-line with the calling
script, so in essence, you end up with one big script that then executes.
The 2 server methods are different in this respect. I just tested this and
report these results with some dismay. It will certainly lead me to rethink
a couple of things. :(
Peter
-----Original Message-----
From: Matthew Lohr [mailto:mlohr@t...]
Sent: Monday, April 16, 2001 3:36 PM
To: ASP Web HowTo
Subject: [asp_web_howto] RE: If file exists
I fixed this problem but I have now confused myself on the server.execute
command. Is it not supposed to pass variables autmatically to the next
page? My variable have no value in the server.execute page but they pass
continuall through the original page
-----Original Message-----
From: Matthew Lohr [mailto:mlohr@t...]
Sent: Monday, April 16, 2001 10:41 AM
To: ASP Web HowTo
Subject: [asp_web_howto] If file exists
I have a page that displays links and some information about the file that
is linked to. This totally depends on someone else uploading a file
correctly to the proper directory. I have used the File Sytem Object to get
the date of ths file to show when it was last updated. Recently I have had
a problem where this file was deleted by the person responsible for it. I
need to know how I can check to see if this file exists before I display the
link (so I can just omit it or state report is unavailable) How can I
implement that into the following code.
<%
' Start of FSO code to get dates
' The following code is repeated throughout the rest of the page and will be
explained only once
' This code is used to get the date the file denoted in the hyperlink was
last updated
set objFSO= server.CreateObject("scripting.FileSystemObject") ' sets the
filesystemobject
PathToFile="C:\inetpub\wwwroot\intranet\admin\reports\currentmonth1.pdf" '
this is the path to the file listed in the hyperlink
If objFSO.GetFile(PathToFile) <> "" then
set TheFile= objFSO.GetFile(PathToFile)
' sets a variable for the file
' The next lines are html/javascript to display hyperlink and text popup
description
%>
<A href="currentmonth.pdf" onmouseout=kill() onmouseover="popup('Current
Calendar month report.','lightgreen')" target=new ;>Current Month</A>
</TD><TD>
<%
dte = thefile.datelastmodified ' variable set to modifed dated of the file
i = instr(dte , " ") ' looks for space in variable as datelastmodified is
displayed as mm/dd/yyyy hh:mm:ss(am/pm)
dte = left(dte, i) ' we only want the date so we pick everything to the left
of the space
response.write dte & "<BR>" & vbcrlf ' this writes the variable dte which
contains properly formatted date
Else
set TheFile=nothing ' close variable
set objFSO=nothing ' close filesystemobject
' End of code to get dates
End if
%>
Message #4 by "Matthew Lohr" <mlohr@t...> on Tue, 17 Apr 2001 09:09:45 -0400
|
|
well what I am trying to do is this. I use the file system object to
display the last updated time of a file. If this file does not exist (it
was renamed, moved or deleted) then I skip displaying the link to this file.
I then want the script to email me and tell me that there is something
wrong. I did not want to have this code in the page 9 times (the number of
reports and it will grow) so I made a seperate page that I was calling with
server.execute. I guess I could write a function to do this but alas I have
not advanced that far yet. If I have to write it all out for each report it
would look like the code that follows. If i knew how to write a function I
could just pass my variable PathToFile in to each one or is that not
correct.
set objFSO= server.CreateObject("scripting.FileSystemObject") ' sets the
filesystemobject
PathToFile= ("currentmonth.pdf") this is the file listed in the hyperlink
set TheFile = objFSO.GetFile(server.MapPath(PathToFile))
If err.number = 0 Then
%>
<A href="<%=(PathtoFile)%>" onmouseout=kill() onmouseover="popup('Prior
Calendar month report.','lightgreen')" target=new ;>Previous Month</A>
</TD><TD>
<%
dte = thefile.datelastmodified
i = instr(dte , " ")
dte = left(dte, i)
response.write dte & "<BR>" & vbcrlf '"<a href=""newbusmth.pdf"">Month to
Date</a>
Else
Dim myMail 'Variable for the mail object
Dim strmsgbody
strmsgbody = "The following report(s) are missing from the Intranet:" &
vbCrLf & tempa
Set myMail = Server.CreateObject("CDONTS.NewMail")
myMail.Subject = "Missing Reports" ' The Subject of the message
myMail.Body = strmsgBody 'The body of the message
myMail.To = "mlohr@t..." 'Where the message is going
myMail.From = "reportscreen@t..." ' Who the message is from
myMail.Send 'sends the email
set TheFile=nothing
set objFSO=nothing
End if
err.number = 0
-----Original Message-----
From: Peter Lanoie [mailto:planoie@e...]
Sent: Tuesday, April 17, 2001 8:17 AM
To: ASP Web HowTo
Subject: [asp_web_howto] RE: If file exists
Matt,
Looks like you understand it correctly.
With Server.Execute and Server.Transfer, the executed or transferred to file
has access to the original request's HTTP headers, which include and POST
and GET information. However, it seems that script scope variables do not
carry over in the way they do with a #include. It kind of makes sense given
what's actually happening. When you use a #include, you are just taking the
script from that included file and placing it in-line with the calling
script, so in essence, you end up with one big script that then executes.
The 2 server methods are different in this respect. I just tested this and
report these results with some dismay. It will certainly lead me to rethink
a couple of things. :(
Peter
-----Original Message-----
From: Matthew Lohr [mailto:mlohr@t...]
Sent: Monday, April 16, 2001 3:36 PM
To: ASP Web HowTo
Subject: [asp_web_howto] RE: If file exists
I fixed this problem but I have now confused myself on the server.execute
command. Is it not supposed to pass variables autmatically to the next
page? My variable have no value in the server.execute page but they pass
continuall through the original page
-----Original Message-----
From: Matthew Lohr [mailto:mlohr@t...]
Sent: Monday, April 16, 2001 10:41 AM
To: ASP Web HowTo
Subject: [asp_web_howto] If file exists
I have a page that displays links and some information about the file that
is linked to. This totally depends on someone else uploading a file
correctly to the proper directory. I have used the File Sytem Object to get
the date of ths file to show when it was last updated. Recently I have had
a problem where this file was deleted by the person responsible for it. I
need to know how I can check to see if this file exists before I display the
link (so I can just omit it or state report is unavailable) How can I
implement that into the following code.
<%
' Start of FSO code to get dates
' The following code is repeated throughout the rest of the page and will be
explained only once
' This code is used to get the date the file denoted in the hyperlink was
last updated
set objFSO= server.CreateObject("scripting.FileSystemObject") ' sets the
filesystemobject
PathToFile="C:\inetpub\wwwroot\intranet\admin\reports\currentmonth1.pdf" '
this is the path to the file listed in the hyperlink
If objFSO.GetFile(PathToFile) <> "" then
set TheFile= objFSO.GetFile(PathToFile)
' sets a variable for the file
' The next lines are html/javascript to display hyperlink and text popup
description
%>
<A href="currentmonth.pdf" onmouseout=kill() onmouseover="popup('Current
Calendar month report.','lightgreen')" target=new ;>Current Month</A>
</TD><TD>
<%
dte = thefile.datelastmodified ' variable set to modifed dated of the file
i = instr(dte , " ") ' looks for space in variable as datelastmodified is
displayed as mm/dd/yyyy hh:mm:ss(am/pm)
dte = left(dte, i) ' we only want the date so we pick everything to the left
of the space
response.write dte & "<BR>" & vbcrlf ' this writes the variable dte which
contains properly formatted date
Else
set TheFile=nothing ' close variable
set objFSO=nothing ' close filesystemobject
' End of code to get dates
End if
%>
Message #5 by "TomMallard" <mallard@s...> on Tue, 17 Apr 2001 07:10:08 -0700
|
|
function checkFile(strMsgBody)
Dim myMail 'Variable for the mail object
Dim strmsgbody
Set myMail = Server.CreateObject("CDONTS.NewMail")
myMail.Subject = "Missing Reports" ' The Subject of the message
myMail.Body = strMsgBody 'The body of the message
myMail.To = "mlohr@t..." 'Where the message is going
myMail.From = "reportscreen@t..." ' Who the message is from
myMail.Send 'sends the email
checkFile = true
end function
I'd move the string outside of the function and pass it in because it's
getting data not available within the function itself...then after the else
(when error > 0)...
strmsgbody = "The following report(s) are missing from the Intranet:" &
vbCrLf & tempa
checkFile(strmsgbody)
Using the function is more appropriate, a lot of times it's also better to
make an include file with the name exactly the same as the function name. By
setting the value of the function to true, you can test if it succeeded or
not...
HTH,
tom
----- Original Message -----
From: "Matthew Lohr" <mlohr@t...>
To: "ASP Web HowTo" <asp_web_howto@p...>
Sent: Tuesday, April 17, 2001 6:09 AM
Subject: [asp_web_howto] RE: If file exists
> well what I am trying to do is this. I use the file system object to
> display the last updated time of a file. If this file does not exist (it
> was renamed, moved or deleted) then I skip displaying the link to this
file.
> I then want the script to email me and tell me that there is something
> wrong. I did not want to have this code in the page 9 times (the number
of
> reports and it will grow) so I made a seperate page that I was calling
with
> server.execute. I guess I could write a function to do this but alas I
have
> not advanced that far yet. If I have to write it all out for each report
it
> would look like the code that follows. If i knew how to write a function
I
> could just pass my variable PathToFile in to each one or is that not
> correct.
>
> set objFSO= server.CreateObject("scripting.FileSystemObject") ' sets the
> filesystemobject
> PathToFile= ("currentmonth.pdf") this is the file listed in the hyperlink
> set TheFile = objFSO.GetFile(server.MapPath(PathToFile))
> If err.number = 0 Then
> %>
> <A href="<%=(PathtoFile)%>" onmouseout=kill() onmouseover="popup('Prior
> Calendar month report.','lightgreen')" target=new ;>Previous Month</A>
> </TD><TD>
> <%
> dte = thefile.datelastmodified
> i = instr(dte , " ")
> dte = left(dte, i)
> response.write dte & "<BR>" & vbcrlf '"<a href=""newbusmth.pdf"">Month
to
> Date</a>
> Else
> Dim myMail 'Variable for the mail object
> Dim strmsgbody
> strmsgbody = "The following report(s) are missing from the Intranet:" &
> vbCrLf & tempa
> Set myMail = Server.CreateObject("CDONTS.NewMail")
> myMail.Subject = "Missing Reports" ' The Subject of the message
> myMail.Body = strmsgBody 'The body of the message
> myMail.To = "mlohr@t..." 'Where the message is going
> myMail.From = "reportscreen@t..." ' Who the message is from
> myMail.Send 'sends the email
> set TheFile=nothing
> set objFSO=nothing
> End if
> err.number = 0
>
> -----Original Message-----
> From: Peter Lanoie [mailto:planoie@e...]
> Sent: Tuesday, April 17, 2001 8:17 AM
> To: ASP Web HowTo
> Subject: [asp_web_howto] RE: If file exists
>
>
> Matt,
>
> Looks like you understand it correctly.
>
> With Server.Execute and Server.Transfer, the executed or transferred to
file
> has access to the original request's HTTP headers, which include and POST
> and GET information. However, it seems that script scope variables do not
> carry over in the way they do with a #include. It kind of makes sense
given
> what's actually happening. When you use a #include, you are just taking
the
> script from that included file and placing it in-line with the calling
> script, so in essence, you end up with one big script that then executes.
> The 2 server methods are different in this respect. I just tested this
and
> report these results with some dismay. It will certainly lead me to
rethink
> a couple of things. :(
>
> Peter
>
> -----Original Message-----
> From: Matthew Lohr [mailto:mlohr@t...]
> Sent: Monday, April 16, 2001 3:36 PM
> To: ASP Web HowTo
> Subject: [asp_web_howto] RE: If file exists
>
>
> I fixed this problem but I have now confused myself on the server.execute
> command. Is it not supposed to pass variables autmatically to the next
> page? My variable have no value in the server.execute page but they pass
> continuall through the original page
>
> -----Original Message-----
> From: Matthew Lohr [mailto:mlohr@t...]
> Sent: Monday, April 16, 2001 10:41 AM
> To: ASP Web HowTo
> Subject: [asp_web_howto] If file exists
>
>
> I have a page that displays links and some information about the file that
> is linked to. This totally depends on someone else uploading a file
> correctly to the proper directory. I have used the File Sytem Object to
get
> the date of ths file to show when it was last updated. Recently I have
had
> a problem where this file was deleted by the person responsible for it. I
> need to know how I can check to see if this file exists before I display
the
> link (so I can just omit it or state report is unavailable) How can I
> implement that into the following code.
>
> <%
> ' Start of FSO code to get dates
> ' The following code is repeated throughout the rest of the page and will
be
> explained only once
> ' This code is used to get the date the file denoted in the hyperlink was
> last updated
> set objFSO= server.CreateObject("scripting.FileSystemObject") ' sets the
> filesystemobject
> PathToFile="C:\inetpub\wwwroot\intranet\admin\reports\currentmonth1.pdf" '
> this is the path to the file listed in the hyperlink
> If objFSO.GetFile(PathToFile) <> "" then
> set TheFile= objFSO.GetFile(PathToFile)
> ' sets a variable for the file
> ' The next lines are html/javascript to display hyperlink and text popup
> description
> %>
> <A href="currentmonth.pdf" onmouseout=kill() onmouseover="popup('Current
> Calendar month report.','lightgreen')" target=new ;>Current Month</A>
> </TD><TD>
> <%
> dte = thefile.datelastmodified ' variable set to modifed dated of the file
> i = instr(dte , " ") ' looks for space in variable as datelastmodified is
> displayed as mm/dd/yyyy hh:mm:ss(am/pm)
> dte = left(dte, i) ' we only want the date so we pick everything to the
left
> of the space
> response.write dte & "<BR>" & vbcrlf ' this writes the variable dte
which
> contains properly formatted date
> Else
> set TheFile=nothing ' close variable
> set objFSO=nothing ' close filesystemobject
> ' End of code to get dates
> End if
> %>
>
Message #6 by Kyle Burns <kburns@c...> on Tue, 17 Apr 2001 10:48:24 -0500
|
|
You can avoid generating an exception at all by using the FileExists method
to test for the existance of the file. Its syntax is as follows:
If ojFSO.FileExists(server.MapPath(PathToFile)) Then
'Process
End If
>> -----Original Message-----
>> From: Matthew Lohr [mailto:mlohr@t...]
>> Sent: Tuesday, April 17, 2001 8:10 AM
>> To: ASP Web HowTo
>> Subject: [asp_web_howto] RE: If file exists
>>
>>
>> well what I am trying to do is this. I use the file system object to
>> display the last updated time of a file. If this file does
>> not exist (it
>> was renamed, moved or deleted) then I skip displaying the
>> link to this file.
>> I then want the script to email me and tell me that there is
>> something
>> wrong. I did not want to have this code in the page 9 times
>> (the number of
>> reports and it will grow) so I made a seperate page that I
>> was calling with
>> server.execute. I guess I could write a function to do this
>> but alas I have
>> not advanced that far yet. If I have to write it all out
>> for each report it
>> would look like the code that follows. If i knew how to
>> write a function I
>> could just pass my variable PathToFile in to each one or is that not
>> correct.
>>
>> set objFSO=
>> server.CreateObject("scripting.FileSystemObject") ' sets the
>> filesystemobject
>> PathToFile= ("currentmonth.pdf") this is the file listed in
>> the hyperlink
>> set TheFile = objFSO.GetFile(server.MapPath(PathToFile))
>> If err.number = 0 Then
>> %>
>> <A href="<%=(PathtoFile)%>" onmouseout=kill()
>> onmouseover="popup('Prior
>> Calendar month report.','lightgreen')" target=new
>> ;>Previous Month</A>
>> </TD><TD>
>> <%
>> dte = thefile.datelastmodified
>> i = instr(dte , " ")
>> dte = left(dte, i)
>> response.write dte & "<BR>" & vbcrlf '"<a
>> href=""newbusmth.pdf"">Month to
>> Date</a>
>> Else
>> Dim myMail 'Variable for the mail object
>> Dim strmsgbody
>> strmsgbody = "The following report(s) are missing from the
>> Intranet:" &
>> vbCrLf & tempa
>> Set myMail = Server.CreateObject("CDONTS.NewMail")
>> myMail.Subject = "Missing Reports" ' The Subject of the message
>> myMail.Body = strmsgBody 'The body of the message
>> myMail.To = "mlohr@t..." 'Where the message is going
>> myMail.From = "reportscreen@t..." ' Who the message is from
>> myMail.Send 'sends the email
>> set TheFile=nothing
>> set objFSO=nothing
>> End if
>> err.number = 0
>>
>> -----Original Message-----
>> From: Peter Lanoie [mailto:planoie@e...]
>> Sent: Tuesday, April 17, 2001 8:17 AM
>> To: ASP Web HowTo
>> Subject: [asp_web_howto] RE: If file exists
>>
>>
>> Matt,
>>
>> Looks like you understand it correctly.
>>
>> With Server.Execute and Server.Transfer, the executed or
>> transferred to file
>> has access to the original request's HTTP headers, which
>> include and POST
>> and GET information. However, it seems that script scope
>> variables do not
>> carry over in the way they do with a #include. It kind of
>> makes sense given
>> what's actually happening. When you use a #include, you are
>> just taking the
>> script from that included file and placing it in-line with
>> the calling
>> script, so in essence, you end up with one big script that
>> then executes.
>> The 2 server methods are different in this respect. I just
>> tested this and
>> report these results with some dismay. It will certainly
>> lead me to rethink
>> a couple of things. :(
>>
>> Peter
>>
>> -----Original Message-----
>> From: Matthew Lohr [mailto:mlohr@t...]
>> Sent: Monday, April 16, 2001 3:36 PM
>> To: ASP Web HowTo
>> Subject: [asp_web_howto] RE: If file exists
>>
>>
>> I fixed this problem but I have now confused myself on the
>> server.execute
>> command. Is it not supposed to pass variables autmatically
>> to the next
>> page? My variable have no value in the server.execute page
>> but they pass
>> continuall through the original page
>>
>> -----Original Message-----
>> From: Matthew Lohr [mailto:mlohr@t...]
>> Sent: Monday, April 16, 2001 10:41 AM
>> To: ASP Web HowTo
>> Subject: [asp_web_howto] If file exists
>>
>>
>> I have a page that displays links and some information about
>> the file that
>> is linked to. This totally depends on someone else uploading a file
>> correctly to the proper directory. I have used the File
>> Sytem Object to get
>> the date of ths file to show when it was last updated.
>> Recently I have had
>> a problem where this file was deleted by the person
>> responsible for it. I
>> need to know how I can check to see if this file exists
>> before I display the
>> link (so I can just omit it or state report is unavailable)
>> How can I
>> implement that into the following code.
>>
>> <%
>> ' Start of FSO code to get dates
>> ' The following code is repeated throughout the rest of the
>> page and will be
>> explained only once
>> ' This code is used to get the date the file denoted in the
>> hyperlink was
>> last updated
>> set objFSO=
>> server.CreateObject("scripting.FileSystemObject") ' sets the
>> filesystemobject
>> PathToFile="C:\inetpub\wwwroot\intranet\admin\reports\current
>> month1.pdf" '
>> this is the path to the file listed in the hyperlink
>> If objFSO.GetFile(PathToFile) <> "" then
>> set TheFile= objFSO.GetFile(PathToFile)
>> ' sets a variable for the file
>> ' The next lines are html/javascript to display hyperlink
>> and text popup
>> description
>> %>
>> <A href="currentmonth.pdf" onmouseout=kill()
>> onmouseover="popup('Current
>> Calendar month report.','lightgreen')" target=new ;>Current
>> Month</A>
>> </TD><TD>
>> <%
>> dte = thefile.datelastmodified ' variable set to modifed
>> dated of the file
>> i = instr(dte , " ") ' looks for space in variable as
>> datelastmodified is
>> displayed as mm/dd/yyyy hh:mm:ss(am/pm)
>> dte = left(dte, i) ' we only want the date so we pick
>> everything to the left
>> of the space
>> response.write dte & "<BR>" & vbcrlf ' this writes the
>> variable dte which
>> contains properly formatted date
>> Else
>> set TheFile=nothing ' close variable
>> set objFSO=nothing ' close filesystemobject
>> ' End of code to get dates
>> End if
>> %>
>
|
|
 |