• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3839
  • Last Modified:

Output FTP results to a log/text file.

I have a VBScript that FTPs files and want it to create a text file displaying the contents of the session it just ran through.
Or at the very least, some sort of error logging so I can go back the next day - - and if it failed to FTP - - I can try to see why.

Here's my script:  The bottom few lines is really the FTP in action.  I'm just setting up my FTP commands in a temporary text file in the beginning:


strLocalFolderName = "C:\"
strFTPServerName = "myftpsite.athome.com"
strLoginID = "anonymous"
strPassword = "hello"
strFTPServerFolder = "Incoming\updates\data"
strfile2put = "myfile.txt"

strFTPScriptFileName = strLocalFolderName & "FTPScript.txt"

Set objMyFile = objFSO.CreateTextFile(strFTPScriptFileName, True)
objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine (strLoginID)
objMyFile.WriteLine (strPassword)
objMyFile.WriteLine ("cd " & strFTPServerFolder)
objMyFile.WriteLine ("ascii")
objMyFile.WriteLine ("prompt n")
objMyFile.WriteLine ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("put " & strfile2put)
objMyFile.WriteLine ("bye")
objMyFile.Close

Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34))
Set objShell = Nothing
0
ZabagaR
Asked:
ZabagaR
  • 6
  • 5
1 Solution
 
zzzzzoocCommented:
You can try outputting the results to a file then reading that. Something like.. "ftp >test.txt", where the incoming data will be outputted to "test.txt". Not sure how well that'll work for you, though, since user-side errors probably won't be logged.
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

You could insert DOS 'echo' commands inside your created "FTPScript.txt" file to report progress as you go...

For example:

objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine ("!echo Connected to FTP Server: " & strFTPServerName)

objMyFile.WriteLine (strLoginID)
objMyFile.WriteLine (strPassword)
objMyFile.WriteLine ("!echo Log-in to FTP Server with User Name: " & strLoginID)

objMyFile.WriteLine ("cd " & strFTPServerFolder)
objMyFile.WriteLine ("!echo Changed FTP directory to: " & strFTPServerFolder)

objMyFile.WriteLine ("ascii")
objMyFile.WriteLine ("prompt n")
objMyFile.WriteLine ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("!echo Changed Local directory to: " & strLocalFolderName)

objMyFile.WriteLine ("put " & strfile2put)
objMyFile.WriteLine ("!echo Put file: " & strfile2put)

objMyFile.WriteLine ("bye")
objMyFile.WriteLine ("!echo Closed FTP session")


These commands can be re-directed to a local log file, thus:

objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine ("!echo Connected to FTP Server: " & strFTPServerName & " >" & strLocalFolderName & "FTPstatus.log")

objMyFile.WriteLine (strLoginID)
objMyFile.WriteLine (strPassword)
objMyFile.WriteLine ("!echo Log-in to FTP Server with User Name: " & strLoginID & " >>" & strLocalFolderName & "FTPstatus.log")

objMyFile.WriteLine ("cd " & strFTPServerFolder)
objMyFile.WriteLine ("!echo Changed FTP directory to: " & strFTPServerFolder & " >>" & strLocalFolderName & "FTPstatus.log")

objMyFile.WriteLine ("ascii")
objMyFile.WriteLine ("prompt n")
objMyFile.WriteLine ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("!echo Changed Local directory to: " & strLocalFolderName & " >>" & strLocalFolderName & "FTPstatus.log")

objMyFile.WriteLine ("put " & strfile2put)
objMyFile.WriteLine ("!echo Put file: " & strfile2put & " >>" & strLocalFolderName & "FTPstatus.log")

objMyFile.WriteLine ("bye")
objMyFile.WriteLine ("!echo Closed FTP session" & " >>" & strLocalFolderName & "FTPstatus.log")


Note the use of the single redirection character ">" in the first line; this creates the "FTPstatus.log"...

objMyFile.WriteLine ("!echo Connected to FTP Server: " & strFTPServerName & " >" & strLocalFolderName & "FTPstatus.log")

Where all other redirection characters ">>" append to the existing file (created in the first line).




Or, as zzzzzooc suggested simply change the objShell.Run line to:

objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34) & ">" & strLocalFolderName & "FTPaudit.log")

Again, a single redirection character ">" indicates the file will be created every time the batch file is run.

Change to double characters ">>" to append to an existing file, e.g.

objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34) & ">>" & strLocalFolderName & "FTPaudit.log")



BFN,

fp.

0
 
ZabagaRAuthor Commented:
Thanks for the input.

Before I posted my question, I tried adding the "> c:\ftp.txt" to the end of the command line.  No file was ever created.

objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34) & ">c:\FTP.txt")

BUT....I see you specified the double " >>"....I'll try that.

Also - this isn't a script I'm actively watching so ECHO won't help me out.  This runs over the weekend and I come in on Monday to see the results.
So if it fails I have to figure why....

-thanks & I'll try the >>

z



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
[ fanpages ]IT Services ConsultantCommented:
Hi,

As I mentioned, the single redirection character ">" removes any existing file & creates a new file, the double character ">>" just appends to an existing file (or create a new file if it doesn't alreadt exist).

As the "echo" statements are redirected to the piped file ("ftp.txt", or whatever you called it), then you can review the file contents whenever you choose.  They will not be shown on-screen during the processing anyway (unless you duplicate the echo statement lines & have one redirecting to the file, and the other to the screen only; i.e. removing the redirection ">c:\FTP.txt" from each of the duplicated lines).

Not sure why you are not seeing anything in your file though.  Presumably the file was created?  Was it zero bytes in length?

BFN,

fp.
0
 
ZabagaRAuthor Commented:
For some reason, the ftpaudit.log was never created when I used this line:

objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34) & ">>" & strLocalFolderName & "FTPaudit.log")
0
 
ZabagaRAuthor Commented:
I think the problem with a log done like this:

objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine ("!echo Connected to FTP Server: " & strFTPServerName & " >" & strLocalFolderName & "FTPstatus.log")

Is that:  If the FTP "OPEN" command works or fails to connect, I end up with the identical log contents.

Either way, I will get a log saying "connected to ftp server...."

0
 
[ fanpages ]IT Services ConsultantCommented:
Yes, you will get the same message regardless of output... as my suggestion did not check any status code to indicate a successful connection or not... I was just showing you examples of what messages you could display.

0
 
ZabagaRAuthor Commented:
Do you have any thought as to why the >> c:\ftp.txt wouldn't output the command to a file?

I need some sort of logging to track errors if they occur during the FTP session.

I'm open to any suggestions...
0
 
[ fanpages ]IT Services ConsultantCommented:
...It seems you cannot use the redirection facility from the Run method of the objShell object.

This following line should be used instead:

objShell.Run ("%comspec% /C ftp -s:" & chr(34) & strFTPScriptFileName & chr(34) & ">>" & strLocalFolderName & "FTPaudit.log")

It runs the same (Run) method but invokes the console command processor [c:\Windows\System32\cmd.exe] and passes the same FTP command to this.

I have tested the above & the FTPaudit.log file was created :)

BFN,

fp.


0
 
ZabagaRAuthor Commented:
OK - I'll try it.  Could I replace the variable %comspec% with c:\windows\system32\cmd.exe ?
Although, I'd most likely use the variable since it will fit with systems that use c:\winnt AND c:\windows

Thanks - I'll try today.

Is there a place on the internet you looked that solution up at? I'm always looking for a nice new link!

0
 
ZabagaRAuthor Commented:
It works.  You da man!

-z-
0
 
[ fanpages ]IT Services ConsultantCommented:
:)

You're very welcome.

Yes, as you mentioned, you could replace the environment variable %comspec% with wherever your command interpreter file was located but, again, as you mentioned, this would presume that all users installed the file in the same folder of the same drive.

No, sorry, there was nowhere specifically I "looked the solution up"... apart from my past experience.

When I initially posted I had forgotten that redirection was not supported in the Shell.Run method.

BFN,

fp.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now