Windows command-line ftp put/upload files older than 1 minute - ftp

I have the following batch file:
open <ip>
username
password
bin
lcd C:\FTP_OUT\
prompt
mput *.PDF
ascii
mput *.XML
bye
How do I change it to upload only files older than 1 minute to the FTP server?

The Windows ftp.exe does not support any kind of a file selection based on a file timestamp.
All you can do is to generate a temporary upload script with an explicit list of files to upload. You can use a PowerShell script to generate the temporary script (implementing in a pure batch file would be very difficult).
A way easier solution is to use an FTP client that supports selecting files based on file timestamp.
For example with WinSCP FTP/SFTP client you can use the following batch file:
#echo off
winscp.com /log=upload.log /command ^
"open ftp://username:password#example.com/" ^
"lcd C:\FTP_OUT" ^
"put *.PDF<1N" ^
"put -transfer=ascii *.XML<1N" ^
"exit"
The <1N in the files mask is a time-constraint that selects files created or modified more than one minute ago only.
See also the guide for converting Windows ftp.exe scripts to WinSCP scripts.
(I'm the author of WinSCP)

Related

How to execute further batch file commands after running "ftp" commands contains in the batch file itself

I have a .bat document where I do some operations with FTP. I want to end the FTP connection and get to finish with the terminal on the desired directory.
I'm trying this, but when it gets to quit, it stops.
cd %USERPROFILE%\Foilder\project\angular
call ng build
#FTP -i -s:"%~f0"&GOTO:EOF
open connection
user
password
cd httpdocs/project
mdelete *.woff
quit
cd %USERPROFILE%\Foilder\Subfolder
It gives me this message right after doing the quit and never runs the next line.
221 Goodbye.
Ideal scenario, run this last cd command and have the terminal ready on the folder that I want to run it. Thing is that I have to do some uploads to two different FTP servers and it would be great to have the console finishing in the folder where I run "dev.bat" to get it to work
Is it possible to do this?
Your script uses a known hack that allows you to specify ftp commands directly in the batch file.
The %~f0 is replaced with the path to the batch file itself. So the ftp -s:%~f0 runs ftp and tells it to use the batch file itself as ftp script file. You have probably noticed that it results in couple of errors, as ftp fails on the first few lines of the batch file, which are not valid ftp commands (the cd ..., call ... and ftp ...).
Equivalently, the batch file would try to run all commands after ftp ... once ftp finishes, also failing, as those are not valid batch file commands. To avoid that, the hack uses GOTO:EOF to skip the rest of the batch file (EOF=eof of the file).
While you actually want to execute some commands after the ftp. At least the cd command. So do not skip the rest of the batch file. Skip only the ftp commands:
ftp -i -s:"%~f0"&goto AFTER_FTP
(ftp commands)
quit
:AFTER_FTP
cd %USERPROFILE%\Foilder\Subfolder
Note the # before ftp. That (and the GOTO:EOF) are clear signs, that the script you based your batch file on, was designed to start with ftp on the very first line and contain nothing else, but the ftp commands. You have deviated from that.
Alternatively, use some more capable FTP client that allows specifying the commands on its command-line without hacks.
For example with my WinSCP FTP client, you can do:
cd %USERPROFILE%\Foilder\project\angular
call ng build
winscp.com /ini=nul /command ^
"open ftp://user:password#connection/" ^
"cd httpdocs/project" ^
"rm *.woff" ^
"exit"
cd %USERPROFILE%\Foilder\Subfolder
There's a guide for converting Windows ftp script to WinSCP script.

FTP copy all files from a directory to local directory

I am trying to create a batch file that FTPs into a URL(say ftp.tester123.com.au) with directory "C:\Documents\Client\" using username mark123 and password testabc into local directory "C:\Desktop\GoHere\".
The script is to copy all the files within this ftp directory to my local machine. I have read the following msdn tutorial
https://support.microsoft.com/en-gb/kb/96269
But it didn't help me with my current issue. Will winscp allow me to perform this task and create a batch file for it? I would like to automate execution of this directory - at say midnight every night. Is this possible?
Alternatively I am very familiar with .NET and winSCP has a wrapper to allow you to write C# instead of standard scripting. If I go with a .NET approach, can I create a simple .exe or batch file that can simply be executed by double clicking?
Thanks in advance!
What you are trying to do is a very trivial task. What you are missing is a conceptual understanding of the task. So it's difficult to give you an answer that will help you, as it's difficult to understand what piece of knowledge you are missing to accomplish it.
The easiest approach to start with, is to make use of WinSCP ability to generate a transfer code:
Login to your FTP server with WinSCP;
Navigate to the source remote directory and to the destination local directory;
Select the remote files to download (all files?);
Use the Files > Download command in the main menu (or the Download command in the files' context menu);
On the Download dialog, click the drop down arrow on the Transfer Settings button and select the Generate Code command.
You will get the Generate transfer code dialog.
There, you can choose if you want WinSCP to generate a WinSCP script or even complete Windows batch file:
or .NET assembly (C#) code:
(These are official screenshots from WinSCP documentation. So they show SFTP upload, not FTP download)
Now that you have your batch file or C# executable ready, you can schedule it to be run using Windows Scheduler.
If you have WinSCP installed, then the following code will work for you. I fetched all files from a particular folder and downloaded it to a local folder. Wrote a logfile along the way. You can generate this code using WinSCP too.
#echo off
set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
set d_name=/export/home/sysm/opt/oss/server/var/fileint/pm/pmexport_%mydate%
#echo %d_name%
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Users\HP\Desktop\Datacom\Reports\WinSCP.log" /ini=nul ^
/command ^
"open sftp://YOUR_USERNAME:YOUR_PASSWORD#FTP_SERVER_IP/ -hostkey=""ssh-rsa 1024 PUuYRVADKXB9j1Si+o89v2fsrsr7w2ZrV3NIqdz6kus0GtY=""" ^
"cd %d_name%" ^
"lcd C:\Users\HP\Desktop\Datacom\Reports\Test_ftp" ^
"get *" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
Modfify it according to your need and then put the code into a .bat file. Later you can execute it manually or use Windows Task Scheduler.

Downloading files with defined changed date time

Dears, could you please share your ideas how to download files from an FTP server to a local directory filtered by changed date.
So, I have a folder on the FTP server with files, but I need to move just those of them which have been changed yesterday (f.e.).
Thanks in advance for your reply.
open anymail.com
login
pass
!:--- FTP commands below here ---
lcd D:\Test
cd /home/my_folder
binary
mget *
disconnect
bye
You can hardly do this with the Windows built-in ftp.exe.
You would have to list the remote directory, redirect the listing to a file. Then parse the listing file to find the files you want. And then generate an ad-hoc download script. Quite a lot of work.
Use some more capable 3rd party Windows FTP command-line client.
For example with WinSCP scripting you can use a batch file (e.g. download_yesterdays.bat) like:
winscp.com /ini=nul /log=ftp.log /command ^
"open ftp://user:password#ftp.example.com/" ^
"cd /remote/path" ^
"lcd c:\local\path" ^
"get *>=yesterday<today" ^
"exit"
The >=yesterday selects files created since yesterday (inclusive). The <today excludes files created today. This syntax is supported since WinSCP 5.15.
In earlier versions of WinSCP, you can use %TIMESTAMP% syntax instead: >=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd% (the TIMESTAMP-1D syntax is supported since WinSCP 5.9).
References:
WinSCP guide to Downloading the most recent file from FTP server
File masks with time-constraints
%TIMESTAMP% syntax
(I'm the author of WinSCP)

Implicit TLS FTP connection using WinSCP or other command line FTP client in Windows

I need to connect to an FTP secured with implicit TLS, username and password from the command line so that I can create a batch later on. I am using WinSCP since it's the most popular (I think) or the one that I can use to achieve this according to extensive googling. I'm not an expert but reading the documentation I have this so far with unsuccessful connection from command prompt:
winscp> open username:password#host:port -implicit -certificate="aa:aa:aa:aa..."
Once connected I need to extract everything there to a folder \hostname\folder or c:\folder then delete the files on the ftp (more or less like cut)
Thanks to Martin Prikryl correction I was able to connect, here is what I did:
Created a text file with the winscp.com commands "connection.txt":
# Automatically abort script on errors
option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect with username and password
open ftp://username:password#host:port -implicit -certificate="aa:aa:aa:aa...."
# Change local directory
lcd C:\local_working_folder
# Force binary mode transfer
option transfer binary
# Download missing files to local directory from remote
synchronize local
# Delete all remote files
rm *
# Disconnect
close
# Exit WinSCP
exit
Now from command prompt or in a batch file you just call winscp.exe from where it is installed. If from installation media it should be C:\Program Files\WinSCP\WinSCP.exe of if extracted from a .zip portable version point to the folder where you extracted the file:
C:\WinSCP\WinSCP.exe /console /script="connection.txt" /log="conn.log"
Then just schedule the batch to automate the process.
Your syntax is almost correct. You are missing only the protocol prefix ftp:// (note that WinSCP defaults to the SFTP protocol).
open ftp://username:password#host:port -implicit -certificate="aa:aa:aa:aa..."
For details see:
https://winscp.net/eng/docs/session_url
https://winscp.net/eng/docs/scriptcommand_open
If that does not help, you need to share with us a script log file or at least an error message.

How to create a .BAT file to download file from HTTP\ftp server?

How to create a .BAT file to download file or folder from FTP server? (and replace with it existing file) (we have links like ftp://me:mypass#example.com/file.file (or http://example.com/file.file) and absolute file link like C:\Users\UserName\Some mixed Русский English Adress\file.file) (using only native windows (xp vista win7 etc) BAT functions and files)
Here is an example of how to automate the built-in ftp.exe tool:
http://www.howtogeek.com/howto/windows/how-to-automate-ftp-uploads-from-the-windows-command-line/
The example is about uploading, but the principle is the same (just use get instead of put).
Still, since this is just "piping commands" into ftp.exe, I recommend not to do this for production-quality batch files (no error handling, etc.), but to use some external tool instead. I provided this answer only because you explicitly asked for a solution that only uses Windows built-in commands.
EDIT: Here's a concrete example:
REM replace this with your user name and password
REM make sure there is no space between the pwd and the >>
echo user me > getftp.dat
echo mypass>> getftp.dat
echo binary >> getftp.dat
REM replace this with the remote dir (or remove, if not required)
echo cd remoteSubDir >> getftp.dat
REM replace this with the local dir
echo lcd C:\Users\UserName\SomeLocalSubDir >> getftp.dat
REM replace this with the file name
echo get file.file >> getftp.dat
echo quit >> getftp.dat
REM replace this with the server name
ftp -n -s:getftp.dat example.com
del getftp.dat
I have previously used WGET in a batch file to accomplish this.
http://www.gnu.org/software/wget/
The command-line FTP program that's built-in to most Windows operating systems is scriptable. You just need to create a text file with the commands you would send if you were running it by hand (one command per line), then execute it like this:
ftp -s:download.scr
What FTP client software are you using? Is it scriptable? If so, create a script that downloads files and call this script from your batch file.
I'm doing this with WS_FTP.

Resources