Automated uploading of files to FTP and sending files by email. Automated uploading files to FTP and sending files by email Downloading a folder from ftp bat

The FTP protocol is one of the common network protocols used by Internet users. The overwhelming majority of people use various FTP clients to download anything from FTP servers. But not many people know that Windows allows you to connect to FTP servers from the command line.

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

Step-by-step instruction:

In order to download file from FTP from Windows command line, let's perform a few simple steps:

  1. Open the Windows command prompt by clicking Start - Run, typing cmd and pressing Enter.
  2. Run the following command: Using this command, we will connect to the Microsoft ftp server.
  3. Enter the username anonymous. The server of this well-known company allows anonymous users to connect:
  4. Enter your 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. To go to the required directory, use the cd command:
  9. The message "250 CWD command successeful" will indicate that we have successfully moved 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 as an alternative), you can use simple DOS commands that will allow you download files from ftp at no additional cost.

I needed to send report files to an FTP server hourly and also duplicate the sending of these files by mail to several recipients. After some research I settled on the built-in utility ftp.exe (C:\Windows\System32\ftp.exe) and 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) to the ftp.server.com server every hour.
New version in Python

Setting up file upload to FTP

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

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

Where:
C:\WINDOWS\system32\ftp.exe- Path to the 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- enter 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 prevent files from being sent again? 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 in which the XML files are stored.
del *.xml- delete all XML files.

Setting up sending files by email

To send files by email, we use the blat utility (http://www.blat.net/). Download and Unpack to any directory, in our case it is c:\%dir\blat/
Installation blat. 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 emails will be sent.
[email protected]- sender's name, which will be displayed in the field.
3 - the number of attempts to resend the letter if the connection to the server fails.
25 - SMTP server port number.
profile- profile name.
[email protected]- username that will be used when connecting to the SMTP server.
Your password- password used when connecting to the SMTP server, i.e. mailbox password.

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 write:

Where:
-p profile– profile name blat (Specified during installation)
-charset windows-1251(Select Windows encoding)
-to [email protected] – address of the letter recipient
-subject "File export"- Letter subject
-body "Auto export"– Body of the letter
-attach c:\%dir%\file_export\*.xml– files or file that needs to be sent
If you need to send letters to several mailboxes, then 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 a file export.bat in which we have the 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 email, then go to the directory where the files are stored and delete them.

Sending files on a schedule

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.

Bottom line

As a result, we have an automated system for sending files to an FTP server and email every hour.

It is often necessary to automate the processes of downloading, uploading and deleting files from an FTP server. For example, when working with distributed 1C databases or for saving backup copies. But not everyone knows that these procedures can be performed from the command line without resorting to additional software, that is, Windows tools. Below I will provide the command syntax for working with FTP, as well as examples of bat files for these operations.

1.ftp command

To exchange files with an FTP server, use the ftp command, here is its syntax:

FTP[-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer]
[-r:recvbuffer] [-b:asyncbuffers] [-w:windowsize] [node]

-v Disable display of responses from a remote server.
-n Disable automatic login upon initial connection.
-i Disabling interactive requests when sending multiple
files.
-d Enable debug mode.
-g Disable file name globalization (see GLOB command).
-s:filename Specify a text file containing FTP commands that
will be executed automatically when you start FTP.
-a Using the local interface to bind the connection.
-A Anonymous login to the service.
-x:send sockbuf Override the default buffer size SO_SNDBUF (8192).
-r:recv sockbuf Override the default buffer size SO_RCVBUF (8192).
-b:async count Overriding the default async counter size (3)
-w:windowsize Override the default transmit buffer size (65535).
node Specifying the name or IP address of the remote node,
to which you want to connect.

As you can see, there are no operators for connecting to the server and working with files. The thing is that this command only starts the ftp session:

!
Temporary transition to the shell.

append
Adding to file.

ascii
Setting the transfer mode for files in ascii format.

bell
Sound signal when command completes

binary
Set the binary file transfer mode.

bye
End the ftp session and log out.

CD <удаленный_каталог>
Changing the working directory on the remote computer where:
<удаленный_каталог> — the name of the directory that will become working.

close
Ending the ftp session.

debug
Switching debugging mode.

delete <удаленный_файл>
Deleting a file on a remote computer where:
<удаленный_файл> — name of the file to be deleted.

dir [ <удаленный_каталог> ] [<локальный_файл> ]
Displays the contents of the remote computer's directory, where:
<удаленный_каталог>
<локальный_файл>

disconnect
Ending the ftp session.

get <удаленный_файл> [<локальный_файл> ]
Getting a file where:
<удаленный_файл>
<локальный_файл> — file name on local computer .

glob
Switching the metacharacter extension of local file names.

hash
Switches the "#" output for each data block transferred.

help [ <команда> ]
Display help information for the ftp command, where:
<команда> — The command whose description will be displayed, if not specified, all commands will be output.

lcd [ <локальный_каталог> ]
Changing the local directory of the working computer, where:
<локальный_каталог> — The name of the new local directory; if not specified, the name of the current directory will be used.

literal <команда_1> … <команда_n>

<команда_n> — commands to send;

ls [<remote_directory>] [<local_file>]
Displays the abbreviated contents of the remote computer's directory, where:
<удаленный_каталог> — directory whose contents will be displayed; if not specified, the current directory is used;
<локальный_файл> — specifies a local file to save to the list; if not specified, the list is displayed on the screen.

mdelete<remote_file_1> … <remote_file_n>
Deleting multiple files on a remote computer where:
<удаленный_файл_n> — names of files to delete.

mdir<remote_directory_1> … <remote_directory_n> <local_file>
Print the contents of several directories on a remote computer, where:
<удаленный_каталог_n> — the directory whose contents will be displayed;
<локальный_файл> — specifies a local file to save to the list; if not specified, the list is displayed on the screen.

mget <удаленный_файл_1> <удаленный_файл_n>
Receiving multiple files where:
<удаленный_файл_n> — remote file to copy.

mkdir <удаленный_каталог>
Create a directory on the remote computer where:
<удаленный_каталог> — name of the remote directory to create.

mls<remote_directory_1> ... <remote_directory_n> <local_file>
Print the shortened contents of several directories on a remote computer, where:
<удаленный_каталог_n> — directory whose contents will be displayed; if not specified, the current directory is used;
<локальный_файл> — specifies a local file to save to the list.

mput <локальный_файл_1> … <локальный_файл_n>
Sending multiple files where:
<локальный_файл_n> — the name of the local files that will be copied.

prompt
Toggle interactive tooltip for compound commands.

put <локальный_файл> [<удаленный_файл> ]
Sending one file, where:
<local_file>
<удаленный_файл>

pwd
Displays the working directory of the remote computer.

quit
End the ftp session and exit to the command line.

quote <команда>
Send an arbitrary ftp command, where:
<команда> — command to send.

recv <удаленный_файл> [<локальный_файл> ]
Retrieving a file using the current file type settings, where:
<удаленный_файл> — remote file for copying;
<local_file> — file name on the local computer .

remotehelp [ <команда> ]
Obtaining help information about commands on a remote system, where:
<team> — remote system command; if not specified, a list of all commands is displayed.

rename <имя_файла> <новое_имя_файла>
Renaming a remote file, where:
<file name> — file name to rename;
<new_file_name> — new file name.

rmdir <имя_каталога>
Deleting a directory on a remote computer where:
<directory_name> — name of the directory to delete.

send <локальный_файл> [<удаленный_файл> ]
Copy a single file using the current file type settings, where:
<local_file> — name of the local file to copy;
<удаленный_файл> — file name on the remote computer.

status
Displays the current ftp connection status.

trace
Toggle packet tracing.

type [ <имя_типа> ]
Setting the file transfer type, where:
<type_name> — file transfer type; if not specified, the current file transfer type will be displayed.

user <имя_пользователя> [<пароль> ] [<учетная_запись> ]
Send information to connect to a remote computer where:
<Username> — username for connecting to the remote computer;
<password> — password for the specified username, if not specified but required for connection, the ftp command will ask the user for it;
<Account> — an account for connecting to a remote computer, if not specified but required for connection, the ftp command will ask the user for it;

verbose
Switching the message output mode.


Let's look at a small example.

First, let’s write a “body file” that will upload to the server website file file_data.dat from " C:\example". The algorithm for the bat file will be as follows:

  • Create a transport.txt file with a sequence of ftp statements;
  • We execute the ftp command, specifying the created file as parameters;
  • Delete transport.txt.

In this example, the file name and directory are hardcoded into the file. This is not always convenient. Let’s modify the bat file so that it receives the data to be loaded as parameters, and we’ll also put all changeable values ​​into file variables. We get the following code ():

Accordingly, to copy the file file_data.dat from " C:\example" to the FTP server in " Temp\Backup", you will need to run this batch file, specifying the file name, local and remote directories as parameters.

In a similar way, you can write bat files for and on an FTP server, as well as an executable file with an arbitrary set of instructions.

Did this article help you?

To connect to an FTP server, it is not necessary to use any third-party programs - this can be done using standard Windows features. To do this, you can use the CMD command line. But to manage the server through the standard operating system capabilities, you will have to study the available commands, since CMD does not have a visual interface. In this article, you will learn the basic FTP commands and how to put them into practice.

Why is it better to download the client than to use CMD?

If the CMD command line allowed you to perform all available operations on FTP servers, then perhaps there would be no need for clients. But the reality is that the CMD line is very limited in terms of server management. Therefore, it’s better to find an opportunity and download to your computer a utility for managing the server via FTP. For example, download the FileZilla client program to your PC - it can be downloaded for free from the official website of the developers. It won't take much time to install. And to connect to the server, you will only need to enter your login, password and host name in the required line. It's much more difficult to do this via CMD.

The specific disadvantage of the CMD line is that you won't even be able to go into passive mode while on the server. And without this mode, you will not be able to exchange files with FTP servers that operate through NAT.

In fact, this is a very big flaw in CMD. Therefore, accept the fact that the Windows command line is only suitable for manipulations within the file system without exchanging data. Although for those who use an FTP server for routine, monotonous work, the command line may seem very convenient. But it’s better to try both the client and CMD to draw any conclusions.

What are the commands to manage FTP in the CMD prompt?

To get started, launch the command line. You can do this by searching in Start - find the cmd.exe file and open it, after which the line will be launched.

To connect to the server, you need to use the OPEN command. After this command, you must enter the IP address or hostname with which you want to set up a connection. After entering this request, you will be able to log in to the server. This can be done using the USER command, which makes a request for authentication through the entered login. Then you need to enter the PASS command, which means password. When you enter your password and press “Enter,” you will be authorized on the server.

In general, the command line is only suitable for “surfing” the server, moving files, etc. First, you will need to see the files that are on the server. To do this, use the LS command. Since there is no cursor on the command line, you need to select a directory or file using special commands. Use the CD or LCD line for this - the first is needed to change the current object on the remote computer, and the second is needed for the local one, that is, for your PC.

Although you will not be able to upload your files to the FTP server via CMD, it is possible to download data to your computer. To do this, use the GET command. First, write the GET command with the file name, and then indicate which directory on your computer is the current one, so that the server will transfer the data to that exact folder.

These commands are used most often. That is, most users manage the server via CMD in order to quickly download files without first installing the client. But you can go much further and use other commands on the server using the CMD prompt. For example, you can add a command completion signal to the server using the BELL string.

If you want to end the session and exit the command line and the server, then enter BYE. And to interrupt the control connection only on the server, so that you remain inside the command line, write CLOSE. In this case, the files that you download to your PC from the server will continue to be downloaded and after the download is complete, the session will be completely closed.

Using CMD you can easily manipulate directories on the server. For example, to delete the specified directory, use the DELETE command. To have a list of files in a specific folder appear on the screen, enter DIR and the path to the directory. If you want to delete not one folder or file, but several at once, then use the MDELETE command, that is, multi-deletion. Similarly, the prefix "m" can be used for other commands so that multiple actions can be combined into one. So, to display a list of files in several directories at once, write MDIR, and to download batches of data from the server, use MGET. But do not confuse the multi-command with a single MKDIR - it is needed in order to generate a directory on the server.

There is a possibility that your FTP server operates without NAT technology, so passive client mode is not needed. In this case, you will be able to upload certain files to the host. Use the MPUT command to upload multiple files, and PUT if you only want to upload one.

Another popular command that will most likely be useful to you is RENAME. Need a command to rename files. It will be useful for those who use the server as a storage for site data. You will also need the PWD command, which determines the current folder on the remote FTP server, not on your computer, as the LCD line does.

The following commands are a partial list of functions you can use in FTP. There are other teams too. And you can find out about them right in the command line if you enter the HELP query. After this, a directory will appear with all the available CMD commands that can be used in FTP. Just keep in mind that at this moment you must be connected to the server, otherwise Windows will give you a different directory. And if you want to request help from the server itself, then write REMOTEHELP.

Of course, managing a server with commands is a stone age thing. Therefore, it is better not to waste time, download a normal FTP client to your computer and use the server to the maximum, and not partially. But practicing on the CMD command line never hurt anyone!

Team FTP implements file exchange with an FTP server in the Windows command line. Utility ftp.exe is included in the standard distribution kit of all versions of operating systems of the Windows family and allows you to exchange with the server both in command and interactive mode.

Command line format:

FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuffer] [-b :asyncbuffers] [-w:windowsize] [node]

-v- Disable display of responses from a remote server.
-n- Disable automatic login upon initial connection.
-I- Disable interactive requests when transferring multiple files.
-d- Enable debug mode.
-g- Disable globalization of file names (see GLOB command).
-s:filename- Specify a text file containing FTP commands that will be executed automatically when FTP starts.
-a- Using the local interface to bind the connection.
-A- Anonymous connection to the server.
-x:send sockbuf- Override the default buffer size SO_SNDBUF (8192).
-r:recv sockbuf- Override the default buffer size SO_RCVBUF (8192).
-b:async count- Override default async counter size (3)
-w:windowsize- Override the default transmit buffer size (65535).
node- Specifying the name or IP address of the remote host to which you want to connect.

Notes:

The mget and mput commands accept the y/n/q parameters as YES/NO/QUIT.
- to stop executing commands, press the key combination CTRL+C.

When running without parameters, FTP goes into interactive mode, waiting for user input. A prompt appears on the screen - ftp >.

To get a list of available commands, you can enter a question mark or the command help

Command set:

!
?
append
ascii
bell
binary
bye
CD
close
literal
debug
dir
disconnect
get
glob
hash
help
lcd
ls
mdelete
mdir
mget
mkdir
mls
mput
open
prompt
send
put
pwd
quit
quote
recv
remotehelp
rename
rmdir
status
trace
type
user
verbose

You can get help for a specific command by entering a question mark or help and the command name:

? connect
help connect

List of Windows FTP client commands:

! - Temporary transition from the FTP environment to the Windows command line (CMD.EXE). To return to FTP use the command EXIT
append adding the data of the transferred file to the end of the existing one.
ascii- setting the file transfer mode in ascii format (text mode)
bell- issuing a sound signal upon completion of the command.
binary- setting the file transfer mode in binary format
bye- end the FTP session and exit the program.
CD- changing the current directory on the remote computer
close- ending the FTP session (closing the connection opened by the command open)
delete- deleting the specified file on the remote computer
debug- switch to debug mode
dir
disconnect- ending the ftp session. Disconnecting from the FTP server without ending the program.
get- receiving a file from an FTP server
glob- Switching the metacharacter extension of local file names. By default, this mode is enabled and allows you to use symbols * And ? in filename patterns.
hash- switching the hash mark output mode for each transferred buffer
help- output of help information on FTP commands
lcd- change the working directory of the local computer
literal- sending an arbitrary string of characters as an ftp command
ls- output the contents of the directory of the remote computer
mdelete- deleting multiple files
mdir- output the contents of several FTP server directories
mget- receiving multiple files from an FTP server
mkdir- creating a directory on the FTP server
mls- output the contents of several directories on a remote computer
mput- sending multiple files to an FTP server open- open a connection (connect) to the FTP server
prompt- changing the line prompt for entering commands in interactive mode.
put- transfer one file
pwd- display the name of the current working directory on the FTP server
quit- end the session and exit the program
quote- sending an arbitrary command to the FTP server
recv- receiving a single file
remotehelp- receiving help from the FTP server
rename- renaming the file
rmdir- deleting a directory on the server
send- single file transfer
status- display of the current session state - which server is connected to, the type of data transferred, the state of modes changed by FTP commands
trace- switching tracing mode
type- setting the file transfer type
user- changing the username in an exchange session with an FTP server
verbose- switching the detail mode when displaying messages

As can be seen from the list of commands listed, the standard FTP client in Windows operating systems does not support a complete list of standard FTP commands and, what is most unpleasant, it does not support the command to switch to passive mode ( passv) exchange with an FTP server, which makes it unsuitable for exchanging data with FTP servers connected to using network address translation technology NAT. In other words, a standard FTP client is not suitable for exchanging files with servers external to the client’s subnet. This applies to all versions of Windows, including Windows 10. It is this fact that determines the scope of use of the ftp.exe utility - only on local networks where a direct TCP connection between the client and server is possible.

Example session using a Windows FTP client:

open 192.168.1.1- open a connection to server 192.168.1.1
user admin- use username admin when connecting to the server. In response to this command, the server will ask for a password.
adminpass- enter a password for the user admin
ls- display a list of files and folders in a remote directory
get myfile.txt- accept file myfile.txt from the remote directory to the current local directory.
lcd C:\files- change the current local directory.
get myfile.txt- accept file myfile.txt from the remote directory to the current local directory (C:\files) .

When using the Windows FTP client in batch files, it is often used to run the program in the mode of managing FTP commands written to a text file.

ftp -s:ftpcomm.txt- A list of commands is written to the ftpcomm.txt file, in the same order in which they would be entered in interactive mode. For example, to receive the file binfile.bin in the C:\temp directory.

Continuing the topic:
Linux

We study the web interface in more detail, and notice the section where they are produced. There you can either set a password or change it. As we see in the example of ASUS, in order...