Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to script FTP upload and download

I'm attempting to make a batch file to upload a file to an FTP server. If I type it in manually it works fine, but when I run the batch file it halts after it's connected... It says:

connected to domain.com.  220 microsoft ftp server  User(domain.com:(none)): 

And then nothing else. What is going on here?

Below is my batch file:

ftp www.domainhere.com  user useridhere  passwordhere  put test.txt  bye  pause 
like image 559
payling Avatar asked Jun 01 '09 18:06

payling


People also ask

How do I automate an FTP file transfer?

Generating script Select the files you want to transfer. Use one of the file transfer commands: Upload, Download, Upload and Delete, Download and Delete. On the transfer confirmation dialog, setup transfer options (if you need any non default settings). Use the Transfer Settings > Generate Code command.

How do I run an FTP script?

You can script the ftp command with the -s:filename option. The syntax is just a list of commands to pass to the ftp shell, each terminated by a newline. This page has a nice reference to the commands that can be performed with ftp .


2 Answers

It's a reasonable idea to want to script an FTP session the way the original poster imagined, and that is the kind of thing Expect would help with. Batch files on Windows cannot do this.

But rather than doing cURL or Expect, you may find it easier to script the FTP interaction with PowerShell. It's a different model, in that you are not directly scripting the text to send to the FTP server. Instead you will use PowerShell to manipulate objects that generate the FTP dialogue for you.

Upload:

$File = "D:\Dev\somefilename.zip" $ftp = "ftp://username:[email protected]/pub/incoming/somefilename.zip"  "ftp url: $ftp"  $webclient = New-Object System.Net.WebClient $uri = New-Object System.Uri($ftp)  "Uploading $File..."  $webclient.UploadFile($uri, $File) 

Download:

$File = "c:\store\somefilename.zip" $ftp = "ftp://username:[email protected]/pub/outbound/somefilename.zip"  "ftp url: $ftp"  $webclient = New-Object System.Net.WebClient $uri = New-Object System.Uri($ftp)  "Downloading $File..."  $webclient.DownloadFile($uri, $File) 

You need PowerShell to do this. If you are not aware, PowerShell is a shell like cmd.exe which runs your .bat files. But PowerShell runs .ps1 files, and is quite a bit more powerful. PowerShell is a free add-on to Windows and will be built-in to future versions of Windows. Get it here.

Source: http://poshcode.org/1134

like image 156
Cheeso Avatar answered Sep 20 '22 09:09

Cheeso


Create a command file with your commands.

I.e., file commands.txt:

open www.domainhere.com user useridhere passwordhere put test.txt bye 

Then run the FTP client from the command line:

ftp -s:commands.txt 

Note: This will work for the Windows FTP client.

like image 45
5 revs, 2 users 64% Avatar answered Sep 19 '22 09:09

5 revs, 2 users 64%