![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-03 14:11 |
48,038 topics / 350,123 posts / today 0 new / 48,251 members |
| DOS学习入门 & 精彩文章 (教学室) » Notes from intermittent learning |
| Printable Version 6,116 / 26 |
| Floor1 tempuser | Posted 2008-03-11 16:42 |
| 高级用户 Posts 261 Credits 547 | |
|
2007-12-03
Why Learn DOS When There's Windows? Because DOS is more efficient in some aspects; learning Linux, it's more convenient to use DOS for switches/routers; for remote control. I. Introduction and Overview 1. Files and Directories File name: file base name + suffix File base name: 1 - 8 characters, must exist for the file name; suffix: 0 - 3 characters, optional. Directory: Called "folder" in Windows. When displayed with DIR, it has the [dir] mark, indicating they are directories. 2. .exe: Executable file .com: Command file .bat: Batch file They are all called "executable files". The priority of the .bat suffix file is higher than those with .exe and .com suffixes. 3. Internal and External DOS Commands Internal commands are directly loaded into memory by command.com when the system starts and can be used directly. In other words, internal commands exist in the command.com file. External commands are independent files existing on the disk and can be loaded into memory for execution when needed. 4. . and .. and \ . : Current directory .. : The upper-level directory of the current directory, also called "parent directory" \ : Root directory. 5. Dir Command When the current directory has . and .., when using the dir command to display the current directory information, they are considered as a file, just with a size of zero. 6. DOS Boot Sequence Boot -> IO.SYS -> MSDOS.SYS -> CONFIG.SYS -> COMMAND.COM -> Autoexec.bat Among them, IO.SYS, MSDOS.SYS, and COMMAND.COM are essential for the startup of the DOS system. io.sys: Input/output processing program Msdos.sys: File processing program Command.com: Command processing program New Explanation: For DOS, when the hard disk boots, the INT19 interrupt first reads the "hard disk MBR". After reading, it is loaded into memory at 0000:007c, and then INT19 also jumps to 0000:007c in memory. At this time, the control right is still INT19. Then, a "program" in the MBR will look for the "bootable partition". After finding it, the bootable partition is loaded into 0000:007c. Then the MBR program will jump to the address 0000:007c, and the system starts to boot. The main boot sector is: cylinder 0, head 0, sector 1 Then comes the FAT (File Allocation Table) Supplementary (2007-12-10) . DOS: disk operating system, single-user single-task OS. Cold boot: When starting the computer, turn on the monitor power -> host power, and the shutdown is in the reverse order. Warm boot: When the system is started, press the CTRL+ALT+DEL key combination to start the system again. Difference between the two: Whether to skip some hardware detection processes such as memory detection, and other running processes are the same. . Common Reserved Names in DOS CON: Keyboard/display NUL: Empty device or virtual device AUX/COM1: First serial communication interface COM2\COM3\COM4: 2nd, 3rd, 4th serial ports LPT1\PRN: First parallel interface . A batch file can be understood as "a DOS command". . Why Not Name a Batch File as an "Internal Command or External Command" of DOS? Answer: If it has the same name as an internal command, then the internal command has a higher priority than the batch, and the batch file cannot be executed; If it has the same name as an external command, it depends on "whether the external command or the batch file is found first". The one found first is executed first. Therefore, it is recommended to avoid naming batches the same as internal and external commands of DOS. . How to Create a Batch File in DOS? a. copy con the batch file to be created b. edit the batch file to be created . Also can use Pause the information to be displayed, similar to echo the information to be displayed . How to Make a "Batch File" Beep? (Relying on the built-in speaker of the computer) In DOS, use edit beep.bat, then enter the editing page, first enter Pause, then enter a space, then hold down the CTRL key, press the "P" key, release the CTRL key, then hold down the CTRL key, press "G", and a "beep control character" can be generated. It can also be done by holding down the CTRL key and pressing "P" and "G" keys successively. In fact, echo can also achieve the same effect, but it is troublesome to input the control character in the text. It is a good way to edit the batch file in DOS, input the control character and save it, then return to Windows to edit. II. Introduction to DOS Commands 1. Dir Displays the current directory information but does not include system and hidden files. dir /p: Display information in pages (p is the abbreviation of page, that is, display screen by page) dir/w: Display information in a wide list, without file size and creation date dir/d: Similar to dir/w display mode dir/b: Only display file names, directory names are also displayed in the form of file names, and the [ ] mark of directory names is not displayed (a concise display) dir /l: Display files and directories in lowercase letters dir /a: Display files with specified attributes, for example: dir /a:r displays all files with read-only attributes in the current directory Example: dir /a:d means only display directory information Example: To find files of a specified date, dir|find "2007-12-04", pay attention to the writing format of the date to match the local date display Example: To find files in multi-level directories, use the parameter /s, such as dir temp /s. If there is a temp directory under the current directory and there is a temp directory under temp, the execution result is to list all files in the last-level temp directory REM dir/? to see help, find that the : after a is optional, but if : is not used, the message "The volume of the current drive has no label, and the file is not found" will be displayed. Why does it work without :? I made a mistake myself! Dir /a:-r: Display all files with non-read-only attributes in the current directory - : Represents the "prefix of no". REM Dir /o: Display information in a classified way, such as file size, alphabetical order, etc. For example: dir /o:s means display information according to file size; dir /o:n displays information according to alphabetical order Dir /n: Display in "long file list mode, file name on the rightmost side" Dir /c: Similar to dir/n Dir/x: Display files not in the 8dot3 naming rule Example: dir >test.txt means import the result of the dir command into a text file 2. Cd Change directory Note: If the following command is used on the C drive c:\>cd e:\test The result is still at c:\>, but it is transferred to the D drive, and the current directory of the D drive is d:\>test>. I encountered the above problem when using at to regularly delete files in a certain directory. Example: @echo off c: rem At that time, I forgot to go to c:, and as a result, many files on the disk where the batch file was located were deleted, including the batch file itself rem Making the at task unable to execute cd "C:\Documents and Settings\All Users\Application Data\Symantec\Symantec AntiVirus Corporate Edition\7.5\Logs" echo y | del *.* Cd .. means go back to the upper-level directory Cd\ means go back to the root directory Cd /d parameter /d means change both the current directory and the current drive 3. Copy The function is to copy and move, but cannot copy subdirectories in the directory Example: Merge two text files 1.txt and 2.txt into 3.txt Copy /a 1.txt+2.txt 3.txt REM Merge these two files according to ASCII code Copy /b 1.txt+2.txt 3.txt REM Merge these two files according to binary code Parameters /a and /b cannot be used together. Among them, /a can only merge plain text files, and /b can merge in both text and binary ways Example: Encrypt a file copy /b test.jpg+1.txt 2.jpg REM test.jpg and 1.txt become a file 2.jpg, so the text information is encrypted. To view this information, you can use the ultraedit software to view REM Remember, the document to be encrypted must be placed on the right side of the + sign 4. Xcopy Can copy non-empty subdirectories in the directory to other places, but need to use the parameter /s /e must be used with parameter /s, indicating that not only subdirectories can be copied, but also empty directories can be copied /v Verify after copying 5. Del Delete Del *.* is a very dangerous command Example: del /f Force delete read-only files Del/a Delete files with specified attributes 6. ren Rename 7. type Can only display the content of one file at a time 8. Format and unformat The available space of the formatted disk is equal to the total space of the disk, indicating that the disk performance is good Format /u means unconditional formatting, at this time unformat is invalid Example: Echo y|formant a: Unformat drive letter means restore the formatted disk unformat /l means list the recoverable file list 9. Md and Rd Create a new directory Example: Create multiple directories at the same time md c:\1 d:\2 e:\3 is equivalent to md c:\1;d:\2 Delete the directory. The directory to be deleted cannot have subdirectories and files. If you want to do this, you can use Rd /s Example: Create an encrypted directory and how to view and delete it md test..\ This creates an "encrypted" folder, which cannot be opened by double-clicking It can be viewed through "Run" in the Start menu or the address bar in Explorer by entering its "complete path" Delete it through rd /s /q Thought: If the "encrypted" folder established is combined with the method of hiding attributes including registry hiding, a relatively private space can be established. Approximate location of the registry: MACHINE\SOFTWARE\WINDOWNS\CURRENTVERSION\EXPLORER\ACVANCED\FOLEDR\SHWOALL\CHECKEDVALUE's value 10. Deltree Delete the directory tree, and it deletes non-empty directories without hesitation Deltree *.* is a very dangerous command 11. Mem Memory 12. Chkdsk Checkdisk 13. Sys C:\>sys a: is equivalent to format a:/s, both are to make a bootable disk If you copy a DOS bootable disk, the two files io.sys and msdos.sys may not be copied to the frontmost track of the disk 14. Cls Clear screen 15. More Display in pages 16. Move Example: @echo off Cd c:\test Move c:*.* a: REM Note that there is no "\" between the 3rd line "c: and *.*", which means to copy all files in the current directory, that is, the test directory, to the A drive. 17. Help As its name implies 18. Attrib Attribute Example: Set all files and subdirectories (including the directory itself) in the c:\test directory to "read-only" attribute Attrib +r c:\test /s /d /s: Indicates to operate on subdirectories /d: Indicates to operate on directories, which can be simply understood as directory Example: Set all files and subdirectories (excluding the directory itself) in the c:\test directory to "read-only" attribute Attrib +r c:\test\*.* /s /d 19. Time and date Date /t: No need to input a new time Echo+|date|find "current date" Echo+|time|find "current time" Example: A batch file can be written to record the time usage of the machine echo+|date/t>test.txt echo+|time/t>>test.txt 20. Lable 21. Defrag Defragment the disk 22. Doskey Establish and call DOS macro commands Example: doskey d=deltree, then executing d is equivalent to executing the deltree command Example: If a lot of commands are input in CMD and you need to use these commands again without repeating input, you can use doskey. The specific operation (in CMD): doskey /h (h is history) is to view the commands that have been used in CMD before, and they can be called again through the F9 key to call their cache table, or the up and down arrows or PaUp and PaDn keys can be used to operate. If you want to clear them, you can use doskey /reinstall. 23. Fdisk Fdisk /mbr: Re-establish the DOS main boot record 24. Memmaker Memory optimization command 25. Emm386 Extended memory management 26. Lh or loadhigh Load the program into the upper memory 27. Msd System detection Cannot be used directly 28. Undelete Before using this command, you need to use the lock command to remove the restriction that the hard disk directory area cannot be modified Undelete /all: Restore all del files without asking Undelete /list: List the recoverable file list 29. Path Example: path c:\test If a command is executed in the current directory and it does not exist, it will continue to look for the command in the path-set path, and execute it if it exists 30. Prompt Set the prompt, which is the common c:\> we see. c: indicates that the current drive letter is C drive, \ indicates the root directory, and > separates the prompt from the command to be input. It can be changed according to needs 31. Backup and restore Backup and restore Restort /s: Restore subdirectories to the specified directory 32. Edit Can only edit text files. If the edited file exists, it will be edited. If it does not exist, it will be temporarily created and enter the editing state. If saved, the file will be established on the disk 33. Set Set variables. Enter the set command directly in CMD, and most system variables will be listed Example: set path=c:\test is equivalent to path c:\test Example: Manually input variable value set /p a= Please input your choice REM In the pure DOS environment, the /p parameter does not exist, and this statement is naturally not good! REM You can define a variable similar to a system variable (a variable that still exists after restarting the machine) through the setx command. However, this command does not exist in the XP system, but exists in 2003. Format: setx variable assignment Set /a var= REM Arithmetic operation on variables Example: set /a var+=1 Set %path:str1=str2% REM Replace str1 with str2 Example: @echo off set var= zgk.com Echo Display the original value of var echo %var% pause set a=%var: =% rem The space in the original variable is replaced echo Display the value of the replaced var echo %a% set b=%var:.=% echo %b% rem The dot "." in the original variable is replaced pause set %path:~10,5% REM Intercept the specified number of columns of the variable Example: @echo off Set var=zgk.com Echo %var% Set b=%var:~2,3% rem Intercept the first 2 columns of the specified variable var, that is, start reading 3 characters from the 3rd column and assign them to the new variable b, rem then b=k.o Echo %b% Pause Set %path:~-10% Example: @echo off set var=zgk.com Echo %var% Set b=%var:~-3% rem Take 3 columns from the back of the specified variable var and assign them to the new variable b, then b=com Echo %b% Set c=%var:~3% rem Intercept the first 3 columns of the specified variable var from the beginning, that is, assign all variables after the 4th column of var to c, rem then c=.com Echo %c% pause Set %path:~0,-2% Example: @echo off set var=zgk.com Echo %var% Set b=%var:~-3% rem Take 3 columns from the back of the specified variable var and assign them to the new variable b, then b=com Echo %b% Set c=%var:~0,-2% rem Remove 2 columns from the var from the back, then assign var to c, then c=zgk.c Echo %c% pause 34. Smartdrv Set disk cache 35. Append Set non-executable file path Example: There is no test.txt in the current directory, and there is in c:\ append c:\ is similar to the path command 36. Debug Debug file name 37. Diskcomp Compare the contents of two floppy disks 38. Expand Unzip command Expand original compressed file path destination 39. Fc Compare the contents of two files Example: fc /c test1.txt test2.txt rem /c Compare case-insensitively 40. Fasthelp Quick help Not available in the XP system Fashhelp command is equivalent to command/? 41. Qbasic Start the basic integrated environment 42. Setver Set version, and can also view the versions of winword and excel The common syntax in config.sys: device=setver.exe REM When device=setver is not added in config.sys, the versions of winword and excel can still be seen. Why can't anything be seen after adding it? There is no error message REM 43. Share 44. Subst Replace a certain drive letter with a specified path Example: subst a: c:\test means replace the A drive with the test directory under the C drive Subst a: /d means delete the replacement 45. Tree Display the structure of all subdirectories under the current directory or specified directory, but does not display file names /f (file): Display the directory and the file names under the directory at the same time Tree /a: Display the directory tree in ASCII code Example: To find the number of test.txt in the c:\test directory tree /f c:\test | find /c /I "test.txt" If there are too many files exceeding the buffer size, you can use: tree /f |more 46. Vsafe Virus protection program, not tried!!!!! 47. Ver View the DOS version number 48. Vol View the volume label of the disk 49. Ctty Change the input and output device of the control Example: ctty aux means set aux as the input and output device 50. Recover Overwrite the disk 51. Replace Example: replace c:\test1.txt d:\test2.txt /r Even if test2.txt has the read-only attribute, it will be replaced Example: replace d:\a.txt d:\backup /s /p /u /s: Replace all files with the same name in D:\backup and its subdirectories with D:\a.txt /p: Confirm before replacing /u: (update), check the modification time of the source file and the target file before replacing. If the former is newer, it will not be replaced Example: @echo off cd %1 for %%i in (*.%3) do if not exist "%2%%i" copy %%i "%2" /y replace *.%3 "%2" /u Execute filesyn.bat d:\ e:\ txt 52. at Customize scheduled tasks (the scheduled tasks customized by this command run in the background, while the tasks customized by "Task Scheduler" in Windows run in the foreground). The main difference between the two is that the at command can only view the scheduled tasks customized by the at command itself, while "Task Scheduler" in the control panel can view it and the tasks customized by the at command; both rely on the task scheduler service. If this service is stopped, running the tasks in "Task Scheduler" can start this service, but the tasks customized by the at command cannot. Example: Set the task customized by at to run in the foreground at 16:30 /interactive c:\test.bat /interactive can make the task run in the foreground Example: Run a program at a specified time At 16:00 c:\test.bat :: Run test.bat at 16:00 Example: Run a task at 15 At 16:30 /every:15 c:\test.bat :: Run at 16:30 on the 15th of each month At 16:30 /next:15 c:\test.bat :: Run at 16:30 on the 15th of next month At 16:30 /every:Thursday c:\test.bat :: Run at 16:30 every Thursday Example: Delete a task At 2 /delete /yes :: Delete the scheduled task with task ID 2, /yes means no need to confirm when deleting the task Example: Delete all tasks At /delete :: Not entering any ID means clear all scheduled tasks 53. shutdows Shutdown /s /t time /f /s Specify the shutdown operation /t How long to shut down after, such as /t 6, shut down after 6 seconds /f Force shutdown, regardless of what program is running Example: Run a program on a specified computer At \\computername 16:30 c:\test.bat 54. find Standard format find "string to find" text file to be searched Example: Find the nihao string in test.txt find "nihao" test.txt Find /c "nihao" test.txt :: /c (count) Count the number of times nihao appears in test.txt Find /n "nihao" test.txt :: /n (number) Count the lines where all nihao are located Find /i "nihao" test.txt :: /i (ignore) Ignore case Find /v "nihao" test.txt :: /v Find the content that does not contain the nihao string 55. Shift Increase the number of parameters in the batch 56. Systeminfo This command is useful if you need a batch to display system information. 57. Ntsd Force close process command, for example: ntsd –c q –pn notepad.exe 58. Create a hidden user Previously mastered the usage of the registry: 1. Use net to create a new user; 2. Export the name item under SAM in the registry; 3. Delete the newly created user; 4. Import the exported registry New method: net user zgktest$ /add, at this time, the newly created user cannot be viewed with net user; add a comment to the newly created user: net user zgktest$ /comment:"comment content" 59. Sfc Scan the integrity of protected system files /scannow: Immediately scan all protected system files and automatically repair if there are modifications. /verifyonly: Only scan and not repair /verifyfile: Only scan the specified file and not repair 59. Taskkill Terminate the process /f Force terminate /im Specify the image name of the process to terminate Example: taskkill /f /im ctfmon.exe /im notepad.exe III. Introduction to Batch Commands 1. Echo on/off Turn on or off echo echo off means not to display all command lines after it Echo display information Echo y|format a: means pass the parameter y Echo n|format a: The following are special ways to echo empty lines Echo. Echo+ Echo; Example: echo nihao @echo nihao Pause Execution result: echo nihao rem Display the command line itself nihao rem Display the execution result of the 1st command line nihao rem Display the execution result of the 2nd command line 2. @ Do not display the command line itself 3. Call Call another batch or file in a batch file Example: Call 2.bat in 1.bat 1.bat @echo off Echo this is 1.bat Call 2.bat :: At this time, using command /c 2.bat instead of call 2.bat is not okay. If there are Chinese characters in 1.bat or 2.bat, a bunch of garbled characters will be displayed :: Using start 2.bat instead of call 2.bat, it will not return to 1.bat after execution. Echo 2.bat end,back to 1.bat 4. Pause Pause by pressing any key 5. Goto label and :label used together Note that there are requirements for naming the label. If there are multiple labels in the batch, the first eight letters of the label name cannot be exactly the same, because COMMAND only recognizes the first eight. If the names are the same, the second label will never be executed. 6. Rem Comment, equivalent to :: and /* */ 7. % Parameter identifier Parameters refer to the strings added after the file name when running the batch file, ranging from %0~%9. %0 represents the file itself, and the strings are from %1~%9 8. If Conditional judgment statement If (not) /i "string1"="string2" command to be executed /I: Indicates case-insensitive judgment Example: @echo off Set /p var1= please input Set /p var2= please input If "%var1%"=="%var2%" (echo variables are equal) else echo variables are not equal :: The variables var1 and var2 use double quotes to compare spaces in the variables. For example, var1=a, var2=a. Without double quotes, they are equal and (echo variables are equal) is executed. With double quotes, (else echo variables are not equal) command is executed pause If (not) exist file command to be executed Example: if exist c:\test1.txt (echo test1.txt exists) else echo test1.txt does not exist If (not) errorlevel number command to be executed Example: @echo off Set /p commandname=Please input the command to be tested whether it exists If errorlever==0 (echo command executed successfully) else echo command did not execute successfully :: Once made a mistake that errorlever and 0 were forgotten to be = =, and there was no space between echo and the displayed information pause Example: @echo off if not exist d:\myfolder\nul md d:\myfolder if not exist d:\myfolder\word\nul md d:\myfolder\word REM In the win98 system, the if statement cannot detect the existence of the directory, but can detect the empty device, so to ensure the normal operation of the script, nul is used. If defined xxx command: Judge whether xxx is a defined variable Example: @echo off Set var=123 If defined var (echo var is a defined variable) else echo not defined pause If defined %var% (echo %var% is a defined variable) else echo %var% is not defined :: Judge whether the value of var is a variable pause Set 123=456 If defined 123 (echo 123 is a defined variable) else echo not defined pause 9. Choice Choice /c: Parameters indicate the characters that can be input, replaced by set /p, which is rarely used now 10. For Loop command FOR [%%f] in (collection) DO [command] /d Only effective for directory operations, ineffective for files Example: @echo off for /d /r %%i in (*) do @echo %%i :: List all unhidden directories in the current directory, and do not operate on files pause /r Recursive (that is, operate on the current directory, and all subdirectories under it are also operated) Example: @echo off for /d /r %%i in (*) do @echo %%i :: List all unhidden directories in the current directory and its subdirectories, and do not operate on files pause /l Iterate over the numerical range Example: @echo off for /l %%i in (1,1,5) do @echo %%i pause /f Read each line of content in the text and assign it to a variable or return the execution result of the command to a variable (very commonly used) Example: There is a text test.txt with the content: Name Relationship ; Display the corresponding relationship Zhang Yi Labor De Employer @echo off for /f "eol=; tokens=1,2 delims= " %%a in (c:\test.txt) do @echo %%a %%b :: Read the content of the text and assign it to the variable %%a, then display the two columns of content in the text. The line with a semicolon is not displayed :: eol=; means ignore the line starting with semicolon ;, even if this parameter is not written, it is executed by default :: tokens=1,2 means print/display the content of the 1st and 2nd columns of the text :: delims= means use space as the delimiter Pause Example: @echo off for /f "eol=; tokens=1,2 delims= " %%a in ('net user') do @echo %%a %%b ~I means delete the quotes Delete the quotes at the beginning and end Delete the quotes at the beginning The quotes at the end and in the middle are not deleted Example: There is a test.txt document with the following content: "afds" "daf Dfs" Afd"dfa Execute the following code @echo off for /f %%a in (c:\test.txt) do @echo %%~a pause The result is as follows: afds daf Dfs” Afd”dfa %~fi means expand %i to a fully qualified path Example: @echo off for /f %%i in ('dir /b') do @echo %%~fi pause %~di means expand %i to a drive letter %~pi means expand %i to the path %~ni means expand %i to the file name %~xi means expand %i to the file suffix %~si means expand %i to the short name of the file Example: @echo off for /f %%i in ('dir /b') do @echo %%~si pause If there is documents and setting in the root directory, only the short name of documents will be displayed %~ai means expand %i to the file attribute %~ti means expand %i to the size and date of the file %~zi means expand %i to the size of the file %~$path:I means search for the specified file in the specified path Example: @echo off for /f "delims=" %%i in ("ping.exe") do @echo %%~$path:i :: Once made a mistake, use double quotes instead of single quotes in the brackets pause 11., Equivalent to space Example: Executing dirc: in CMD will have an error message, using dir,c: will not 12. " " Delimiter, often used to delimit "directories with spaces" Example: Executing dir c:\documents and setting in CMD will have an error message, using dir "c:\document and setting" will not 13. ^ Escape character, cancel the function of special symbols Example: If you want to write the > special symbol into the text file echo > >test.txt is wrong Echo ^> >test.txt is correct, canceling the output redirection function of the > special symbol 14. & Command connection character, regardless of whether the command before & is executed correctly, the command after it will be executed in sequence 15. && Command connection character, only if the command before && is executed correctly, the command after it will be executed 16. || Command connection character, only if the command before || is executed incorrectly, the command after it will be executed 17. | Pipe command Take the output result of the previous command as the input of the next command 18. > Output redirection command Write the output result of the previous command to the device behind, and the content of the device behind is overwritten 19. >> Output redirection command Write the output result of the previous command to the device behind, and the content of the device behind is not overwritten 20. < Output redirection command Take the specified content of the device behind as the input of the previous device 21. * and? Wildcards * represents any number of characters ? represents any one character 22. Concept understanding: Variable expansion and delayed variable expansion Variable expansion: In the execution of the batch, the process of replacing the variable name with the value of the variable. Delayed variable expansion (using the flag setlocal enabledelayedexpansion) Example: @echo off for /l %%i in (1,1,5) do ( set var=%%i echo %var% ) Pause :: Code interpretation, the execution result of this code segment shows "5 echo are in the off state" :: Why? :: Because when CMD executes the batch, it first reads each command line into memory for matching :: Matching means checking whether there is a syntax error in the command line, not executing the command line :: The red code is one command line :: The variable var is first matched and cannot be executed, so at this time var has not been assigned and is empty Example: @echo off setlocal enabledelayedexpansion for /l %%i in (1,1,5) do ( set var=%%i echo !var! ) Pause :: Code interpretation, the execution result of this code segment shows "1 2 3 4 5 in the form of a sequence" :: Why does this code segment execute correctly? :: Because it uses "delayed environment variable expansion" :: Variables in delayed environment variable expansion use "! !" instead of "% %" :: The purpose of using delayed environment variable expansion is to execute the command line once first and then match :: Therefore, the variable var is first assigned to 1, and then the command line is matched Similarly: set var=test & echo %var% will have an error message: echo is in the off state setlocal enabledelayedexpansion set var=test & echo !var! is executed correctly 23. ; When the commands are the same, different targets can be separated Example: dir c:\;d:\;e:\ is equivalent to dir c:\ dir d:\ dir e:\ Supplementary: autoexec.bat: Batch file that runs automatically when the computer starts winstart.bat: Batch file that runs automatically when entering the Windows state Dosstart.bat: Batch file that runs automatically when restarting into MS-DOS under Windows Difference: The execution time periods are different IV. Skill Collection 1. *.* can be replaced by. Del *.* equ del . 2. To shield the display information of the command regardless of whether the command is executed correctly, redirect the command to the empty device NUL Example: dir >nul 2>nul 3. Add content to the text Type con >> text name REM Tested, press F6 key or CTRL+Z to stop input Type con >filename equ copy con filename type nul>filename means to clear the content of the file, but the attribute of filename cannot be read-only and hidden 4.指法练习 in DOS Copy con nul 5. Regedit /e Export the registry, for example: regedit /e test, export the registry database to the file test.reg Import the registry, for example: regedit/c test.reg, import test.reg into the registry Example: regedit test.dat, import test.dat into the registry 6. In "Run" or CMD, directly run "hh ntcmds.chm", which will start the "Command Line Reference" window, which is convenient for self-study of DOS commands 7. 2>nul 2 represents the error message handle 8. Make programs execute one after another start /w first program start second program 9. View the list of users connected to the local machine Net session Clear the user temp already connected to the local Net session \\temp /delete 10. In the subnet, cannot ping the gateway, but can ping other machines normally Solution idea: Obtain the correct MAC address of the gateway, then execute arp -a gateway IP gateway MAC address 11. Create a new file type nul>new.txt Example: Summary of methods to create a batch file a. type nul>test.bat (test.bat is empty, need to enter content to edit) b. copy con test.bat c. echo dir c:>test.bat echo dir d:>>test.bat d. edit test.bat edlin test.bat e. Create a text file under Windows and then change the extension (need to enter content to edit) 12. Command prompt is prohibited Solution idea: reg delete HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows /f >nul 13. How to understand the error message "bad command or filename" when the batch execution encounters it? Command error; The command is correct, but it is not in the current directory and the search path set by PATH; There are characters that COMMAND cannot interpret in the batch file content. 14. When writing a menu with a batch, do not use special symbols, such as: |, which will cause ambiguity and errors. 15. To chat with others on QQ, you can refer to the following code: Set/p qq=Please input the QQ number you want to chat with him/her (then press enter): start tencent://Message/?Uin=%qq% 16. How to add an empty line in a batch @echo off echo first^ echo second rem ^ is an escape character, which can escape the empty line. In fact, I don't understand it very well, but this method works? 17. How to call a vbs file in a batch (not tested) start "" a.vbs or start c:\a.vbs How to install an inf file in a batch (view relevant content in "Tools" "Folder Options" "File Types" Advanced under "Edit") rundll32.exe setupapi,InstallHinfSection DefaultInstall 132 c:\b.inf 18. If the hard disk is locked with Cacls, the solution Cacls D: /t /c /g dfdgdfghfh:f rem Unlock the D drive 19. Set the operation of the local "user account" control userpasswords2 V. Taking Things Out of Context 1. @echo msgbox "Download completed",4096 >> e:\test.vbs start e:\test.vbs REM The meaning of 4096 is: System mode: All applications are suspended before the user responds to the message box 2. Add a pop-up window in a batch a、Msg %username% /time:5 “hello,zgk” REM Requires the TermService service support; /time:5 means the pop-up window stays for 5 seconds; “hello,zgk” means the content of the pop-up window. b、net send %computername% “Content displayed in the pop-up window” >nul REM Requires the message messenger service support c、Use the built-in mshta program of the system Use the msgbox function in vbscript to display mshta vbscrip:msgbox(“Specific content displayed in the pop-up window”,64,”Title of the pop-up window”)(windows.close) REM 64 means the pop-up window stays for 64 seconds, and windows.close means close the window after staying for 64 seconds d、Use the popup function of vbscript mshta vbscript:CreateObject("Wscript.Shell").popup("Content displayed in the window",7,"Title of the window",64)(window.close) e、mshta javascript:window.alert("Warning window information");window.close() f、Use the popup function of javascript mshta "javascript:new ActiveXObject('WScript.Shell').popup('Hello,lxmxn',7,'batch script',64);window.close();" g、Use the confirm function of javascript mshta javascript:confirm("ha");window.close() 3. Code that continuously pops up windows @echo off Start notepad.exe %0 Rem Tried it, it's really fierce, only restart the machine 4. Set the number of loop times to start a program or command @echo off :loop If not exist c:\test.txt echo. >c:\test.txt &goto err1 If not exist c:\test1.txt echo. >c:\test1.txt &goto err1 If not exist c:\test2.txt echo. >c:\test2.txt &goto err1 If not exist c:\test3.txt echo. >c:\test3.txt &goto err1 :err1 Start notepad.exe Goto loop Rem In fact, it can be realized by other methods, just for learning! 5. Batch to record the login time of the computer @echo off Date /t >record.txt Time /t >>record.txt Attrib +s +h record.txt Attrib +s +h record.bat Finally, add record.bat to the following position in the registry: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] "Userinit"="C:\\WINDOWS\\system32\\userinit.exe,record.bat," Note: Write according to the actual path of record.bat. Remember, the comma, after it cannot be omitted! 9. Hide the "flashing CMD window screen" when the batch file runs Method 1: Use JS code new ActiveXObject('WScript.Shell').Run('cmd /c hidden.bat',0); Method 2: Use VB code Set ws = CreateObject("Wscript.Shell") ws.run "cmd /c felix.bat",vbhide Method 3: Use VB code CreateObject("WScript.Shell").Run "cmd /c felix.bat",0 10. Copy a certain file to all directories including subdirectories on a certain disk @echo off for /r D: %%i in (.) do copy /y 01.txt "%%i" pause REM Copy 01.txt to all directories under the D drive 11. How to clean up garbage files more cleanly Pay attention to directories such as cookies, temp, tempor~1, history and win386.swp and registry information 12. Script code to run the program in quiet mode On Error Resume Next set wshshell=createobject("wscript.shell") a=wshshell.run ("file name",0) rem The file name is the program to be run, and 0 means the program does not display the running window when it runs 13. Batch code to delete garbage files such as found.001/found.002, which is good! for %%a in (c d e f g) do ( for /r %%a:\ %%i in (FOUND.00*) do (rd /s /q %%i >nul 2>nul) ) 14. Batch code to clear duplicate lines @echo off for /f "delims=" %%i in (a.txt) do ( findstr /c:"%%i" b.txt 2>nul||echo %%i>>b.txt ) Pause rem Why can it be realized? rem For example, the content of a.txt is "Zhang San Li Si Zhang San" in three lines, then assign Zhang San to the variable %%i, and then execute to find "Zhang San" in b.txt. If not found, it means that the first half of the second line statement is wrong, so the command after || is executed. Then, the second time Zhang San is assigned to %%i and then Zhang San is found in b.txt Rem can be found, the statement is correct, and the latter command will not be executed. I don't know if the explanation is correct 17. Can the file type in the registry be modified through a batch to realize "a certain type of file cannot be opened"? @echo off reg add HKCR\.bat /f /ve /d bat >nul 2>nul reg add HKCR\.cmd /f /ve /d cmd >nul 2>nul echo. echo Oh, bat/cmd files cannot be opened or edited! pause >nul Rem Bat and cmd suffix files cannot be opened. If you need to open them, you can restore the registry. rem Of course, the premise is that there is a registry backup |
|
| Floor2 tuliangwl | Posted 2008-03-11 17:43 |
| 初级用户 Posts 28 Credits 55 | |
|
A sofa, heh heh! Learn...
|
|
| Floor3 jydyx | Posted 2009-02-09 13:58 |
| 新手上路 Posts 8 Credits 14 | |
| Floor4 cnmagician | Posted 2009-02-09 16:22 |
| 初级用户 Posts 32 Credits 38 | |
|
Learn slowly, no fear.
|
|
| Floor5 stlzlg | Posted 2009-03-02 12:34 |
| 新手上路 Posts 5 Credits 10 | |
|
It's a rather long note. It seems I need to digest it slowly.
|
|
| Floor6 cuihao1472 | Posted 2009-03-04 00:33 |
| 新手上路 Posts 7 Credits 14 From 北京 | |
| Floor7 fuagui | Posted 2009-04-02 22:44 |
| 新手上路 Posts 3 Credits 6 From 湖南 | |
|
The building owner has worked hard. The materials from shallow to deep are really suitable for learning
|
|
| Floor8 tinlin | Posted 2009-04-05 00:39 |
| 初级用户 Posts 76 Credits 160 | |
|
THANK FOR YOU SHARING!
|
|
| Floor9 kida | Posted 2009-04-12 10:37 |
| 初级用户 Posts 63 Credits 139 | |
|
Wow, so long, heh, learning
|
|
| Floor10 deaniqpl | Posted 2009-04-13 16:48 |
| 新手上路 Posts 8 Credits 14 | |
|
Learned a lot from this note, easy to understand
|
|
| Floor11 generalchd | Posted 2009-04-24 05:51 |
| 新手上路 Posts 5 Credits 10 | |
| Floor12 qiuzisiyu | Posted 2009-04-27 14:16 |
| 新手上路 Posts 3 Credits 4 | |
|
Learning is endless. Really thank the LZ for sharing. You've worked hard.
|
|
| Floor13 dongbao1206 | Posted 2009-05-01 23:54 |
| 新手上路 Posts 8 Credits 15 | |
|
Thanks for sharing
|
|
| Floor14 cisheng97 | Posted 2009-05-04 08:12 |
| 初级用户 Posts 24 Credits 31 | |
|
Well, that's great. If you like it, just go ahead and learn.
|
|
| Floor15 liaobin | Posted 2009-05-15 07:43 |
| 新手上路 Posts 5 Credits 10 | |
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |