Menu
Is free
registration
home  /  Installation and configuration/ Ftp commands with text file. Automated file upload to FTP and send files to email

Ftp commands with text file. Automated file upload to FTP and send files to email

Standard Windows utility "FTP-client" from the command line (CMD) - for backing up or downloading files without your participation (automatically).

  1. open - connect to a remote host via FTP;
  2. cd - change to another directory on the server;
  3. bin - binary mode of data transfer between server and client;
  4. put - transfer files to FTP;
  5. bye - end the session with the server and exit.

In order to master the windows FTP client and start working with it, let's try to connect to any FTP server in the console mode, for example 213.43.125.75.

We enter open 213.43.125.75 (this is the IP address of the FTP server), instead we enter the address or domain of the FTP server you need, you will be required to enter a username and password. After successful authorization, we create a folder for placing archives on FTP, without hesitation, we call it backup (the folder is created by the mkdir command). Now go to the directory we created with the following command: cd back-up. At the next stage, copy the file to the server, for which you need to set the file transfer mode to "binary" using the bin command without any parameters. And now we enter put D: /back-up/back-up.zip, where the parameter of the put command will be the full path to the copied file. When copying, you can specify a mask in the directory, for example: D: / back-up / *. Zip (* .zip - all files with the "zip" extension). After copying, close the connection to the server with the bye command.

To make our FTP client do everything automatically, without asking questions, let's create a script for it. Below is a text file (let's call it back-up.ftp and save it to the root of the C: / drive - for ease of use and writing to the startup line) containing a number of commands for the FTP client that will be automatically executed when the FTP client is started from command line.

Open 213.43.125.75 (instead of numbers - the address of the desired FTP server)
user
password
cd back-up (change to an existing directory on the FTP server)
bin (Transfer mode - binary)
put D: /back-up/back-up.zip (Path to file on your computer)
bye (End connection)

As a result, you should get a back-up.ftp file with the following content:

open 213.43.125.75
user
password
cd back-up
bin
put D: /back-up/back-up.zip

FTP is an essential TCP / IP utility for transferring files between systems. One of the main advantages of FTP is its compatibility with many different remote host systems: files can be transferred between remote Win-dows 2000, Windows NT and UNIX systems, and even IBM servers such as the AS / 400. To run the text-based FTP utility that comes with Windows 2000 and NT, just enter ftp at the command line.

Here are the 10 most useful FTP commands.

10. Help (or?). Many FTP commands can be entered in different ways. For example, Help and the question mark (?) Serve the same function. Beginners should start with the Help command, after entering which the system presents the user with a complete list of FTP commands. To get a short description of a command, enter? followed by the name of the command:

Ftp>? open

9. Open. An FTP session usually begins with an Open command that connects to the specified FTP server (the FTP service must be running on the remote site). Upon receiving the Open command, the system asks for a user ID and password. Many FTP servers allow you to log in with an anonymous ID and blank password. To communicate with a computer named teca2, enter

Ftp> open teca2

8. Quit (or Bye). The Quit command ends any open session and exits the FTP shell:

Ftp> quit

To close an open session without exiting FTP, use the Close command.

7. Pwd. The Pwd command displays the directory on the remote machine that is currently connected through an FTP session:

6. Ls. The Ls command displays a list of files and subdirectories located in the current directory on the remote computer. Ls allows you to access remote files:

5. Cd. If the file you want is not in the current directory of the remote system, you can use the Cd command to change to a different directory. When connected to a UNIX machine, the remote directory is usually specified with a slash to the right (in this case, the / downloads directory):

Ftp> cd / downloads

4. Lcd. The Lcd command changes the current directory on the local machine. Any files received from the remote machine will be written to the current directory by default. To change the current local directory to C: emp, enter the command

Ftp> lcd C: emp

3. Bin (or Binary). By default, FTP transfers files in ASCII format, which is convenient for text files. But to upload executable and .zip files, you need to change the upload type to binary using the Bin command:

To return to ASCII mode, use the ASCII command.

2. Put (or Send). The Put command allows you to copy a local file to the current directory of the remote machine. To upload a file named localfile.txt to the C: emp directory on the remote system, enter the command

Ftp> put C: emp localfile.txt

1. Get (or Recv). The Get command is used to download files from the Internet (or from remote machines) into the current directory of the local machine. To get a file named remotefile.txt, enter the command

Ftp> get remotefile.txt

Michael Ooty is Science Editor for Windows NT Magazine and President of TECA, a software development and consulting firm. You can contact him at:

FTP is one of the common network protocols used by Internet users. The overwhelming majority of people use various FTP clients to download something from FTP servers. But not many people know that in Windows, the ability to connect to FTP servers from the command line is available.

In this tip, you will learn how download files from FTP servers using simple DOS commands.

To download file from FTP from Windows command line, let's do a few simple steps:

  1. Open Windows Command Prompt by clicking on Start - Run, typing cmd and pressing Enter.
  2. Run the following command: With this command, we will connect to the ftp server of Microsoft.
  3. Enter the username anonymous. The server of this well-known company allows anonymous users to connect:
  4. Enter e-mail as a password (you can enter a non-existent one):
  5. Congratulations on your successful login!
  6. In order to see what files and directories are available on the ftp server, enter the dir command:
  7. As a result, we get a list of files and directories located on the ftp server:
  8. In order to go to the required directory, use the cd command:
  9. The message "250 CWD command successeful" will tell you that we have successfully changed to the directory we need:
  10. In order to download the file we need, enter the get command and the name of the downloaded file:
  11. After successfully receiving the file, the system will notify us:
  12. To leave the ftp server and disconnect from it, run the bye command:

Perhaps many will say that it is much more convenient to download files using multifunctional download managers or ftp clients .. But if necessary (or alternatively), you can use simple DOS commands that will allow you download files from ftp at no additional cost.

FTP (File Transfer Protocol) is a popular network protocol used to copy files from one computer to another on a local network or on the Internet. FTP is one of the oldest application protocols, dating back to long before HTTP, and even before TCP / IP, in 1971.

The FTP protocol is vulnerable, that is, FTP cannot encrypt its traffic, all transfers are clear text, so usernames, passwords, commands and data can be read by anyone who can intercept the packet over the network. For secure data transfer, SFTP (Secure File Transfer Protocol) is used. Unlike standard FTP, it encrypts both commands and data, preventing passwords and confidential information from being transmitted openly over the network. SFTP is similar in functionality to FTP, but since it uses a different protocol, standard FTP clients cannot communicate with the SFTP server and vice versa. Next, we will consider the basic commands for working with an FTP program.

An FTP client is included with most Linux distributions. Let's start by launching the program and ftp connection and, of course, we will look at the basic commands for downloading from a ftp server and uploading to ftp, creating directories, deleting files, etc. In this article we will describe only the basic commands, and at the end of the article we will provide a help and manual from the console - you can always find out about the purpose of the command and its syntax, as well as about all the available commands on a specific ftp server.

FTP connection

To start the FTP connection, just enter the command ftp<сервер> for example:

ftp test.hostingthutor.com

After pressing the enter key, the command output will be as follows:

Connected to test..55.5.11).
220 test ..
Name (test.site: ftpuser):

Another way to connect is to launch ftp from the console, and then connect to the ftp server using the command open:

ftp
ftp> test.site

In addition, it is possible to connect via ip:

ftp 114.55.5.11

Or with such an appeal ftp This e-mail is protected from spambots. To view it, Java-script support must be enabled in your browser, that is:

ftp This e-mail is being protected from spambots. To view it, Java-script support must be enabled in your browser.
230 User ftpuser logged in
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

From the message, you can see that a binary (binary) transfer type is used to transfer files. Binary file transfer mode is the transfer of files as they are stored on the FTP server. Ascii (text) mode is used to transfer only text files. You can enter commands ascii or binary to switch between transmission modes. Binary mode must be used for all non-text file types - images, archives, programs, etc.

So, let's move on to the commands for navigating and navigating through the ftp server directories:

pwd - the command will show the current directory on the ftp server:

ftp> pwd
257 "/" is the current directory

ls - the command will show a list of files and directories in the current directory:

ftp> ls
227 Entering Passive Mode.
150 Opening ASCII mode data connection for file list
-rw-r - r-- 1 ftpuser ftpuser 3034978 Jun 31 19:02 file1.tar.gz
-rw-r - r-- 1 ftpuser ftpuser 30842294 Jul 31 20:08 file2.tar.gz
-rw-r - r-- 1 ftpuser ftpuser 67798316 Jul 31 19:46 file3.tar.gz
-rw-r - r-- 1 ftpuser ftpuser 6001252 Jan 17 12:02 file4.zip
-rw-r - r-- 1 ftpuser ftpuser 31386394 Jan 17 11:28 file5.tar.gz
drwxr-xr-x 2 ftpuser ftpuser 4 Jan 17 20:23 www
-rw-r - r-- 1 ftpuser ftpuser 48546694 Jan 17 11:33 file6.zip
226 Transfer complete

cd<имядиректории> - the command to go to the desired directory:

ftp> cd www
250 CWD command successful

Checking with the team pwd :

ftp> pwd
257 "/ www" is the current directory

mkdir<имя директории> - creating a new directory (directory):

ftp> mkdir tmp
257 "/ tmp" - Directory successfully created

rmdir<имя директории> - deleting a directory (directory):

ftp> rmdir tmp
250 RMD command successful

Deleting files on ftp server

delete<имяфайла> - deletes a file on a remote ftp server:

ftp> delete test1.sql
250 DELE command successful

Downloading files from ftp

get - download the file to the local machine. get fileName or get fileName newFileName

ftp> get file.zip
local: file.zip remote: file.zip
227 Entering Passive Mode.

226 Transfer complete
486694 bytes received in 0.229 secs (6.5e + 04 Kbytes / sec)

Download file.zip to your local machine as file2.zip:

ftp> get file.zip file2.zip
local: file2.zip remote: file.zip
227 Entering Passive Mode.
150 Opening BINARY mode data connection for file.zip (486694 bytes)
226 Transfer complete
486694 bytes received in 0.306 secs (9.4e + 04 Kbytes / sec)

Using the command get files from the remote ftp server are copied to the current local directory. To change the current local directory you need to use the command lcd:

lcd<путь> - change the current directory on the local machine:

ftp> lcd / root
Local directory now / root

To download multiple files from remote ftp server to the local machine, you can use the command mget .

ftp> mget * .sql
mget test2.sql? y
227 Entering Passive Mode.
(23957080 bytes)
226 Transfer complete
23957080 bytes received in 0.233 secs (1e + 05 Kbytes / sec)
mget test1.sql? y
227 Entering Passive Mode.
(11873185 bytes)
226 Transfer complete
11873185 bytes received in 0.135 secs (8.6e + 04 Kbytes / sec)

The download of each file must be confirmed (yes / no) y /n .

Another download option mget :

ftp> mget test1.sql test2.sql
mget test1.sql? y
227 Entering Passive Mode.

226 Transfer complete
11873185 bytes received in 0.101 secs (1.1e + 05 Kbytes / sec)
mget test2.sql? y
227 Entering Passive Mode.

226 Transfer complete
23957080 bytes received in 0.204 secs (1.1e + 05 Kbytes / sec)

Uploading file to ftp server

put<имяфайла> - command for uploading one file to ftp server.

ftp> put test1.sql
local: test1.sql remote: test1.sql
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql
226 Transfer complete
11873185 bytes sent in 0.129 secs (9e + 04 Kbytes / sec)

For upload multiple files you can immediately use the command mput :

ftp> mput test1.sql test2.sql
mput test1.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql
226 Transfer complete
11873185 bytes sent in 0.0964 secs (1.2e + 05 Kbytes / sec)
mput test2.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test2.sql
226 Transfer complete
23957080 bytes sent in 0.354 secs (6.6e + 04 Kbytes / sec)

The download of each file must be confirmed y / n (Not really).

Another command option mput :

ftp> mput * .sql
mput test1.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql
226 Transfer complete
11873185 bytes sent in 0.0985 secs (1.2e + 05 Kbytes / sec)
mput test2.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test2.sql
226 Transfer complete
23957080 bytes sent in 0.2 secs (1.2e + 05 Kbytes / sec)

If large files are uploaded to FTP, it would be nice to watch the progress of the download. To do this, you can use the commands hash and tick .

hash - the command after which ftp will print the "#" character every 1024 bytes of data:

ftp> hash
Hash mark printing on (1024 bytes / hash mark).
put file2.tar.gaz
##########################
226 Transfer complete
785888111 bytes sent in 6.94 secs (1.1e + 05 Kbytes / sec)

tick - the command will display the byte counter:

ftp> tick
Hash mark printing off.
Tick ​​counter printing on (10240 bytes / tick increment).
ftp> put file2.tar.gz
local: file2.tar.gz remote: file2.tar.gz
227 Entering Passive Mode.
150 Opening BINARY mode data connection for file2.tar.gz
Bytes transferred: 912706618 -> counter
226 Transfer complete
912706618 bytes sent in 8.08 secs (1.1e + 05 Kbytes / sec)

That's all the basic set of commands for working with ftp in the console. To view a list of available commands on a given FTP server, you can use the command help :

ftp> help
Commands may be abbreviated. Commands are:

Debug mdir sendport site
$ dir mget put size
account disconnect mkdir pwd status
append exit mls quit struct
ascii form mode quote system
bell get modtime recv sunique
binary glob mput reget tenex
bye hash newer rstatus tick
case help nmap rhelp trace
cd idle nlist rename type
cdup image ntrans reset user
chmod lcd open restart umask
close ls prompt rmdir verbose
cr macdef passive runique?
delete mdelete proxy send

Also, you can get a short help for each command. help<команда> :

ftp> help status
status show current status

ftp> help quit
quit terminate ftp session and exit

ftp> help bye
bye terminate ftp session and exit

And finally, the two teams that are higher quit or bye to close ftp session and exit:

ftp> quit
221 Goodbye.

Detailed information with a description of the commands can be obtained using man ftp at the command line.

# man ftp
Formatting page, please wait ...
FTP (1) BSD General Commands Manual FTP (1)

NAME
ftp - Internet file transfer program

SYNOPSIS
ftp [-Apinegvd]
pftp [-Apinegvd]
................
...............

It took me hourly to send report files to the FTP server and also duplicate sending these files in mail to several recipients. After some research I settled on the built-in ftp.exe utility (C: \ Windows \ System32 \ ftp.exe) and the good old blat. So, let's begin. We need to upload all XML files from the% dir% \ file_export directory (where% dir% is the path to our folder) on an hourly basis to the ftp.server.com server.
New Python version

Setting up uploading files to FTP

Create a command file for ftp.exe
Create a text document (for example ftpcomm.txt) in it we write:
login
password
cd IN
binary

bye
where:
login- ftp server login
pasword- password for ftp server
cd IN- go to the desired folder
binary- install the binary way of transferring files. In this case, the file is not recoded during transmission and is recorded unchanged. This is the most reliable way to transfer files.
mput C: \% dir% \ file_export \ *. xml- copy one or more local files to a remote computer indicating the directory
bye- end of FTP session and exit from FTP server
Create export.bat file
We prescribe in it:

where:
C: \ WINDOWS \ system32 \ ftp.exe- Path to ftp.exe utility
-s: c: \% dir% \ ftpcomm.tx t - set the path to a text file containing FTP commands, the commands will be automatically launched after FTP starts.
-i- disable interactive mode when transferring multiple files.
ftp.server.com- we register the address of our FTP server.
We check the work by running export.bat. We see that the files have been uploaded to the FTP server. But here a problem arises, if we need to upload files once an hour, how can we exclude re-sending of files? The solution is trivial:
Add to the export.bat file:
cd / d C: \% dir% \ file_export \
del * .xml
where:
cd / d C: \% dir% \ file_export \- go to the directory where the XML files are stored.
del * .xml- delete all XML files.

Setting up sending files to email

To send files to mail, we use the blat utility (http://www.blat.net/). Download and Unpack to any directory, in our case it is c: \% dir \ blat /
Installing blat. We call the command line. On the command line, go to our folder:
cd c: \% dir% \ blat /
We start the blat installation (we register the blat profile in the registry, the data is written to the Windows system registry in the branch: .
blat.exe -install smtp.yandex.ru [email protected] 3 25 profile [email protected] Your_Password
Where:
smtp.yandex.ru- the name of the mail server through which the sending of emails will be performed.
[email protected]- the name of the sender, which will be displayed in the field.
3 - the number of attempts to re-send a message if the connection to the server fails.
25 - port number of the SMTP server.
profile- profile name.
[email protected]- username that will be used when connecting to the SMTP server.
Your password- the password used when connecting to the SMTP server, i.e. password for the mailbox.

Adding sending emails to export.bat

In the export.bat file after the line
C: \ WINDOWS \ system32 \ ftp.exe -s: c: \% dir% \ ftpcomm.txt -i ftp.server.com
We register:

where:
-p profile- blat profile name (Specified during installation)
-charset windows-1251(Choosing Windows encoding)
-to [email protected] - address of the recipient of the letter
-subject "File export"- Letter subject
-body "Auto export"- Body of the letter
-attach c: \% dir% \ file_export \ *. xml- files or file to be sent
If you need to send letters to several mailboxes, then we write this line several times, indicating the recipient's mailboxes:
blat.exe -p profile -charset windows-1251 -to [email protected]-subject "File export" -body "Auto export" -attach c: \% dir% \ file_export \ *. xml
blat.exe -p profile -charset windows-1251 -to [email protected]-subject "File export" -body "Auto export" -attach c: \% dir% \ file_export \ *. xml
blat.exe -p profile -charset windows-1251 -to [email protected]-subject "File export" -body "Auto export" -attach c: \% dir% \ file_export \ *. xml

Final export.bat file

As a result, we get the export.bat file in which we have the following lines:
C: \ WINDOWS \ system32 \ ftp.exe -s: c: \% dir% \ ftpcomm.txt -i ftp.server.com
blat.exe -p profile -charset windows-1251 -to [email protected]-subject "File export" -body "Auto export" -attach c: \% dir% \ file_export \ *. xml
cd / d C: \% dir% \ file_export \
del * .xml
In the first line we send the files to the FTP server, in the second we send the files by e-mail, then go to the directory where the files are stored and delete them.

Scheduled File Sending

Let me remind you that we need to send files hourly, for this we create a task in the Windows scheduler. Where we indicate that we need to run export.bat once an hour.

Outcome

As a result, we have an automated system of hourly sending files to an FTP server and e-mail.