How script upload and download ftp?

I am trying to make a batch file to upload a file to an ftp server. If I type it manually, it works fine, but when I run the batch file, it stops after connecting it ... it says

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

then nothing else. What's going on here?

Below is my batch file:

 ftp www.domainhere.com user useridhere passwordhere put test.txt bye pause 
+38
command-line powershell ftp
Jun 01 '09 at 18:31
source share
9 answers

It is a reasonable idea to want an FTP session script as the original poster had expected, and this is what Expect can count on. Windows batch files cannot do this.

But instead of doing cURL or Expect, it might be easier for you to script to interact FTP with Powershell. This is another model in which you do not directly process the text to send to the FTP server. Instead, you will use Powershell to manage the objects that generate the FTP dialog for you.

Loading:

 $File = "D:\Dev\somefilename.zip" $ftp = "ftp://username:password@example.com/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:password@example.com/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) 

For this you need Powershell. If you don't know, Powershell is a shell like cmd.exe that runs your .bat files. But Powershell runs .ps1 files, and it is a bit more powerful. Powershell is a free add-on for Windows and will be integrated into future versions of Windows. Get here .

Source: http://poshcode.org/1134

+62
Jun 01 '09 at 21:42
source share

Create a batch file with your commands

ie: commands.txt

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

Then start the FTP client from the command line: ftp -s: commands.txt

Note. This will work for the Windows FTP client.

Edit: should have had an interrupt after the username before the password.

+24
Jun 01 '09 at 19:03
source share

Batch files do not work. 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 just waits for ftp to exit.

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

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

+6
Jun 01 '09 at 19:09
source share

I did this using PowerShell :

 function DownloadFromFtp($destination, $ftp_uri, $user, $pass){ $dirs = GetDirecoryTree $ftp_uri $user $pass foreach($dir in $dirs){ $path = [io.path]::Combine($destination,$dir) if ((Test-Path $path) -eq $false) { "Creating $path ..." New-Item -Path $path -ItemType Directory | Out-Null }else{ "Exists $path ..." } } $files = GetFilesTree $ftp_uri $user $pass foreach($file in $files){ $source = [io.path]::Combine($ftp_uri,$file) $dest = [io.path]::Combine($destination,$file) "Downloading $source ..." Get-FTPFile $source $dest $user $pass } } function UploadToFtp($artifacts, $ftp_uri, $user, $pass){ $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) foreach($item in Get-ChildItem -recurse $artifacts){ $relpath = [system.io.path]::GetFullPath($item.FullName).SubString([system.io.path]::GetFullPath($artifacts).Length + 1) if ($item.Attributes -eq "Directory"){ try{ Write-Host Creating $item.Name $makeDirectory = [System.Net.WebRequest]::Create($ftp_uri+$relpath); $makeDirectory.Credentials = New-Object System.Net.NetworkCredential($user,$pass) $makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory; $makeDirectory.GetResponse(); }catch [Net.WebException] { Write-Host $item.Name probably exists ... } continue; } "Uploading $item..." $uri = New-Object System.Uri($ftp_uri+$relpath) $webclient.UploadFile($uri, $item.FullName) } } function Get-FTPFile ($Source,$Target,$UserName,$Password) { $ftprequest = [System.Net.FtpWebRequest]::create($Source) $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password) $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile $ftprequest.UseBinary = $true $ftprequest.KeepAlive = $false $ftpresponse = $ftprequest.GetResponse() $responsestream = $ftpresponse.GetResponseStream() $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create) [byte[]]$readbuffer = New-Object byte[] 1024 do{ $readlength = $responsestream.Read($readbuffer,0,1024) $targetfile.Write($readbuffer,0,$readlength) } while ($readlength -ne 0) $targetfile.close() } #task ListFiles { # # $files = GetFilesTree 'ftp://127.0.0.1/' "web" "web" # $files | ForEach-Object {Write-Host $_ -foregroundcolor cyan} #} function GetDirecoryTree($ftp, $user, $pass){ $creds = New-Object System.Net.NetworkCredential($user,$pass) $files = New-Object "system.collections.generic.list[string]" $folders = New-Object "system.collections.generic.queue[string]" $folders.Enqueue($ftp) while($folders.Count -gt 0){ $fld = $folders.Dequeue() $newFiles = GetAllFiles $creds $fld $dirs = GetDirectories $creds $fld foreach ($line in $dirs){ $dir = @($newFiles | Where { $line.EndsWith($_) })[0] [void]$newFiles.Remove($dir) $folders.Enqueue($fld + $dir + "/") [void]$files.Add($fld.Replace($ftp, "") + $dir + "/") } } return ,$files } function GetFilesTree($ftp, $user, $pass){ $creds = New-Object System.Net.NetworkCredential($user,$pass) $files = New-Object "system.collections.generic.list[string]" $folders = New-Object "system.collections.generic.queue[string]" $folders.Enqueue($ftp) while($folders.Count -gt 0){ $fld = $folders.Dequeue() $newFiles = GetAllFiles $creds $fld $dirs = GetDirectories $creds $fld foreach ($line in $dirs){ $dir = @($newFiles | Where { $line.EndsWith($_) })[0] [void]$newFiles.Remove($dir) $folders.Enqueue($fld + $dir + "/") } $newFiles | ForEach-Object { $files.Add($fld.Replace($ftp, "") + $_) } } return ,$files } function GetDirectories($creds, $fld){ $dirs = New-Object "system.collections.generic.list[string]" $operation = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails $reader = GetStream $creds $fld $operation while (($line = $reader.ReadLine()) -ne $null) { if ($line.Trim().ToLower().StartsWith("d") -or $line.Contains(" <DIR> ")) { [void]$dirs.Add($line) } } $reader.Dispose(); return ,$dirs } function GetAllFiles($creds, $fld){ $newFiles = New-Object "system.collections.generic.list[string]" $operation = [System.Net.WebRequestMethods+Ftp]::ListDirectory $reader = GetStream $creds $fld $operation while (($line = $reader.ReadLine()) -ne $null) { [void]$newFiles.Add($line.Trim()) } $reader.Dispose(); return ,$newFiles } function GetStream($creds, $url, $meth){ $ftp = [System.Net.WebRequest]::Create($url) $ftp.Credentials = $creds $ftp.Method = $meth $response = $ftp.GetResponse() return New-Object IO.StreamReader $response.GetResponseStream() } Export-ModuleMember UploadToFtp, DownLoadFromFtp 
+6
Apr 01 2018-11-11T00:
source share

I know this is an old question, but I wanted to add something to the answers already here, hoping to help someone else.

You can script the ftp command with the -s:filename parameter. The syntax is just a list of commands to pass to the ftp shell, each of which ends with a newline. This page has a nice link to commands that can be executed using ftp .

Download / Download the entire directory structure

Using regular ftp does not work very well when you need to have a complete directory tree copied to or from an ftp site. Therefore, you can use something similar to handle these situations.

These scripts work with the Windows ftp command and allow you to load and load entire directories from a single command. This makes it quite independent when used in different systems.

Basically, they display the directory structure to be loaded / loaded, issue the appropriate ftp commands to the file, and then execute these commands when the mapping is complete.

ftpupload.bat

 @echo off SET FTPADDRESS=%1 SET FTPUSERNAME=%2 SET FTPPASSWORD=%3 SET LOCALDIR=%~f4 SET REMOTEDIR=%5 if "%FTPADDRESS%" == "" goto FTP_UPLOAD_USAGE if "%FTPUSERNAME%" == "" goto FTP_UPLOAD_USAGE if "%FTPPASSWORD%" == "" goto FTP_UPLOAD_USAGE if "%LOCALDIR%" == "" goto FTP_UPLOAD_USAGE if "%REMOTEDIR%" == "" goto FTP_UPLOAD_USAGE :TEMP_NAME set TMPFILE=%TMP%\%RANDOM%_ftpupload.tmp if exist "%TMPFILE%" goto TEMP_NAME SET INITIALDIR=%CD% echo user %FTPUSERNAME% %FTPPASSWORD% > %TMPFILE% echo bin >> %TMPFILE% echo lcd %LOCALDIR% >> %TMPFILE% cd %LOCALDIR% setlocal EnableDelayedExpansion echo mkdir !REMOTEDIR! >> !TMPFILE! echo cd %REMOTEDIR% >> !TMPFILE! echo mput * >> !TMPFILE! for /d /r %%d in (*) do ( set CURRENT_DIRECTORY=%%d set RELATIVE_DIRECTORY=!CURRENT_DIRECTORY:%LOCALDIR%=! echo mkdir "!REMOTEDIR!/!RELATIVE_DIRECTORY:~1!" >> !TMPFILE! echo cd "!REMOTEDIR!/!RELATIVE_DIRECTORY:~1!" >> !TMPFILE! echo mput "!RELATIVE_DIRECTORY:~1!\*" >> !TMPFILE! ) echo quit >> !TMPFILE! endlocal EnableDelayedExpansion ftp -n -i "-s:%TMPFILE%" %FTPADDRESS% del %TMPFILE% cd %INITIALDIR% goto FTP_UPLOAD_EXIT :FTP_UPLOAD_USAGE echo Usage: ftpupload [address] [username] [password] [local directory] [remote directory] echo. :FTP_UPLOAD_EXIT set INITIALDIR= set FTPADDRESS= set FTPUSERNAME= set FTPPASSWORD= set LOCALDIR= set REMOTEDIR= set TMPFILE= set CURRENT_DIRECTORY= set RELATIVE_DIRECTORY= @echo on 

ftpget.bat

 @echo off SET FTPADDRESS=%1 SET FTPUSERNAME=%2 SET FTPPASSWORD=%3 SET LOCALDIR=%~f4 SET REMOTEDIR=%5 SET REMOTEFILE=%6 if "%FTPADDRESS%" == "" goto FTP_UPLOAD_USAGE if "%FTPUSERNAME%" == "" goto FTP_UPLOAD_USAGE if "%FTPPASSWORD%" == "" goto FTP_UPLOAD_USAGE if "%LOCALDIR%" == "" goto FTP_UPLOAD_USAGE if not defined REMOTEDIR goto FTP_UPLOAD_USAGE if not defined REMOTEFILE goto FTP_UPLOAD_USAGE :TEMP_NAME set TMPFILE=%TMP%\%RANDOM%_ftpupload.tmp if exist "%TMPFILE%" goto TEMP_NAME echo user %FTPUSERNAME% %FTPPASSWORD% > %TMPFILE% echo bin >> %TMPFILE% echo lcd %LOCALDIR% >> %TMPFILE% echo cd "%REMOTEDIR%" >> %TMPFILE% echo mget "%REMOTEFILE%" >> %TMPFILE% echo quit >> %TMPFILE% ftp -n -i "-s:%TMPFILE%" %FTPADDRESS% del %TMPFILE% goto FTP_UPLOAD_EXIT :FTP_UPLOAD_USAGE echo Usage: ftpget [address] [username] [password] [local directory] [remote directory] [remote file pattern] echo. :FTP_UPLOAD_EXIT set FTPADDRESS= set FTPUSERNAME= set FTPPASSWORD= set LOCALDIR= set REMOTEFILE= set REMOTEDIR= set TMPFILE= set CURRENT_DIRECTORY= set RELATIVE_DIRECTORY= @echo on 
+2
Apr 22 '15 at 7:28
source share

I had a similar problem - like the original poster, I wanted to automate the file upload, but I could not figure out how to do this. Since this is located at the registration terminal in my family store, I did not want to install powershell (although it looks like a simple option), I just need a simple .bat file for this. This is pretty much what literacy and another user said; I am new to this, therefore a more detailed example and explanation (thanks also http://www.howtogeek.com/howto/windows/how-to-automate-ftp-uploads-from-the-windows-command-line/ , which explains how to do this using only one .bat file.)

Essentially you need 2 files - one .bat and one .txt. A byte tells ftp.exe which switches to use. TXT gives a list of ftp.exe commands. In a text file, put this:

 username password cd whereverYouWantToPutTheFile lcd whereverTheFileComesFrom put C:\InventoryExport\inventory.test (or your file path) bye 

Save this place where you want. In the BAT file, put:

 ftp.exe -s:C:\Windows\System32\test.txt destinationIP pause 

Obviously, change the path after -s: anywhere in your text file. Take a break when you start it, it's just so you can see any errors. Of course, you can use "get" or any other ftp command in the .txt file to do whatever you need.

I'm not sure if you need the lcd command in a text file, for example, I said that I am new to using the command line for this type of thing, but this works for me.

+1
Aug 07 '13 at 18:02
source share

Try it manually:

 $ ftp www.domainhere.com > useridhere > passwordhere > put test.txt > bye > pause 
0
02 Sep '09 at 18:08
source share

I had the same problem and it was solved with a solution similar to what Cheeso suggested above.

"does not work, says the password srequire, tried this in several different ways"

Yes, because FTP sessions through a batch file do not require the username to be placed in the "user" line. Take it off and try it.

Or you could see this because your FTP command file is incorrectly encoded (this is also a bit). This is the crappy part about creating an FTP command file at runtime. The out-file Powershell cmdlet does not have the encoding option that Windows FTP accepts (at least not the one I could find).

No matter how WebClient.DownloadFile does, this is the way to go.

0
Sep 28 '09 at 17:01
source share

This script creates a batch file, then passes the command file to the ftp program, creating a log in the path. Finally, print the source bat file, command files, and the log for this session.

 @echo on @echo off > %0.ftp ::== GETmy!dir.bat >> %0.ftp echo a00002t >> %0.ftp echo iasdad$2 >> %0.ftp echo help >> %0.ftp echo prompt >> %0.ftp echo ascii >> %0.ftp echo !dir REPORT.CP1C.ROLLEDUP.TXT >> %0.ftp echo get REPORT.CP1C.ROLLEDUP.TXT >> %0.ftp echo !dir REPORT.CP1C.ROLLEDUP.TXT >> %0.ftp echo ************************************************* >> %0.ftp echo !dir CONTENT.CP1C.ROLLEDUP.TXT >> %0.ftp echo get CONTENT.CP1C.ROLLEDUP.TXT >> %0.ftp echo !dir CONTENT.CP1C.ROLLEDUP.TXT >> %0.ftp echo ************************************************* >> %0.ftp echo !dir WORKLOAD.CP1c.ROLLEDUP.TXT >> %0.ftp echo get WORKLOAD.CP1C.ROLLEDUP.TXT >> %0.ftp echo !dir WORKLOAD.CP1C.ROLLEDUP.TXT >> %0.ftp echo ************************************************* >> %0.ftp echo !dir REPORT.TMMC.ROLLEDUP.TXT >> %0.ftp echo get REPORT.TMMC.ROLLEDUP.TXT >> %0.ftp echo !dir REPORT.TMMC.ROLLEDUP.TXT >> %0.ftp echo ************************************************* >> %0.ftp echo !dir CONTENT.TMMC.ROLLEDUP.TXT >> %0.ftp echo get CONTENT.TMMC.ROLLEDUP.TXT >> %0.ftp echo !dir CONTENT.TMMC.ROLLEDUP.TXT >> %0.ftp echo ************************************************** >> %0.ftp echo !dir WORKLOAD.TMMC.ROLLEDUP.TXT >> %0.ftp echo get WORKLOAD.TMMC.ROLLEDUP.TXT >> %0.ftp echo !dir WORKLOAD.TMMC.ROLLEDUP.TXT >> %0.ftp echo quit ftp -d -v -s:%0.ftp 150.45.12.18 > %0.log type %0.bat type %0.ftp type %0.log 
0
Nov 25 '13 at 19:55
source share



All Articles