Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a file exists on FTP site

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
0
0
US
Hello
I created a VB script with a few batch jobs to copy a file from FTP site. Here is the code. Can someone let me know how to check FTP site for the existance of a file?

1) This is a batch file (called from an sp) that calls vbs file:
d:
cd \activity
\\server\directory\MyScript.vbs
2)
This is VBS file that calls a batch file to get a file from FTP site
Dim objFSO
Dim objTextFile
Dim oScript
Dim strCMD
Dim strFileName
strFileName="\\server\QA\Feed.txt"

On Error Resume Next
' shell command object
Set oScript = Server.CreateObject("WSCRIPT.SHELL")


'Run batch file to connect to FTP site an grab the file
oScript.run "GetFTP.bat"

'Call an application that works with the file
oScript.run (App.exe strFileName)

Set oScript=Nothing

Here is GetFTP.bat (it call another batch)

ftp -s:"FinallyGetFile.bat"

Here is FinallyGetFile.bat

open ftp.Site.com
userID
password
lcd \\server\QA\
get Feed.txt
close
bye
-------------------------------------------

Please let me know if this looks good and also how I can have VB script check if the file actually exist on FTP site before trying to get it

thanks!!!
 
develop155,

This is one way you can check the existence of feed.txt at the _root_ of the ftp site.

The command file being cmd.txt, say, is like this. (Maybe avoid .bat for the ftp command file. Bat extension has a common association and connotation.)
Code:
open ftp.Site.com 
userID
password
lc 
close
bye
The vbs testing its existence can be scripted like this.
Code:
ftpcmd="cmd.txt"    'add full path is necessary
targetfile="feedtxt"    'at root of ftp site

set wshshell=createobject("wscript.shell")
set oexec=wshshell.exec("ftp -s:" & ftpcmd)
do while oexec.status=0 : wscript.sleep 50 : loop
s=oexec.stdout.readall
set wshshell=nothing

if instr(1,s,targetfile,1)<>0 then
    wscript.echo "Target file -" & targetfile & "- is present in the ftp site in the designated directory."
else
    wscript.echo "Target file -" & targetfile & "- is not present in the ftp site in the designated directory."
end if
You can make it a function, instead of wscript.echo, returning a boolean true for existence and false for non-existence.

regards - tsuji
 
tsuji, thanks a lot, I have a few Q's:
1)what is the difference between wshshell.run AND wshshell.exec
2) What is oexec? I assume it gets status from running the ftp command? What kind of values does it have
3) What is wscript? I do not see it initizlized anywhere


thanks so much!!!
 
-Erratum-

I had a typo up there in the cmd.txt. [red]"lc" (_wrong_)[/red] should be read [blue]"ls"[/blue] which is a command ftp subcommand. Maybe that's why you ask?

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top