Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Bat scripts to handle ftp

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use Bat scripts to deal with ftp. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. FTP command description

1. In the windows system, because FTP is a program, if the FTP command is called directly in the bat script, the command line window always pops up during execution, looping in the > FTP cursor, so in another way, use-S:filename, enter-h for more commands to view.

2, FTP login is successful, you need to download a single remote file, use the get command, if you download multiple files, use mget, it supports wildcards, in the use of mget, you need to select Y prompt N for each file, if you want to download all files without interaction, you can enter the prompt command to turn off the interaction mode.

The Mput and mget commands should pay attention to two issues:

Ⅰ, cannot specify a life word to the target file, and all files on the command line are treated as source files

Ⅱ and mput,mget commands cannot upload or download directories, but only files

Ⅲ, Prompt: each time you execute this command, it loops between on and off. At the beginning of the ftp script, prompt defaults to on, so generally, when uploading or downloading files, execute prompt once to turn off the interactive on and change it to off.

3. The transfer mode of the file:

Binary, binary transmission

Ascii, ascII transmission

In the FTP file transfer process, ASCII transfers HTML and text written files, while binary code transfer can transfer text and non-text (execution files, compressed files, pictures, etc.), with versatility, binary code transfer speed is faster than ASCII transfer, so in the establishment of bat scripts, generally enter the bin command to enable binary transfer. If you transfer a non-text file in ASCII mode, you may show a lot of garbled code, and if you upload some CGI scripts, you may not be able to run such scripts, and you will see an error in server 500error on the browser.

The difference between Ascii and binary mode is the processing of carriage return line feeds. Binary mode does not do any processing on the data. Asci mode converts carriage return line feeds into native carriage return characters, such as\ r in Unix,\ r in Windows,\ r in Mac. The downlink Terminator of the unix system is one byte, the hexadecimal 0A, while the ms system is two bytes, the hexadecimal 0D0A.

So when you use ascii to download files from unix's ftp server (whether binary or text files, redhat's vsftp is turned off by default binary,ascii by default), every byte detected is 0A, it will automatically insert a 0D, so if your file is a binary file such as executable files, compressed packages and so on, it will definitely not be used. If your file is a text file under unix, you are correct to use ascii mode. If you misuse binary mode, you will not wrap the file on windows. There are black squares in it.

When using the FTP client to transfer files, because these software are relatively intelligent, it can automatically enable the transfer mode according to the suffix, so you don't have to worry about problems.

2. Verification of examples:

Environment description: there is a FTP server (windows version) on 192.168.133.34. The FTP user name is lgh and the password is www.yisu.com. The user's root directory is D:\ lgh. The document inside is shown in the picture below. Sql.rar is a compressed file of sql.bat. Pay attention to the picture. I will refer to this file in a later example to interpret the mget,get,prompt,bin,ascii and other ftp commands completely.

Example 1: directly BAT calls the FTP command

Ftpbat.bat content

(* * do not name the bat file ftp.bat, there will be problems in execution *)

The code is as follows:

Ftp

Open 192.168.133.34

Lgh

Www.yisu.com

Get sql.rar

Bye

Judging from the results on the figure, it is not possible for the bat script to directly call the FTP command of xp. The H:\ > ftp will always loop when it is executed. My XP system here is SP3, other versions, I did not test. If there is any difference, you are welcome to leave a message.

Example 2: upload and download a single file with put,get

Upload the file flower.zip from the H:\ js directory on the XP machine to the FTP server

Download the sql.rar file from the FTP server to the H:\ JS directory on the XP machine

The ftp-get-put.bat content is as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo get sql.rar > > "ftpfilename%"

Echo put flower.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

Let's take a look at the results, see the following two pictures. From the point of view of the XP system, the JS directory of the H disk has an extra flower.zip in the root directory of the sql.rar,FTP server, which shows that the script just completed the normal execution and achieved the desired goal.

Example 3: upload and download multiple files with put,get

Copy flower.zip once on the XP machine and rename it flower2.zip

Delete the sql.rar file downloaded by the second instance on the XP machine so as not to affect this operation

Delete the flower.zip file uploaded by the second instance on the FTP server so as not to affect this operation

Upload the files flower.zip and flower2.zip from the H:\ js directory on the XP machine to the FTP server

The ftp-get-put-many.bat content is as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo put flower.zip flower2.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

Check the root directory on the FTP server to see if flower.zip and flower2.zip have been uploaded successfully. From the picture, only the flower2.zip file was successfully uploaded.

In that ftp-get-put-many.bat script,

Echo put flower.zip flower2.zip > > "ftpfilename%"

Flower2.zip is the last file to be transferred. When using put, it means that bat only recognizes the last file when processing uploads. Let's add one more file, flower3.zip, to verify.

Copy flower.zip once on the XP machine and rename it flower3.zip

Delete the flower2.zip file uploaded by the second instance on the FTP server so as not to affect this operation

The content of ftp-get-put-many.bat is modified as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo put flower.zip flower2.zip flower3.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

As a result, only the flower2.zip was uploaded successfully, because when bat double-clicked, the execution process flashed by, and we could not see the execution process. We went to the command line to execute it to see what went wrong.

Command line, execute the ftp-get-put-many.bat script, from the execution point of view, flower.zip three files are put, but in the transfer, but only passed flower2.zip, really do not understand, do you have friends who know? Give me some direction.

In another way, use the wildcard * and try it.

The content of ftp-get-put-many.bat is modified as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo put flower*.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

Delete all uploaded flower files on the FTP server so as not to affect this operation

Execute ftp-get-put-many.bat, and only the flower.zip file is uploaded successfully. Oh, flower3.zip file, why can't upload.?

In fact, using put to transfer multiple files can be replaced by repetition. For example, the content of ftp-get-put-many.bat is modified as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo put flower.zip > > "ftpfilename%"

Echo put flower2.zip > > "ftpfilename%"

Echo put flower3.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

Example 4: upload and download multiple files with mput,mget

Delete files about flower on the FTP server

The ftp-mget-mput-many.bat content is as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo mput flower.zip flower2.zip flower3.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

After double-clicking ftp-mget-mput-many.bat, I found a pop-up window of the system, and it got stuck and stopped.

The reason is that the mput command is used, but the interaction is not turned off with Prompt

The content of ftp-mget-mput-many.bat is modified as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo Prompt > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo mput flower.zip flower2.zip flower3.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

Execute ftp-mget-mput-many.bat again, and the result is shown below:

Try using the wildcard character *

Delete files about flower on the FTP server

The content of ftp-mget-mput-many.bat is modified as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo Prompt > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo mput flower*.zip > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

Still successfully upload the relevant flower*.zip file. Because the result is the same as the previous step, we will not grasp the picture.

Example 5: using ascii to transfer non-text files between windows systems

Ascii the rmb.jpg on the XP computer to the root directory of the FTP server.

The content of ftp-get-ascii.bat is modified as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.133.34 > "ftpfilename%"

Echo lgh > > "ftpfilename%"

Echo www.liuguohua.com > > "ftpfilename%"

Echo ascii > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo put rmb.jpg > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

After performing ftp-get-ascii.bat, it is found that rmb.jpg can be uploaded and opened normally, so this phenomenon is different from the previous ascii that cannot transmit non-text things.

This is because the FTP server is a WINDOWS system, and the bat script on the XP computer is also run on the windows system, so it is possible for bat to transfer files using FTP in the windows system, whether using ascii or binary. Next, let's do another experiment. The FTP server selects centos, and then tests the problem of ascii transferring non-text files.

Example 6: using ascii to transfer non-text files between windows system and linux system

Set up a vsftpd server on RHEL 5.464-bit system

As mentioned above, redhat enables binary transfer by default and turns off ascii code transfer, so we also need to modify the configuration file of vsftpd, modify the / etc/vsftpd/vsftpd.conf file, allow the client to transfer ascii, and restart vsftpd.

Put rmb.jpg on the FTP server, and notice that there are only ftp-getascii.bat files in the H:\ js directory.

The contents of the ftp-getascii.bat file are modified as follows:

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.145.226 > "ftpfilename%"

Echo ftp1 > > "ftpfilename%"

Echo redhat > > "ftpfilename%"

Echo asc > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo get rmb.jpg > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

Execute ftp-getascii.bat, look at the results, the rmb.jpg file has been downloaded to the XP computer, double-click this file, found that the image garbled, the file was destroyed, are caused by ascii code transmission.

Switch to bin transmission and test it.

Delete the rmb.jpg file on the XP computer, and then double-click the ftp-getascii.bat file to download a rmb.jpg file from LINUX's FTP server.

Modify ftp-getascii.bat to change ascii to binary transmission

The code is as follows:

@ echo off

Set ftpfilename=autoftp.cfg

Echo open 192.168.145.226 > "ftpfilename%"

Echo ftp1 > > "ftpfilename%"

Echo redhat > > "ftpfilename%"

Echo bin > > "ftpfilename%"

Echo lcd h:\ js > > "% ftpfilename%"

Echo get rmb.jpg > > "ftpfilename%"

Echo bye > > "ftpfilename%"

Ftp-s: "% ftpfilename%"

Del "ftpfilename%"

As a result, the file is downloaded normally and the image is displayed normally.

Extended reading:

The internal commands used by ftp are as follows (square brackets indicate optional):

1! [cmd [ARGs]]: execute interactive shell,exit in the local machine to return to the ftp environment, such as:! ls*.zip.

2. $macro-ame [args]: executes the macro definition macro-name.

3.account [password]: provides the supplementary password required to access system resources after a successful login to the remote system.

4.append local-file [remote-file]: appends local files to the remote system host, or uses the local file name if the remote system file name is not specified.

5.ascii: use ascii type transport.

6.bell: the computer rings once after each command is executed.

7.bin: use binary file transfer.

8.bye: exits the ftp session process.

9.case: when using mget, change the uppercase to lowercase letters in the remote host file name.

10.cd remote-dir: enter the remote host directory.

11.cdup: enter the parent directory of the remote host directory.

12.chmod mode file-name: set the access mode of the remote host file file-name to mode, such as chmod 777 a.out.

13.close: interrupts the ftp session with the remote server (corresponding to open).

14.cr: when transferring files in asscii mode, enter newline is converted to return line.

15.delete remote-file: delete the remote host file.

16.debug [debug-value]: sets the debug mode to display every command sent to the remote host, such as deb up 3. Setting it to 0 means canceling debug.

17.dir [remote-dir] [local-file]: displays the remote host directory and stores the results in the local file local-file.

18.disconnection: same as close.

19.form format: sets the file transfer mode to format and defaults to file mode.

20.get remote-file [local-file]: transfer the file remote-file of the remote host to the local-file of the local hard disk.

21.glob: sets the filename extension for mdelete,mget,mput. By default, the filename is not extended, same as the-g argument on the command line.

22.hash: displays a hash symbol (#) for every 1024 bytes transferred.

23.help [cmd]: displays help information for the ftp internal command cmd, such as help get.

24.idle [seconds]: sets the sleep timer of the remote server to [seconds] seconds.

25.image: sets the binary transfer mode (same as binary).

26.lcd [dir]: change the local working directory to dir.

27.ls [remote-dir] [local-file]: displays the remote directory remote-dir and saves it to the local file local-file.

28.macdef macro-name: define a macro that ends when a blank line under macdef is encountered.

29.mdelete [remote-file]: deletes the remote host file.

30.mdir remote-files local-file: similar to dir, but can specify multiple remote files, such as: mdir * .o.*.zipoutfile.

31.mget remote-files: transfer multiple remote files.

32.mkdir dir-name: create a directory on the remote host.

33.mls remote-file local-file: same as nlist, but multiple file names can be specified.

34.mode [modename]: sets the file transfer mode to modename and defaults to stream mode.

35.modtime file-name: displays the last modification time of the remote host file.

36.mput local-file: transfer multiple files to a remote host.

37.newer file-name: if the modification time of the file-name on the remote machine is closer than that of the file with the same name on the local hard disk, retransmit the file.

38.nlist [remote-dir] [local-file]: displays the file list of the remote host directory and saves it to the local-file of the local hard disk.

39.nmap [inpattern outpattern]: set the file name mapping mechanism, so that when a file is transferred, some characters in the file are converted to each other, such as: nmap $1.account2.account3 [$1jijin2]. [$2memename 3], then when transferring the file a1.a2.a3, the filename becomes a1jina2. This command is especially suitable for remote hosts that are non-UNIX

The condition of the machine.

40. N transfer [inchars [outchars]]: set the translation mechanism of file name characters, such as ntrans1R, then the file name LLL will become RRR.

41.open host [port]: a specified ftp server connection is established, and the connection port can be specified.

42.passive: enter the passive transmission mode.

43.prompt: sets interactive prompts for multiple file transfers.

44.proxy ftp-cmd: in a secondary control connection, execute a ftp command that allows two ftp servers to be connected to transfer files between the two servers. The first ftp command must be open to first establish a connection between the two servers.

45.put local-file [remote-file]: transfers the local file local-file to a remote host.

46.pwd: displays the current working directory of the remote host.

47.quit: with bye, exit the ftp session.

48.quote arg1,arg2...: sends the parameters verbatim to the remote ftp server, such as quote syst.

49.recv remote-file [local-file]: same as get.

50.reget remote-file [local-file]: similar to get, but if local-file exists, the transmission is resumed from the last transmission interruption.

51.rhelp [cmd-name]: request help from a remote host.

52.rstatus [file-name]: if no file name is specified, the status of the remote host is displayed, otherwise the file status is displayed.

53.rename [from] [to]: change the file name of the remote host.

54.reset: clears the answer queue.

55.restart marker: restart get or put from the specified flag marker, such as restart 130.

56.rmdir dir-name: delete the remote host directory.

57.runique: set the unique storage of the file name. If the file exists, add the suffix.. 1, 2, etc. after the original file.

58.send local-file [remote-file]: same as put.

59.sendport: sets the use of the PORT command.

60.site arg1,arg2...: sends parameters verbatim to the remote ftp host as SITE commands.

61.size file-name: displays the file size of the remote host, such as site idle 7200.

62.status: displays the current ftp status.

63.struct [struct-name]: set the file transfer structure to struct-name, using the stream structure by default.

64.sunique: sets the remote host file name store to unique (corresponding to runique).

65.system: displays the operating system type of the remote host.

66.tenex: set the file transfer type to the desired type of the TENEX machine.

67.tick: sets the byte counter for transfer.

68.trace: set up package tracking.

69.type [type-name]: sets the file transfer type to type-name, defaults to ascii, such as type binary, and sets the binary transfer mode.

70.umask [newmask]: set the default umask of the remote server to newmask, such as umask 3.

71.user user-name [password] [account]: indicate your identity to the remote host. When you need a password, you must enter a password, such as user anonymous my@email.

72.verbose: with the-v parameter of the command line, that is, setting the detailed reporting mode, all responses from the ftp server will be displayed to the user. The default is on.

73. [cmd]: same as help.

This is the end of this article on "how to use Bat scripts to deal with ftp". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report