Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a .bat file that runs a ftp script, grabbing files from the server and copying them to my machine. Works great.

I now want this .bat file to report if any of these errors occured.

If you could point me in the right direction to the general idea of how to do this, that would be great.

Thanks.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

To be honest I've never had great success using fixed FTP scripts. Even if you deal with the return codes its hard to know exactly what went wrong.

I'd recommend using PowerShell or Python for the job instead. Both these have access to an FTP client that can be dynamically controlled. You'll know exactly whats worked or failed, and be able to deal with the issue then and there.

Simple Example in PS

$url = "ftp://ftp.foo.com/bar.txt"
$destination = "c:\foo\bar.txt"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $destination)

If you need to do more complicated things I'd start by looking at http://msdn.microsoft.com/en-us/library/ms229718.aspx Those examples are in C#, but it should be pretty easy to convert them to PS.

A script built in this way can list a dir and loop over the files downloading the ones you want. If there are any errors you can handle them on a per file level how you like.

share|improve this answer
add comment

It's common for console utilites to return some code indicating that error was happened, they are described e.g. here. If ftp exits after some error it probably returns a non-zero code indicating fault. IMHO there is no simple way to parse output of a program in batch files, but you can check errorlevel and save a logfile (using output stream redirection: command args >log) or send it by email.

share|improve this answer
    
Are there any ftp specific error levels, indicators of what the error was, or am I only given "something" went wrong? Thank You. –  Tommy Jan 21 '10 at 20:09
    
Looks like this is a custome error check: experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/… –  Tommy Jan 21 '10 at 20:11
    
Yes, that script is parsing ftp's output (it does not use ftp errorlevels, only find). It is what I called 'complicated' in my post, through. Sorry, I cannot provide you more detail about batch files and ftp because I do not have an accessible Windows system now. –  whitequark Jan 21 '10 at 20:27
add comment

That error message is a FTP specific error, and is because your firewall will by default disable FTP connections (this is a good thing!).

You can ignore this message as it will not affect your computer in any way.

share|improve this answer
add comment

Here's how to do it in a BAT file.

Call FTP, while redirecting the output and errors to separate files. If the error file is nonblank, then there's an issue.

Assuming you've created set variables called FTPFILE, OUTPUTFILE and ERRORFILE, then the following should work:

  1. Call FTP, capturing output and errors to separate files.

    ftp -i -s:"%FTPFILE%" >"%OUTPUTFILE%" 2>"%ERRORFILE%"
    
  2. Check if the FTP call failed

    if  ERRORLEVEL 1 (
        echo Call to ftp returned an error. See file: %OUTPUTFILE%
        exit /b 1
    )
    
  3. Check if the FTP session contained errors. Do this by checking the size of the errorfile.

    FOR /F "usebackq" %%A IN ('%ERRORFILE%') DO set ERRORFILESIZE=%%~zA
    if %ERRORFILESIZE% GTR 0 ( 
        echo Error while performing the FTP. See file: %ERRORFILE%
        exit /b 2
    )
    
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.