FTP batch script is not exiting with error status - batch-file

There is a ftp command in my batch script :
FTP -n -s:D:\scripts\Test\get.ftp
Where get.ftp contains all ftp commands including "mget abc*".
Issue here is when file(s) of names starting with abc* is not available, mget is not failing. Also, if any other ftp command fails also, the script is not exiting with error status 1. i.e. "FTP -n -s:D:\scripts\Test\get.ftp" exiting without issues.
Not able to make the batch script fail when there is no file to pick up.
Need suggestion if someone has faced similar issue.
-Krishna

The mget command works by obtaining a remote folder listing and parsing the list for the wildcard pattern that you provide. As long as the listing can be obtained successfully,
it is not considered an error if your pattern did not match any of the files on the list.
Your batch script can be setup to compare the local folder listing before and after invoking the ftp command to check if a file was downloaded. You can also use a scripted ftp solution like kermit or ftp script to be able to have more control on error reporting.

Related

Using 'for' command to find and download the latest file on SFTP server in PSFTP

I have gone through the following guide to set up an SSIS package to retrieve a text file located on an SFTP server:
https://www.mssqltips.com/sqlservertip/3435/using-sftp-with-sql-server-integration-services/
To summarize, the SSIS package executes PSFTP.exe (A PuTTY tool) which takes the necessary credentials to connect to the server. It also takes a batch file that it executes after connecting. This batch file contains the commands to retrieve the desired text file. To start from the guide, it simply contains a cmd command to change directory, and a get command to retrieve the file:
cmd DataDump
get TeleMarketingResults.txt
All of this works fine.
The issue arises when I try to make this batch file logic more complex as it does not seem to recognize basic keywords. For instance, I would like to modify it to retrieve the most recent file, so I tried adding this:
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo The most recently created file is %LAST%
but then I get these errors:
psftp: unknown command "for"
psftp: unknown command "echo"
If I execute the batch file manually in a local directory, it works. The issue only occurs when passing it as a parameter to PSFTP.exe. Why is this?
psftp script file can contain psftp commands only. for, set or dir are not psftp commands.
There's hardly any reasonable way to retrieve latest file using psftp. You would have to do it in two steps. First to retrieve listing and store it to a file. Then parse that file using some smart batch file commands to find the latest files. And then run psftp again to download that file. It is cumbersome and ineffective as it requires two connections.
You better use a more powerful SFTP client. For example it's trivial with my WinSCP SFTP client. See
Question WinSCP select most recent file or
WinSCP article Downloading the most recent file.

Running WinSCP script displays nothing

I'm trying to use WinSCP through script to synchronize a local folder with an FTP one.
I created a script a below
open ftp://<user>:<pw>#ftp.myserver.com/initFolder/
synchronize remote -delete C:\Data\backup /initFolder/
exit
And I run it with the command
"C:\Program Files (x86)\WinSCP\WinSCP.exe" /log="C:\data\bin\log\WinSCP.log" /ini=nul /script="C:\data\bin\myScript.txt"
However, it appears nothing is happening. The command exists immediately with no message (either error or some processing / confirmation, etc).
Also no log file is created either.
What am I doing wrong?
Please advise
Indeed, with the command-line syntax you are using, WinSCP runs without any GUI.
If you are starting WinSCP from a console window (e.g. from Windows batch file), use winscp.com instead of winscp.exe. winscp.com is a console application. You will see any errors in the console.
Alternatively, you can add /console switch to winscp.exe command-line to make it open its own console window. Though you will rarely want this.

Batch Script responds with "Error opening script file" when opening FTP file

I want to use a Batch file to upload files from a folder on my computer.
When I call the FTP Script to do the upload:
FTP -v -i -s:ftp.txt
or
FTP -v -i -s:c:\path\to\file\ftp.txt
Neither of those works, and instead
Error opening script file: ftp.txt
is returned.
Strange thing is, that the exact same script is being executed without problems on another computer.
Opening the batch file with admin-rights does not help. I'm running Windows 10.
Edit: The ftp.txt looks like this:
open ##host##
##user##
##password##
lcd c:\local\path\
cd path/on/server/
binary
mput "*.xxx"
disconnect
bye
Make a new, empty file with echo >ftp.test and try with that file.
If that gives you the same error, you'll need to look at your execution environment (look at set and the process owner of cmd.exe). It's also possible that the ftp command is being run as a user that doesn't have access to that file.
If you don't get an error then it's probably either a file-permissions or special-character issue with ftp.txt. Retyping the same content into a different file will get around those issues. Remember not to copy/paste because you could accidentally copy invisible special characters that you're trying to avoid.
I would like to add to this conversation since there have been a lot of views and no solution. I had the same response and it wasn't a permissions issue. It was also no the text file that was the issue because it never technically reached the text file. It was the path to the text file. The following for me didn't work. I had to put quotations around the path even though there were no spaces to confuse the command line.
What you tried:
FTP -v -i -s:c:\path\to\file\ftp.txt
What I tried and worked. (I didn't need to use -v, mine works without it)
FTP -i -s:"c:\path\to\file\ftp.txt"
Running the file acted normal for me. My issue was trying to run the batch file through Windows Task Manager. Somehow, this was causing it to lose the text file somehow. I hope this helps anyone else who reads this thread.
I had to put the .txt file path in the "Start In" section for it to work in Windows Task Manager. No problems since.

Ignore Error Code 2 PSFTP

We have an desktop application that dynamically generates a command file to pull specific files that have the current date in the name. So in the end we have a command file that looks like this:
lcd e:\localpath
mget Filename0111.dat
mget Filenametwo0111.dat
mget Filenamethree0111.dat
bye
Where 0111 is MMDD. The command file is created via a .bat file that the desktop app executes. The application then connects to the remote server via PSFTP.exe and runs that command file to pull files.
The problem we're running into is we updated the PSFTP.exe to a newer version due to a separate issue that occurred. Now if a file is not available on the remote server it returns an error code 2 which stops the rest of the files from being retrieved. So if the first file in the list doesn't exist then it fails and the rest of the files are not downloaded.
Is there a way to ignore the error code 2 so that the rest of the files get retrieved? I had thought at first to run PSFTP.exe and it's commands through a batch file but that didn't work.
Any ideas?
PSFTP.exe has a command -be that will continue executing the batch if there is an issue.
When running a batch file, this option causes PSFTP to continue processing even if a command fails to complete successfully.
You might want this to happen if you wanted to delete a file and didn't care if it was already not present, for example.

Copy files from Ftp to local directory using batch file

I try to copy a lot o file from my ftp = ftp://ftp.prodega.ch so:
I created a code.txt file with this text:
open ftp://ftp.prodega.ch
user
password
lcd /D "E:\f2\" //this is my local directory
cd Bilder1/ //this is ftp folder
mget *
pause
the I execute in cmd this row : ftp -s:code.txt but I meet with this error:
unknown host: ftp://ftp.prodega.ch
help me please
When connecting to a server via the built-in FTP client you have to skip ftp://!
So open ftp.prodega.ch will fix your issue.
However, you might still face another problem. The standard FTP client doesn't support passive mode which is required by most of the servers. If you are not able to modify the server, you won't be able to download the files. You should consider using a PowerShell script instead or use a different FTP client with command line support.

Resources