In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Batch batch script". In daily operation, I believe many people have doubts about how to use Batch batch script. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use Batch batch script". Next, please follow the editor to study!
1. Preface
When it comes to automation solutions, I believe most people will think of using the Python language, just need to write Python scripts according to the functional scenario. On the contrary, the Batch batch on the PC side seems to be about to be forgotten, and many people scoff at it, thinking that such an old scripting language seems useless and that Python can take its place.
Compared with Python scripts, Batch batch scripts are more concise, convenient and efficient in some scenarios, namely, write-as-you-go, do not need to rely on the Python environment, and can completely get rid of tedious steps such as packaging.
two。 Batch processing
The basic new batch script is simple, just create a new text file, then change the encoding to: ANSI encoding, then write the script logic code, and finally save the file to end with .bat.
Batch common commands include: echo,: / rem, title/color, cd/md/dir, rd/del/copy, pause, goto, for, if, set, start and so on.
Where:
@ echo off indicates that the echo is turned off at the beginning of this line, and the batch command and the execution result are not displayed. It is usually placed on the first line of the batch file.
Echo log parameter: used to output logs in the console, preferring to understand script execution logic
:: / rem comment content: in-comment command
Title/color: set the form title and background color
Cd: switching directories
Md: create a directory
Dir: displays the contents of a folder
Rd: delete a directory
Del delete mode file: delete the file. By configuring the delete mode, you can delete any file, including hidden, read-only, system files
Copy: copying files
Pause: pause command, usually placed on the last line of the batch file
Goto: jump command, which is generally used with ": task name" to perform a circular task. For an example, see Section 3.
For: loop command, similar to for syntax in Python
If: judge command
Set: set a variable
Start: commands that invoke external programs
3. Practical scene
Let's talk about some practical scenarios for batch processing:
1. Classify the files under the folder or desktop, and then put them into different folders for easy induction and management.
@ echo off for% I in (*) do (md% ~ xi move *% ~ xi% ~ xi) pause
You read it correctly, only need 4 lines of code, for is used to traverse the current folder, the results of traversal use do to execute the following command%% ~ xi is the extension to intercept%% I, use the md command to create a new folder move is used to move the source files to a new folder.
2. Delete all build folders under the current directory (including subdirectories)
After compiling with Android Studio, if the project has multiple Module, there may be multiple build folders, which can be deleted with one click using the batch script below.
@ echo off:: open to the current directory cd / d "% ~ dp0" echo to delete:: delete for / r / D% I in (* build*) do rd / s / Q "% I" echo deletion completed pause
The script is simple, just open the project root directory, use the "for + do" loop to traverse, and delete the files according to the matching rules.
Where:% ~ dp0: batch files current directory / s: delete files from all subdirectories.
/ Q: specifies that the delete operation is performed in "quiet mode". Deletion does not require confirmation, and the batch script code is more concise than the Python script.
3. Execute Python script scheduling tasks
For example, when I finish writing a Python crawler, I want to execute it every 5 minutes. Here you can use the goto command.
The @ echo off title loop runs the Python code:: every 5 minutes, once in s set INTERVAL=300::, prints out the execution time, echo starts execution -% time% python C:/test.py:: uses timeout for countdown timeout% INTERVAL%:: create a new task: Task echo starts execution -% time% python C:/test.py timeout% INTERVAL%:: use the goto command Start jumping to the above task and start executing goto Task
4. Git submission code
Normally use the git command line to submit the code (without using IDE), you need to use git add., git commit-m submit log, git pull, git push four commands.
To use a batch script, just double-click and enter the submission log.
The specific code is as follows:
@ echo off title submit code echo submit code, simplify the operation:: status git status:: set: wait for input, assign value to variable msg set / p commit_msg= code submit comment: submit code 4 commands git add. Git commit-m% commit_msg% git pull git push echo submitted successfully pause
5. Clear the junk files of the system
Specify the delete mode and the path to be deleted, and call the del command to delete it.
@ echo off:: configure title Alic Feng batTool for Clean color 03 mode con cols=42 lines=20 echo executes cleaning,Please waiting... :: the program deletes the system useless files and starts del / f / s / Q% systemdrive%\ *. Tmp 1 > nul 2 > nul del / f / s / Q% systemdrive%\ *. _ mp 1 > nul 2 > nul del / f / s / Q% systemdrive%\ *. Log 1 > nul 2 > nul del / f / s / Q% systemdrive%\. Gid 1 > nul 2 > nul del / f / s / Q% systemdrive%\ *. Chk 1 > nul 2 > nul del / f / s / Q% systemdrive%\ *. Old 1 > nul 2 > nul del / f / s / Q% systemdrive%\ recycled\ *. * 1 > nul 2 > nul del / f / s / Q% windir%\ *. Bak 1 > nul 2 > nul del / f / s / Q% windir%\ prefetch\ *. * 1 > nul 2 > nul del / f / s / Q% windir%\ temp\. * 1 > nul 2 > nul del / f / Q% userprofile%\ cookies. * 1 > nul 2 > nul del / f / Q% userprofile%\ recent\. *. * 1 > nul 2 > nul del / f / s / Q "% userprofile%\ Local Settings\. Temporary Internet Files\ *. "1 > nul 2 > nul del / f / s / Q"% userprofile%\ Local Settings\ Temp\ *. * "1 > nul 2 > nul del / f / s / Q"% userprofile%\ recent\ *. * "1 > nul 2 > nul:: delete system junk files end echo cleanup system garbage completed! Echo. & pause at this point, the study on "how to use Batch batch scripts" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.