Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

DOS batch combat


May 23, 2021 DOS Command learning manual



A file with a suffix of bat is a batch file, a text file. S imply enough, it automatically executes multiple commands continuously, and the contents of a batch file are one command at a time. S o what's the use of it?

For example, when you start wps software, you must do it every time


C:\>cd wps

C:\WPS>spdos

C:\WPS>py

C:\WPS>wbx

C:\WPS>wps


If you do this every time you use WPS, don't you find it a hassle?


If there's a way to write just one batch file, you'll automatically execute all the commands you just made, do you want to learn?


You'll be surprised when you finish this section and the first batch file you've written executes smoothly.


In addition, every time your computer starts up, you look for autoexec.bat a batch file that can perform commands that you have to perform every time you turn on, such as setting pathpaths, loading mouse-driven mouses, disk-accelerated smartdrv, and so on, to make your computer truly automated.



Echo, call, pause, rem are some of the most commonly used commands for batch files, and we'll start with them. E cho represents the character after this command is displayed

Echo off means that all running commands after this statement do not display the command line itself

It looks like echo off, but it is added to the front of the other command lines, indicating that the runtime does not display the command line itself.

Call calls another batch file (if you call another batch file directly, you will not be able to execute the current file successor command after executing that file)

Pause runs this sentence with a pause that shows Press any key to continue... Wait for the user to press any key before continuing.

Rem means that the character after this command is an interpreting line, not executed, just for your future use.


Example: edit the a.bat file with edit, enter the following contents after the deposit is c: s.bat, after executing the batch file can be implemented: write all the files in the root directory to a.txt, start UDOS, enter WPS and other functions.


The contents of the batch file are: The file represents:


Echo off does not display the command line


Dir c: ..txt . .txt


Call c: sucdos s ucdos.bat ucdos call ucdos


echo hello show "hello"


Pause pause and wait for the button to continue


Rem uses wps comments to use wps


cd ucdos enters the ucdos directory


wps uses wps


You can also use parameters in a batch file like the C language, which requires only one argument notation.


%represents a parameter, which refers to a string that is added after the file name when the batch file is run. V ariables can be represented from %0 to %9, %0 for the file name itself, and strings are represented in %1 to %9 order.


For example, the next batch of files in the C: Root is named f.bat, and the content is format %1


If you perform C:\gt;f a: you actually performformat a:


Also such as C: The next batch of processing files in the root is called .bat, and the content is type %1 type %2


Then running C:\t a.txt b.txt displays the contents of the a.txt and b-.txt files sequentially



If goto choice for is a more advanced command in a batch file, and if you're skilled with these, you're an expert at batch files.


If means that you will determine whether the specified conditions are met and that different commands will be executed. T here are three formats:

1, if "parameters" and "strings" of commands to be executed

If the argument is equal to the specified string, the condition is established and the command is run, otherwise the next sentence is run. (Note that there are two equal numbers)

e.g. if "%1"""a" format a:


2, if exist file name command to be executed

If there is a specified file, the condition is true, run the command, or run the next sentence. S uch as if exist config.sys edit config .sys


3, if errorlevel digital command to be executed

If the return code is equal to the specified number, the condition is true, and the command is run, otherwise the next sentence is run. For example, if errorlevel 2 goto x2 DOS program runtime will return a number to DOS, called error code errorlevel or return code


The goto batch file that runs here jumps to the label specified by goto and is typically used with if. Such as:


goto end


:end

echo this is the end


Choice uses this command to allow the user to enter a character to run different commands. W hen using should add /c: parameters, c: should be written after the prompt can be entered characters, there are no spaces between. It has a return code of 1234...


e.g. choice /c:dme defrag, mem, end

Will be displayed

defrag,mem,end[D,M,E]?


For example, the contents .bat test are as follows:

@echo off

choice /c:dme defrag,mem,end

if errorlevel 3 goto defrag should first judge the error code with the highest value

if errorlevel 2 goto mem

if errotlevel 1 goto end


:defrag

c:\dos\defrag

goto end


:mem

Mem

goto end


:end

echo good bye


After this file is run, defrag, mem, end, D, M, E? The user can select d m e, and then the if statement will make a judgment, d indicates the execution of the program segment labeled defrag, m indicates the execution of the program segment labeled mem, e indicates the execution label end of the program segment, each program segment ends with goto end to jump the program to the end mark, and then the program will show good bye, the end of the file.


The for loop command executes the same command multiple times as long as the conditions are met.


Format FOR(%%f) in (Collection) DO (Command)

As long as parameter f is in the specified collection, the condition is true and the command is executed


If there is one line in a batch file:

for %%c in (*.bat *.txt) do type %%c

The meaning is that if the file ends with a bat or txt, the contents of the file are displayed.



DOS automatically runs autoexec.bat this file at startup, where we typically load every required program, such as: path (set path), smartdrv (disk acceleration), mouse (mouse start), mscdex (optical drive connection), doskey (keyboard management), set (set environment variables), and so on.


If this file is not in the boot root, the computer will let the user enter the date and time.


For example, a typical autoexec .bat as follows:


@echo off does not display the command line


The $p a directory prompt before setting the prompt with $g


path c:\dos; c :\; c :\windows; c :\ucdos; c :\tools set the path


lh c: 'dos' doskey.com load keyboard management


lh c:'mouse mouse.com load mouse management


lh c:?dos\smartdrv.exe load disk acceleration management


lh c:?dos\mscdex /S/D:MSCD000 /M:12 /V load CD-ROM drive


set temp=c:?temp set up a temporary directory


The label is indicated by a string, and the line where the label is located is not executed