views:

5456

answers:

9

I'm attempting to make a batch file to upload a file to 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)):

then nothing else. What the heck is going on here?

Below is my batch file:

ftp www.domainhere.com 

user useridhere

passwordhere

put test.txt

bye

pause
A: 

Consider just using something like cURL rather than manually scripting the FTP conversation.

Jeff Mc
+5  A: 

Create a command file with your commands

ie: 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.

Edit: Should have had a linebreak after the user name before the password.

cbeuker
doesn't work, says password is srequire, tried it a couple different ways
payling
+4  A: 

Batch files don't work that way. They don't just "type" everything - they run system commands, in this case ftp, wait for them to return, and run the next command... so in this case, the interpreter is simply waiting for ftp to exit.

If you must use the ftp command, then prepare a script file (for example, commands.txt and run ftp -s:commands.txt.

But using cURL, or a PHP/Perl/Python/whatever script may be a better idea.

grawity
A: 

I would go with the cURL way as Jeff and grawity propose, but if your client is not scriptable, you could use Expect to automatically send it commands.

Damien Pollet
I thought that it would be simple to use a batch file to upload and download files from the ftp server using windows scheduler to execute it every so often.
payling
+13  A: 

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

Cheeso
Hrrm, looks like good idea seeing how I can't get a batch to run the way i want it... I just finished installing it.. Not exactly sure what I'm doing but I'll give it a few minutes to sink in. Will I be able to incorporate windows scheduler with this to run my script in set intervals?
payling
ok, I've got it to upload and download correctly. How would I go about automating the process?
payling
You're doing fine. If you are doing FTP from within powershell, then it *is* automated. What you want is to schedule it. Which means schtasks.exe. http://support.microsoft.com/kb/814596
Cheeso
haha, was waiting to get a couple more answers out of you first! =0 I figured it all out, I just used a batch file with powershell.exe /pathto/update.ps1 then put windows schedular on the bat file. Not sure if that's the best way but it's working! Thanks for all your help cheeso!
payling
the way to run powershell on a schedule is to specify this command line to the scheduler: c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe <fullpath-to-ps1-file> <args-to-ps1-file>
Cheeso
How would you modify this to upload several files with different extensions?
Fishwalker
A: 

ftp www.domainhere.com

useridhere

passwordhere

put test.txt

bye

pause

A: 

I keep getting password invalid but its valid because i can use my browser to connect to the ftp site. Any ideas?

A: 

I had this same issue, and solved it with a solution similar to what Cheeso provided, above.

"doesn't work, says password is srequire, tried it a couple different ways "

Yep, that's because FTP sessions via a command file don't require the username to be prefaced with the string "user". Drop that, and try it.

Or, you could be seeing this because your FTP command file is not properly encoded (that bit me, too). That's the crappy part about generating a FTP command file at runtime. Powershell's out-file cmdlet does not have an encoding option that Windows FTP will accept (at least not one that I could find).

Regardless, as doing a WebClient.DownloadFile is the way to go.

BryceNetwork23
A: 

is this script apply to SFTP connection? if it can how i want to write it?

thank you

dxerox