Hardware and software setup

Automated file upload to FTP and send files to email. Access FTP server from Windows command line Ftp command with text file

We previously used FileZilla Client, a free FTP client for Windows that can be used to access and transfer files to your FTP server. We have also seen how we can access an FTP server using NotePad++, a Notepad alternative for Windows. In this post, we will discuss how to access FTP from command line.

The Windows command line allows you to access the server using the FTP command. By connecting to the server, you can transfer files and download files from your PC simply by using commands. I will also list some FTP commands that will come in handy over time.

Accessing FTP Servers from the Command Line

Here are the steps that will show you how to use FTP from the command line:

March 2020 update:

We now recommend using this tool for your error. In addition, this tool fixes common computer errors, protects you from file loss, malware, hardware failures and optimizes your PC for maximum performance. You can quickly fix problems with your PC and prevent other programs from appearing with this software:

  • Step 1: Download PC Repair & Optimizer Tool(Windows 10, 8, 7, XP, Vista - Microsoft Gold Certified).
  • Step 2: Click " Start Scan' to find problems Windows Registry, which can cause problems with the PC.
  • Step 3: Click " fix everything' to fix all problems.

step1: Launch a command prompt and change the directory where all your files are located. Because this is where you can move your files to the server and upload them to the same folder.

step2: Enter command

ftp domain name

Example: ftp azharftp.clanteam.com

Step 3: Enter your username and password.

Step 4: You see the connection configuration. Now you can perform actions on the server.

These are the FTP commands:

You can use help to get full list FTP commands. This command does not require a connection to a remote system.

  • Help: Requests a list of all available FTP commands.
  • ASCII: activate Ascii mode.
  • Status: to see how the current FTP session is configured.
  • clue: Activate and deactivate interactive mode.
  • ls: list of directories equivalent to you.
  • ls-l: long list of directories, more details.
  • pwd: Show current directory name
  • CD: Change directory.
  • lcd: changes the local current directory.
  • receive: download file from FTP server.
  • put: file to the server.
  • mget: upload multiple files from FTP server.
  • mput: upload multiple files to FTP server.
  • binary: activate binary mode.
  • Delete: delete any file on the FTP server.
  • mkdir: create a directory on the FTP server.
  • ASCII: Sets the file transfer mode to ASCII (Note: This is the default mode for most FTP programs).
  • exit / close / for now / disconnect: Disconnect from the FTP server.
  • Command prefix exclamation point causes the command to be executed in local system, not in the remote.

And today we will look at working with FTP server ohm via command cmd line. As I said this method it is used quite rarely, at least by me, since it is easier to work through an FTP client. However, for writing automated scripts for exchanging data between your local machine and the FTP server, this knowledge will be very useful to you.

In this video, we will not write a script, we will do this in the next video, but simply consider the basic commands for working with the FTP server through the command line.

Basic commands for working with the FTP server through the CMD command line

And first we need to run the command line ( Start \ Applications \ Utilities \ Command Prompt) or win+r\cmd.

First of all, you need to switch to the FTP server mode, this is done through the command FTP

Enter USERNAME who has access to this server and PASSWORD

If we lead the team DIR then we will get a list of files and folders that are contained on the FTP server.

I will not talk about all the commands that are used to work with the FTP server through the command line, but only those that we need to write the script. These are mainly commands that allow us to copy files from the FTP server to our local computer and vice versa from the local computer to the FTP server.

You can always dial HELP and you will see a list of supported commands, to find out the purpose of the command, just enter HELP COMMAND_NAME.

But, here the description is very modest, so if you want to know more detailed description various teams, visit my site in a feature article. I'll post the link in the description of this video.

I do not have access to directly write files to the root of this FTP server, but I do have access to the htdocs folder, so let's first go to this folder. This is done by commands CD FOLDER_NAME.

We see that we have nothing in this folder. Let's try to copy here some file from our local computer. For this on local computer let's create a daddy, let's say I have it nod32_update since in one of the next videos, I will talk about how to write a script for automatic update antivirus NOD32. And let's create some Text Document admissible 123.txt.

To copy the 123.txt file to the FTP server, enter the command PUT c:\nod32_update\123.txt. The file is copied, we can check it with the command DIR. As you can see, the file was copied exactly to the folder into which we passed through the command CD.

If we need to copy all files from a specific folder on the local computer to a folder on the FTP server, then we need to specify the current local directory and the directory on the server.

We learned to specify the directory on the server, through the command CD

The local directory is specified through the command LCD, if we type it, we will see that the current directory is the folder with the current user's profile. And since I'm going to copy files from another folder, I enter the command LCD PATH_TO_DESIRED_FOLDER

What's the point, there's a team MPUT which copies files from local folder to the remote one and it does not ask which folders these are, so we need to provide this information.

In order to copy all files from a local folder to a remote folder, I specify the command MPUT *.*, where *.* means all files in the local folder.

Here we are asked a confirmation question for each file, we want to copy it or not, for this we live Enter. To prevent such confirmations from being displayed, there is a command PROMT disables interactive mode off.

Now let's try to do reverse action, namely copy all files from the remote folder on the FTP server to the local one. This is done through the command MGET*.*.

As you can see, everything is fine, all the necessary files have appeared in our folder and now we can finish working with the FTP server through the command QUIT.

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

Set up uploading files to FTP

Create 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- install binary way file transfers. In this case, the file is not recoded during transmission and is recorded unchanged. This is the most reliable way file transfer.
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 export.bat file
We write 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 run 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, then how to exclude the re-sending of files? The solution is banal:
Add the following 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.

Set up sending files to email

To send files to the mail, use the blat utility (http://www.blat.net/). Download and Unpack to any directory, in our case it is c:\%dir\blat/
Blat installation. We call the command line. On the command line, go to our folder:
cd c:\%dir%\blat/
We start the installation of blat (we register the blat profile in the registry, the data is written in the registry Windows systems to the thread: .
blat.exe -install smtp.yandex.ru [email protected] 3 25 profile [email protected] Your_Password
Where:
smtp.yandex.ru- name mail server, through which e-mails will be sent.
[email protected]- the name of the sender, which will be displayed in the field.
3 - the number of attempts to resend the message when the connection to the server fails.
25 - port number of the SMTP server.
profile- profile name.
[email protected]- username to 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 file export.bat after the line
C:\WINDOWS\system32\ftp.exe -s:c:\%dir%\ftpcomm.txt -i ftp.server.com
We prescribe:

where:
-p profile– profile name blat (Specified during installation)
-charset windows-1251(Choose Windows encoding)
-to [email protected] - address of the recipient of the letter
-subject "File export"- Letter subject
-body "Auto export"- The 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 write given 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 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 files to the FTP server, in the second we send files by e-mail, then we 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 Windows Scheduler. Where we indicate that we need to run export.bat once an hour.

Outcome

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

And I thought: does everyone know that regular ones are enough to work with FTP storages? Windows tools? In this article, I will describe three simple method transferring files via FTP as an example. The same tricks work in. All you need is , Explorer and the command line.

Method one: Internet Explorer 8

Any Internet user is familiar with the HTTP addresses for accessing websites that are entered in address bar browser - for example, . Addresses for accessing FTP servers are built according to the same principle: .

Type in the address bar the address of a public FTP storage, such as Microsoft (Figure A) and click . If the connection is successful, you will see the root directory. To access subdirectories and files, click on the main directory. To download a file, click on it right click mouse and select the "Save Target As" option.

As you can see, all database articles, fixes, updates, utilities, and Microsoft documentation are stored on the server, only they are presented in the form of a list with dates. Details about the FTP storage are contained in the "readme.txt" files.

Figure A. Microsoft FTP Server as viewed from Internet Explorer 8.

Please note that in order to successfully connect to the FTP server using IE, the options "Enable FTP folder view (outside of Internet Explorer)" and "Use passive FTP protocol ( Use Passive FTP (for firewall and DSL modem compatibility) in the Browsing section of the Advanced tab of the Internet Options dialog box. You can call it from the Tools menu.

To access private FTP servers, you must enter a username and password (Figure B). However, in IE 7 or IE 8, you can only view a list of files this way. To download, you will have to use the Explorer ( windows explorer). For details, see fig. C, D, E and F.


Figure B Access to private FTP servers requires a username and password.


Figure C Select the Page | Open FTP site in Windows Explorer” (Page | Open FTP Site in Windows Explorer) or “View | Open FTP Site in Windows Explorer” (View | Open FTP Site in Windows Explorer) to launch Explorer.



Figure D In the Internet Explorer Security Warning dialog box, click the Allow button.

It is often necessary to automate the process of downloading, uploading, and deleting files from an FTP server. For example, when working with distributed bases 1C data or to save backups. But not everyone knows that these procedures can be performed from the command line without resorting to an additional software, that is Windows tools. Below I will give 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, the ftp command is used, 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 the remote server.
-n Disable automatic login on initial connection.
-i Disable Interactive Requests When Passing Multiple
files.
-d Enable debug mode.
-g Disable globalization of filenames (see GLOB command).
-s:filename Specifies a text file containing FTP commands that
will be executed automatically when FTP is started.
-a Using the local interface to bind the connection.
-A Anonymous login to the service.
-x:send sockbuf Override standard SO_SNDBUF buffer size (8192).
-r:recv sockbuf Override standard buffer size SO_RCVBUF (8192).
-b:async count Overriding default async counter size (3)
-w:windowsize Overriding the default send buffer size (65535).
node Specifying the name or IP address of the remote host,
to which you want to connect.

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

!
Temporary transition to the shell.

append
Adding to a file.

ascii
Set the transfer mode for files in ascii format.

bell
extradition sound signal at the end of the command

binary
Sets the file transfer mode in binary format.

bye
End the ftp session and exit.

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

close
Terminating an ftp session.

debug
Switch debug mode.

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

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

disconnect
Terminating an ftp session.

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

glob
Switching the metacharacter extension of local file names.

hash
Toggle output "#" for each data block transmitted.

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

lcd [ <локальный_каталог> ]
Change 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>]
The output of the reduced contents of the directory of the remote computer, 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 be deleted.

mdir<remote_directory_1> … <remote_dir_n> <local_file>
Listing the contents of several directories on a remote computer, where:
<удаленный_каталог_n> — 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>
Getting multiple files where:
<удаленный_файл_n> — remote file to copy.

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

mls<remote_directory_1> ... <remote_dir_n> <local_file>
Outputting the abbreviated contents of several directories on the 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> is the name of the local files to be copied.

prompt
Switching interactive hint for composite commands.

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

pwd
Display the working directory of the remote computer.

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

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

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

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

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

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

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

status
Reflection current state ftp connections.

trace
Packet trace switch.

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

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

verbose
Switching the message display mode.


Let's consider a small example.

First, let's write a batch file that will be uploaded to the server site file file_data.dat from " C:\example» . The algorithm of 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.

V this example the name of the file and directories are hard-coded into the file. This is not always convenient. Let's modify the bat-file in such a way that it receives data for loading as parameters, and also we will take out all the changed values ​​in 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" by specifying the file name, local and remote directories as parameters.

Similarly, 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?

Liked the article? Share with friends!
Was this article helpful?
Yes
Not
Thanks for your feedback!
Something went wrong and your vote was not counted.
Thank you. Your message has been sent
Did you find an error in the text?
Select it, click Ctrl+Enter and we'll fix it!