ASP to Check if a file exists on an FTP server

ASP to Check if a file exists on an FTP server

am 26.04.2006 23:47:39 von nvanhaaster

Ok I have a script that is designed to access a FTP site to GET, PUT
and DELETE files. It is initiated by a page where the user enters in a
few variables that get stored in a database for future reporting. All
that aside what I am looking for is a simple way through ASP (vbscript)
to first loginto the FTP server and check if the files are there, then
give a message box back to the USER displaying the different types of
Files found. Here is an example of the message box:
===================
LogHours File : Found
NLHours File : Found
NYHours File : Not Found
HR Data File : Found
"OK" "CANCEL"
====================

The problem I am having is determining what i should be using, should i
be using my FTP script to log into the FTP site or can I use
Scripting.FileSystemobject. From what i know the
Scripting.FileSystemobject is only for local files.

So then comes what commands could i use to check if a file is on an FTP
Site?

Here is my code for the user input page. If you need more details
please let me know

<%
Dim Site
Dim isActive
Dim adoCon
Dim rsSite
Dim strSQL

Dim uname,fromemail,fromname
Dim rsPass
Dim pasSQL
uname = Request.Cookies("cookiename")("value")


Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &
Server.MapPath("/db.mdb")
Set rsSite = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM locations ORDER BY location;"
rsSite.Open strSQL, adoCon

%>






Nightly Reports







class=view>



Date: name="ndate" size="10" onchange=checkdate()>
Billable Hours type=text size=10 name=bhours value=0>
Goal Hours type=text size=10 name=ghours value=0>
Dialer Training Hours
Location: name=site>
<%
Do While Not rsSite.EOF
If rsSite("isOpen") = "True" Then
response.write ""
End If
rsSite.MoveNext
Loop
%>
onclick=Nextpage()> value=Reset>



The FTP Code that Has Been Modified From a webpost other places

'=====================
Function FTP( strCMD )
'=====================
Dim strFile, objTempFldr, objFile, objRegExp
Dim objShell, WSX, ReturnCode, Output, strLog, strErrorLog
Dim objFSO

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

set objTempFldr = objFSO.GetSpecialFolder( 2 )
strFile = objFSO.GetTempName

strFile = objTempFldr & "" & strFile & ".ftp"
if not objFSO.FileExists( strFile ) then objFSO.CreateTextFile( strFile
)
Set objFile = objFSO.OpenTextFile( strFile, 2, True )

objFile.WriteLine( strUser )
objFile.WriteLine( strPass )
If LocalDir <> "" Then objFile.WriteLine( "lcd " & LocalDir )
If RemoteDir <> "" Then objFile.WriteLine( "cd " & RemoteDir )
objFile.WriteLine( Mode )


objFile.WriteLine( strCMD )
objFile.WriteLine( "bye" )
objFile.Close()
Set objShell = Server.CreateObject("WScript.Shell")


set WSX = objShell.Exec( COMMAND_FTP & strFile & " " & strHost )
set ReturnCode = WSX.StdErr
set Output = WSX.stdOut
strErrorLog = objTempFldr.Path & "ftpErrors.txt"
strLog = objTempFldr.Path & "ftpLog.txt"

Set objFile = objFSO.OpenTextFile( strErrorLog, 2, True )
objFile.Write( ReturnCode.ReadAll() )
objFile.Close()

Set objFile = objFSO.OpenTextFile( strLog, 2, True )
objFile.Write( Output.ReadAll() )
objFile.Close()

'objFSO.DeleteFile strFile, True
set objFSO = nothing

Set objRegExp = New RegExp
objRegExp.IgnoreCase = True

objRegExp.Pattern = "not connected|invalid command|error"

If (objRegExp.Test( Output.ReadAll ) = True ) or (objRegExp.Test(
ReturnCode.ReadAll ) ) Then
FTP = False
Else
FTP = True
End If
Set objRegExp = nothing

End Function
'==================================
'==================================

FTP(strCommands)

Re: ASP to Check if a file exists on an FTP server

am 27.04.2006 00:00:58 von nvanhaaster

Just to Add the file names are easy such as
NLFiles.TXT
NYFiles.TXT
LGFile.csv
HRFile.csv

Re: ASP to Check if a file exists on an FTP server

am 27.04.2006 04:33:03 von McKirahan

wrote in message
news:1146088059.264131.242480@i39g2000cwa.googlegroups.com.. .
> Ok I have a script that is designed to access a FTP site to GET, PUT
> and DELETE files. It is initiated by a page where the user enters in a
> few variables that get stored in a database for future reporting. All
> that aside what I am looking for is a simple way through ASP (vbscript)
> to first loginto the FTP server and check if the files are there, then
> give a message box back to the USER displaying the different types of
> Files found. Here is an example of the message box:
> ===================
> LogHours File : Found
> NLHours File : Found
> NYHours File : Not Found
> HR Data File : Found
> "OK" "CANCEL"
> ====================
>
> The problem I am having is determining what i should be using, should i
> be using my FTP script to log into the FTP site or can I use
> Scripting.FileSystemobject. From what i know the
> Scripting.FileSystemobject is only for local files.
>
> So then comes what commands could i use to check if a file is on an FTP
> Site?

[snip]

One approach is to use ftp to download the contents of the directory
and examine it for the files of interest.

Try the following after changing the values of:
cDOM, cUSR, cPWD, and cDIR.

Also, the "ftp_dir." filename prefix values could be changed if you want.


Option Explicit
'*
'* Declare Constants
'*
Const cVBS = "ftp_dir.vbs"
Const cFTP = "ftp_dir.ftp"
Const cLOG = "ftp_dir.log"
Const cWSS = "%comspec% /C ftp -i -s:"
'*
Const cDOM = "your_domain.com"
Const cUSR = "your_username"
Const cPWD = "your_password"
Const cDIR = "your_subdirectory_if_applicable"
'*
'* Declare Variables
'*
Dim arrFIL(3)
arrFIL(0) = "NLFiles.TXT"
arrFIL(1) = "NYFiles.TXT"
arrFIL(2) = "LGFile.csv"
arrFIL(3) = "HRFile.csv"
Dim intFIL
Dim strFIL
Dim strFTP
strFTP = "open %dom|%usr|%pwd|hash|ascii|cd %dir|dir|close|bye"
strFTP = Replace(strFTP,"%dom",cDOM)
strFTP = Replace(strFTP,"%usr",cUSR)
strFTP = Replace(strFTP,"%pwd",cPWD)
strFTP = Replace(strFTP,"%dir",cDIR)
strFTP = Replace(strFTP,"|",vbCrLf)
Dim strLOG
Dim strMSG
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOTF
Dim objWSS
'*
'* FTP File
'*
Set objOTF = objFSO.OpenTextFile(cFTP,2,true)
objOTF.WriteLine(strFTP)
Set objOTF = Nothing
'*
'* FTP Exec
'*
Set objWSS = CreateObject("WScript.Shell")
objWSS.Run cWSS & cFTP & " >> " & cLOG,2,True
Set objWSS = Nothing
'*
'* LOG File
'*
Set objOTF = objFSO.OpenTextFile(cLOG,1)
strLOG = objOTF.ReadAll
Set objOTF = Nothing
'*
'* Destroy Objects
'*
objFSO.DeleteFile(cFTP,True)
Set objFSO = Nothing
'*
'* Finish
'*
For intFIL = 0 To UBound(arrFIL)
strFIL = arrFIL(intFIL)
strMSG = strMSG & vbCrLf & strFIL & " : "
If InStr(strLOG,strFIL) > 0 Then
strMSG = strMSG & "Found"
Else
strMSG = strMSG & "Not Found"
End If
Next
MsgBox strMSG,vbOKCancel,cVBS



If case-sensitivity of filenames is an issue then just change:
If InStr(strLOG,strFIL) > 0 Then
to
If InStr(LCase(strLOG),LCase(strFIL)) > 0 Then