It is often necessary to automate the process of downloading, uploading, and deleting files from an FTP server. For example, when working with distributed 1C databases or to save backups. 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 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
Issuing a sound signal at the end of a 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
Toggle interactive tooltip for compound 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 a 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 of the current state of the ftp connection.
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 a remote computer where:
<Username>
— username to connect to the remote computer;
<password>
- password for the specified username, if not specified, but required to connect, the ftp command will request it from the user;
<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 display mode.
Let's consider a small example.
First, let's write a batch file that will be uploaded to the server website 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.
In this example, the file and directory names are hardcoded 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 the 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?
And today we will look at working with an FTP server through the cmd command line. As I said, this method 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 a more detailed description of the various commands, go to my website 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 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. To do this, create a folder on the local computer, let's say I have it nod32_update because in one of the following videos, I will talk about how to write a script to automatically update the NOD32 antivirus. And let's create some text document valid 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 a local folder to a 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 the opposite, namely copy all the 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.
FTP is an important TCP/IP utility for transferring files between systems. One of the main advantages of FTP is compatibility with many different remote host systems: files can be transferred between remote Windows 2000, Windows NT and UNIX systems, and even IBM servers such as AS/400. You can run the text-based FTP utility that comes with Windows 2000 and NT by typing ftp at the command prompt.
Here are 10 most useful FTP commands.
10. Help (or?). Many FTP commands can be entered in different ways. For example, Help and a question mark (?) perform the same function. Beginners should start with the Help command, after entering which the system provides the user with a complete list of FTP commands. To get a short description of a command, type? followed by the command name:
ftp>? open
9. Open. An FTP session usually begins with an Open command that establishes a connection to the specified FTP server (the remote host must be running an FTP service). Upon receiving the Open command, the system prompts for a user ID and password. Many FTP servers allow you to log in with an anonymous ID and a blank password. To establish communication with a computer named teca2, enter
ftp>open teca2
8. Quit (or Bye). The Quit command terminates any open session and exits the FTP shell:
ftp>quit
To close an open session without terminating FTP, use the Close command.
7.Pwd. The pwd command displays the directory on the remote machine that is currently connected to in an FTP session:
6.Ls. The Ls command lists the files and subdirectories located in the current directory on the remote computer. ls allows you to access remote files:
5. CD. If the desired file is not in the current directory of the remote system, you can use the Cd command to change to another directory. If a connection to a UNIX machine is established, then the remote directory is usually specified with a forward slash (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 a 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 transfer executable and .zip files, you need to change the transfer 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 a remote machine. To send a file named localfile.txt to the C:emp directory on the remote system, you need to 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) to the current directory of the local machine. To get a file named remotefile.txt, enter the command
Ftp>get remotefile.txt
Michael Auty is the Science Editor of the US-based Windows NT Magazine and President of TECA, a software development and consulting company. He can be contacted at:
FTP is an important TCP/IP utility for transferring files between systems. One of the main advantages of FTP is compatibility with many different remote host systems: files can be transferred between remote Windows 2000, Windows NT and UNIX systems, and even IBM servers such as AS/400. You can run the text-based FTP utility that comes with Windows 2000 and NT by typing ftp at the command prompt.
Here are 10 most useful FTP commands.
10. Help (or?). Many FTP commands can be entered in different ways. For example, Help and a question mark (?) perform the same function. Beginners should start with the Help command, after entering which the system provides the user with a complete list of FTP commands. To get a short description of a command, type? followed by the command name:
ftp>? open
9. Open. An FTP session usually begins with an Open command that establishes a connection to the specified FTP server (the remote host must be running an FTP service). Upon receiving the Open command, the system prompts for a user ID and password. Many FTP servers allow you to log in with an anonymous ID and a blank password. To establish communication with a computer named teca2, enter
ftp>open teca2
8. Quit (or Bye). The Quit command terminates any open session and exits the FTP shell:
ftp>quit
To close an open session without terminating FTP, use the Close command.
7.Pwd. The pwd command displays the directory on the remote machine that is currently connected to in an FTP session:
6.Ls. The Ls command lists the files and subdirectories located in the current directory on the remote computer. ls allows you to access remote files:
5. CD. If the desired file is not in the current directory of the remote system, you can use the Cd command to change to another directory. If a connection to a UNIX machine is established, then the remote directory is usually specified with a forward slash (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 a 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 transfer executable and .zip files, you need to change the transfer 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 a remote machine. To send a file named localfile.txt to the C:emp directory on the remote system, you need to 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) to the current directory of the local machine. To get a file named remotefile.txt, enter the command
Ftp>get remotefile.txt
Michael Auty is the Science Editor of the US-based Windows NT Magazine and President of TECA, a software development and consulting company. He can be contacted at:
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 all XML files hourly 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- set the binary method 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, 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 by 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 write the blat profile to the registry, the data is written to the registry of the Windows system in the branch: .
blat.exe -install smtp.yandex.ru [email protected] 3 25 profile [email protected] Your_Password
Where:
smtp.yandex.ru- name of the 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 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 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 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 for hourly sending files to an FTP server and email.