Application of Dos. bat batch file processing-Batch Compression

Source: Internet
Author: User

I often watch cartoons on my computer. After mangameeya is used, I usually compress the downloaded cartoon into a rarfile. There are several advantages: one is space saving, and the other is that in a computer, the processing speed of a file is faster than that of multiple files. Copying and pasting is an example. Many people should feel it, obviously, the size of the copied files is small, but the speed is higher than the capacity, and the speed is slower when the number is small. Also, it takes a long time for me to delete the previously left MATLAB after the reinstallation. I have dozens of Gbit/s of download folders, but they are deleted in two or three seconds.

Downloading a cartoon is generally a compressed file directly downloaded from a verycd, that is, the mangadowner, which downloads images. In the past, I downloaded a lot of cartoons, but it was not difficult to compress them. Some time ago, I put the cartoon I downloaded and the new cartoon I downloaded some time ago. I found that there were a lot of files not compressed. I usually use a folder to put a volume, and now there are hundreds of folders, I want to make a folder into a compressed file (if too many images are compressed together, mangameeya reading speed will also be affected ). If the mouse points are compressed one by one, the hand gets cramps. For example, I want to change the folder on the left to the rarfile on the right.

So I found the software for batch compression on the Internet, but I tried it and found that it was used to add a file to multiple compressed files at the same time, it is not a function I need. You can continue searching for such software but still cannot find it. I thought of using the doscommand line for batch compression.

I just feel like using the doscommand line, but I don't know how to do it. As a result, I searched for tutorials, but there were a lot of online courses, and I got started quickly. Now let's summarize the methods.

Because it will be used frequently in the future, create a BAT file. You can create a TXT file and change the extension to bat.

Right-click the BAT file and select edit. Enter the following text.

@ Echo off
For/d % C in (*) Do (
RAR a % C
If errorlevel 0 rmdir/S % C
)

Save the file and the folder to be compressed in the same directory and run it. (Note! The premise is to add the WinRAR installation folder to the environment variable. If no environment variable is added, change the RAR in this sentence to path/rar.exe, where path is the installation path of WinRAR, such as C: /program files/WinRAR/rar.exe a % C. Make sure this command exists. Otherwise, the folder will be deleted even if it is not compressed, I am too lazy to write a safer BAT file .....)

Explanation of the preceding commands:

The @ echo off in the first line does not play much role, but the following command is not displayed in the window during execution.

The for in the second line is the focus. As we all know, it is a loop like a C language. /D % C in (*) indicates traversing all folders (excluding subfolders ). If no/d exists, all files are traversed (excluding folders ). C In % C is a loop variable name. Like I, which is commonly used in C language, it can be any letter, but can only be one letter, and two % are indispensable. * In in (*) indicates all. It is a wildcard character. You can name the object here. For example, if you want to compress a folder starting with oolong, you can write it as in (Oolong *). The statement in the parentheses after do is the loop body.

Raris a command. You can find the rar.exe program in the winrarinstallation folder. If no environment variable is added, change RAR in this example to path/rar.exe, where path is the installation path of WinRAR. Rarcommand (rar.exe, also known as the program line) after a is a parameter, indicates the creation of the rarfile. Followed by two % C, the first is the name of the created rarfile, and the second is the name of the compressed file. This line is used to compress a folder into a rarfile with the same name as it.

In fact, at this time I have implemented the functions, but after compression, there is another problem, that is, the original image still needs to be deleted, otherwise it will take up a lot of places. Although it is convenient to delete Multiple folders in Windows, it can save trouble. So there is the next sentence. If errorlevel 0 indicates whether the execution result of the command (rar.exe) on the slave is 0. I checked it from the help of RAR. If the execution result returns 0, the compression is successful. Rmdir is the command for deleting folders./s indicates that all subdirectories and files under the specified directory will be deleted in addition to the directory. Used to delete a directory tree. If/Q is added, no prompt is displayed. If/Q is not displayed, you will be asked if you are sure to delete it each time you delete it. This line means that if the compression is successful, the compressed folder will be deleted.

The following figure shows the running time. After the window is run, it is closed.

Through this batch processing command, I deeply felt its convenience. The disadvantage of Windows is that you cannot customize commands or functions at will. Batch Processing solves this problem to some extent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.