In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what batch orders are there". The content of the explanation is simple and clear, and it is easy to learn and understand. now please follow the editor's train of thought to study and learn "what batch orders are there?"
1. Set:
1. Set (no switch)
Set. = test
Set.
:: if a variable is:\. At the beginning of these three path-related symbols, you can omit a space when you use set to view variables that begin with that character.
Echo tmp:*\ =%
:: displays the section after the first\ of the tmp variable, and the rest of the variable substitution and offset is too simple to explain.
2. Set / p variable name = comment nul | | echo variable n is not pure number or zero
Using the characteristic that the denominator cannot be zero, set is used to judge whether a variable is a non-zero pure number.
Set nasty 1
Set / a "naughty dollars 123 |!! 234 percent percent zero"
:: bit operations,!, ^, | and & are commonly used in Boolean operations, while logical shifts are often used in binary operations (> > you can also determine whether the value is negative, see the following example)
Set / a nasty echo 100, "1 / (- 100 > > 31)" | | the variable n is negative.
In accordance with the storage characteristics of positive and negative numbers in cmd, the "Boolean operation" of judging positive and negative numbers can be realized by logical displacement, and a variety of algorithms can be derived, such as comparing the size of two or more numbers with slight modification.
Set / a nasty colors 100
:: use ~ to reverse all binary ones and zeros, and the combination of negative signs after or before can simply add or subtract 1. This technique is mainly used to reduce the use of parentheses, because the priority of ~ sign and minus sign is higher than that of arithmetic operator.
Set / a test=%test:~5,1%-0
:: can avoid errors when% test:~5,1% is empty
Set / a decimal = 0x hexadecimal, decimal = 0 octal
Quickly convert hexadecimal and octal numbers to decimal, but unfortunately there is no binary.
: loop
Set / a nasty 1001
Echo% NRV 3%
Goto loop
:: this is superior to the conventional filling method
For an in (test 123 ABC test @ # $123) do set / a ".% astat1"
Set. | findstr / v / e "= 1"
:: a classic scheme to obtain the number of repetitions of a string
2. For:
This is the strongest internal command in batch processing, not one of them!
1. For (no switch)
The code is as follows:
For% an in (c:\ *. *) do echo% a
:: show all non-hidden, non-system property files in the root directory of C disk
For% an in (.\..) Do echo% ~ nxa
:: displays the folder name of the directory at the previous level
Set str=123234345
Set str=%str:,=\%
For% an in (% str%\..) Do echo% ~ nxa
Using the previous technique, we skillfully take the penultimate string and match it with for / f "delims=\".
For an in (* .txt) do (
For / f "useback delims="% b in ("% a") do (
Set str=%%b
For% c in ("! str: delimiter ="! ") Do (
For / f "tokens=1*" d in (c) do echo% ~ d
)
)
)
The combination of for without parameters and for / f is very powerful, just to cite this one example
For an in 123do for an in 234 do for an in 345 do echo a
:: in fact, if you only read the parameters of the last layer of for, even if multiple layers of for are nested, you can use the same parameters, such as% a
For z in (! tmp!) Do echo! z!
: at present, it is known that the best way to get rid of call to achieve multi-layer variable nesting, many people use
2 、 for / l
For / l an in () do echo
:: infinite loop, the number of steps is 0 is the same effect, but not this concise
For / l an in (- 4 1) do echo a
:: the three parameters in for / l from left to right are the initial value, the number of steps and the termination point, respectively. When the number given by the user is insufficient, the insufficient term will be assigned 0 in the order from right to left.
3 、 for / d / r
For / r / d an in (*) do echo% a
:: you can traverse all subfolders, and you can combine the r switch and the d switch because their parameters overlap, but the l switch and the f switch don't work.
4 、 for / f
For / f itself does not have many skills, its advantage is that it can analyze the output of other commands as input, so for / f can be said to be the well-deserved king of internal commands.
For / f "tokens=* delims=0123" an in ("0000123456") do echo a
:: n characters with prefix removed
For / f "skip=99" an in (1.txt) do echo 1.txt at least 100 lines
:: I was quite impressed when I saw it written by a moderator.
For / l% an in (1 1 10) do (
For / f "tokens=1,2* delims=\"% an in ("! tmp!") Do (
For c in (a b) do echo c
Set tmp=%%c
)
)
:: infinitely expand the range of values of tokens
Set tmp=123=234=345=456
For / l% an in (1 140) do (
For / f "tokens=1,2* delims==" an in ("! tmp!") Do (
Set stringency stringency%% a department%% b
Set tmp=%%c
)
)
Echo str:~1%
: sometimes set variable substitution cannot replace some special characters, so it can be handled with for / f
Set test=d:\ test\
For an in (test.*) do (
If "% ~ za" neq "% ~ z$test:a" replace / p / u "% a"% ~ dp$test:a "
)
:: determine whether the file with the name test in the current directory exists in the d:\ test\ folder with the same name. If it exists, the size is different and the modification date is earlier, it will be replaced, otherwise it will not be processed. The "% ~ dp$path:a" parameter in the for help message does not seem to have been used by anyone, although its scope of application is very narrow, but it might as well give it a try in specific cases.
Setlocal enabledelayedexpansion
Set t=tmp
Set @ = t
For / f% an in ('echo!% @%!') Do echo! a!
:: another three-tier nesting method is not practical.
III. Findstr
My favorite command, but the startup speed of the external command is too slow, so I seldom show up when I use it.
Findstr / s / m. * *.
:: in fact, findstr is also a dir. Although it is slower than dir, it has more functions to find the contents of files.
Findstr / n. * 1.txt | findstr "^ 5000:"
:: a very practical method to fetch specified rows, which can be taken within the specified range with regularization.
Set / p n = Please enter numbers or uppercase and lowercase letters
(echo! n!) | findstr / I "[0-9a-Z]" & & echo was typed incorrectly!
:: is this practical enough? Do not explain
Dir | findstr ['- Z]
:: find lines with wide characters by using the order of the actual size of characters in the findstr and if commands
Findstr / x "." 1.txt
:: find 10-byte lines in 1.txt
(type 1.txtecho;) | findstr / o. * | more + 1
:: with for, it is easy to get the number of bytes per line of 1.txt
Findstr > 1.txt / m / p. * *.
Dir / b / a g:1.txt | findstr > 2.txt / v / I / m / g:1.txt
:: to get the file name containing non-printable characters, the key is to fetch the set of findstr.
Findstr "^ Rar!" / g:1.txt
: here 1.txt is the 1.txt of the previous technique, the content is a list of all files containing non-printable characters, this technique can search rar files, although simple, but so far has not missed, original.
More > tmp + 2 1.txt
Findstr > the first two lines. Txt / x / v / g:1.txt 2.txt
:: sometimes you can use this method to get the first few lines, of course, in most cases, it is not as appropriate as for / f, and there is a special character bug
@ echo off
Findstr / n. * 1.txt > tmp1
Find / n / v "" 2.txt | more > tmp2 + 2
For / f "tokens=2*delims=]:% an in ('fc / n / lb10000 tmp1 tmp2^ | sort') do (
Echo;%%b
)
Del tmp?
Pause
: qzwqzw pioneered the idea of using fc / n to output double text at the same time, but there is a defect that the sorting may be disrupted, so a find is added to make up for it.
IV. Start, call, cmd
They are put together because the functions of these three commands overlap.
1 、 start
@ echo off
1 cd. > tmp
Set / packs% 1
% 1 start / b ""% 0: (quit after five seconds) tm
If not "1" = "" goto 1
Set / p n = enter any character
If defined n (
Del tmp
Echo you entered% n% and quit after five seconds.
) else echo input is empty! Quit in five seconds.
: (quit after five seconds)
Ping / n 5 localhost > nul
If exist 2p exit
Pause
:: clever use of start / b to let set / p achieve the delay function of choice. I don't know which one of my predecessors invented it. I like it again. The skills of% 1 and% 2 here are only embellished. I just think it's fun to build building blocks.
2 、 call
Set aquib
Set Benec
Call echo%% a%
Delay reading and nesting of variables can still be realized with the help of call without variable delay, but it is defective in efficiency
3 、 cmd
Set aquib
Set Benec
Cmd / c echo%% a%
This proves that the effect of a call command is similar to that of cmd / c. The difference between the two commands is that "for" and "if" commands cannot be run with call, because for and if may actually be keywords rather than real commands.
Set aquib
Set Benec
Cmd / v:on / c echo!% a%!
:: without setlocal, you can still use variable delay
1 0:: echo; successfully called itself
2
: personal is very common, here with% 1 and% 2 skills for my preference, which:: can be changed to rem depending on the situation. Although the cmd command does not appear here, it actually executes cmd / c% 0 when running itself.
@ echo off
1 cmd / v:on / c 0::
Set nasty 123
Echo! n!
Pause
:: integrate the first two techniques to enable variable delay without using setlocal
@ echo off
Set str=test Test 1234
Setlocal enabledelayedexpansion
For / f "delims=:;"% an in ('((cmd / u / c echo! str! ^) ^ & echo^; ^) ^ | findstr / o ^;') do set / a n=%%a-5
For / f "delims=:" an in ('((echo! str! ^) ^ & echo^; ^) ^ | findstr / o ^;') do set / a d=n-%%a+3
Set / a macaque 2pm sqummerd
Echo total! M! Two characters,! d! Single-byte characters,! s! Double-byte characters
Pause
:: an alternative method for judging the number of single and double characters in three steps. The advantage is that it supports the calculation of super-long strings (at this time, there are many steps and difficult to use with conventional algorithms), and the disadvantage is that it is inefficient.
Ren 1.exe 1.bat
Echo, double-click 1.bat.
:: why can it work like this? Because exe is opened in "% 1"% * and bat is cmd / c "% 1"% *, treating exe as a bat runtime is equivalent to cmd / c 1.exe. However, this is only suitable for double-click to open, when calling this file within the cmd is run as a real bat, so there will be errors.
V. other orders
1. Xcopy is much more powerful than copy. The biggest pity is that it is an external command.
Xcopy / a source folder destination folder
:: xcopy is also very useful for screening
Xcopy / l / y / n% cd%.
:: the short name of the file in the current directory will not really be copied.
Xcopy / DVR 1-31-2011 / l "% cd%" tmp\
:: get a list of files with a modification date after January 31, 2011
Xcopy / t * .txt C:\ test\
:: copy the directory structure containing txt files to C:\ test
@ echo 1.txt > list
Xcopy / exclude:list? .txt test\
:: copy all files with a single character name to the test folder
Xcopy / s * .txt..\ txt\
:: copy all the subfiles named txt to the txt folder in the directory above
For / f "delims=" an in ('dir / s / b / ad ^ | sort-r') do rd "a" 2 > nul
:: the classic idea of deleting empty folders, using the feature that rd does not delete non-empty folders by default, to delete empty folders in sequence
For / d% an in (*) do (
Xcopy / Q / h / r / s / k "% a"tmp\"
Rd / s / Q "% a"
Ren "tmp"a"
)
Delete an alternative scheme for an empty folder
2. Compared with the previous boss-level commands, these commands are relatively inconspicuous, so they fall into the same category
Copy nul+Unicode.bat decryption .bat
:: use Unicode file header to encode obfuscated encrypted bat, you can use this command to decrypt
Echo > tmp 12323412 2323242134122434 345
More / T20 tmp > alignment. Txt
Type aligns .txt
Pause
The t switch of the more command is also of great use, and the hidden rules are not explained.
Cmd / u / c echo 0123456789 | more
The:: more command converts the nul characters output from cmd / u to spaces, allowing you to print a single byte character word for word.
@ echo off&setlocal enabledelayedexpansion
Set nasty 32768
(for% an in (16384 8192 4096 2048 1024 5126128 64 32 16 84 2 1) do sort / rec! n!% 0&&set / a ncopyright%% a | | set / a naughty%% a) > nul 2 > nul
The longest line of echo has n characters
Pause
:: this may be used to judge the number of characters of the longest line when the number of characters of the longest line is greater than 128 (the rec switch will fail when it is shorter than 128, and the N power of 2 in the code is cobbled together, which can be omitted in actual combat), it supports super-long strings, and the efficiency of calculating large files is obviously better than the traditional algorithm. The new half method comes from the repost of plp626, and the / rec switch of sort is more chicken-ridden. After thinking about it, I can only think of this use. I have never seen a precedent.
Ren 1.exe 1.bat
Echo, double-click 1.bat.
:: why can I change exe to run under the suffix of bat? Because exe is opened in "% 1"% * and bat is cmd / c "% 1"% *, treating exe as a bat runtime is equivalent to cmd / c 1.exe. However, this is only suitable for double-click to open, when calling this file within the cmd is run as a real bat, so there will be errors. And for the same reason, it can be changed to a com or cmd suffix.
3. Introduce some tricks in the cmd window. Of course, they are just "deceiving" the cmd window. Once it is output to a file, it will reveal its original shape:
@ echo off
Echo 1
Echo 2
Echo 3
Echo is retrogressive.
Pause > nul
:: this is awesome. I don't know who found it.
Set "dq="
(echo 2, the importance of family planning% dq%
Echo 1. Implement the scientific concept of development% dq% wow) | sort
:: with the help of the tab key and backspace character to achieve multi-line binding sorting and wrong line display, the space between tab and backspace is the key, otherwise it becomes a backspace.
Set / p = show different colors on the same line:
Set / p = blue on red background
Echo green words with yellow background
Findstr / aPUR 41. * Blue letter on red background?
Findstr / aVOR 62. * green letters with yellow background?
Del > nul red background blue word yellow background green word
Pause
It is a common way to display different colors on the same line, but many people always use (four backspaces and four spaces) to show that they do not understand the meaning of the backspace key.
@ prompt $_
Dir fuck.tmp
Pause
: with this prompt, when the echo is turned on, both the command and the command result can be output at the same time, without any extra content, so it is suitable for making bat running log.
Echo
: this black dot appeared as a supporting role in the previous introduction, which is the 0x07 in the ANSI code, which is also equivalent to the ctrl+G entered in cmd. It will emit a "drip" every time it is displayed on the screen, so be careful when you findstr *. * in the future (unless you have to, it is recommended to add / p switch when you need to display the results to the window), in case tens of thousands of results are accidentally printed out. Your computer will keep ringing like a telegram. I've been hit N times.
VI. The operating Mechanism of cmd
1. Preprocessing mechanism: priority of special characters, division of sentences and chunks.
Setlocal enabledelayedexpansion
(set nasty 3
Set / a nemesis 2thecommenting% n% homemade nailing n)
:: using the preprocessing mechanism to interpret a variable as multiple values
Setlocal enabledelayedexpansion
Echo ^!
When there is a variable delay symbol in the statement, it will be preprocessed twice, which must be noted.
Set str=.
Set "str name=str"
For an in (tmp%) do if defined an echo a variable str exists
: make use of the characteristic that the parameter variables of for are explained after the if parameters are divided, to make up for the compatibility defect of if defined with blank variable names, the essential reason is that for and if are special functions, and their parameters have been "remembered" by cmd in the preprocessing of chunks, and then they can not be changed.
(del 0
If echo can find me, he'll give you candy.
Pause > nul)
:: the contents in parentheses are understood as a chunk, and the commands do not need to be read from the file when running, so you can still run even if you delete them.
Echo "test&pause | sort
:: when there are odd double quotation marks in a command, all subsequent characters will be escaped
For / f tokens ^ = 2delims ^ = ^ "an in (" 123 "test" 456 ") do echo% a
:: by escaping special characters, use double quotation marks as delimiters in for
For / f tokens ^ = 2delims ^ = ^ "an in (^" 123456 ") do echo% a
Set / p = ^ "
Errors often occur when a set of strings contains odd double quotation marks. The solution is to escape one of them and keep valid double quotation marks in pairs, but they cannot be escaped by escape characters within the quotation mark pairs, so escape characters should be used outside the quotation mark pairs.
Set / a "1 / (% random%%%2)" & & set com= | | set com=/f "tokens=2"
For com% an in ("123 234 345") do echo% a
:: if the random value is even, the second segment of the specified string is displayed, otherwise the entire segment is displayed. Customizing commands with variables here is more flexible and convenient than the conventional approach (one if corresponds to one command), but it is important to note that variable delay occurs after interpreting chunks, so the% com% here cannot use variable delay.
Set / a\ test1=123,test2=234
(@ echo off
For / f "tokens=1* delims=="% an in ('set\') do echo% b
) | sort
:: sort sorts the output of the for command, and the @ echo off is not redundant, because if the chunk before the channel (for, if, or statements wrapped in parentheses), the contents of the chunk will run in the form of cmd / c, the echo is turned on, and the variable delay is turned off by default.
Dir / ad 123\ & & md234 | | rd 345&tree / f | more
When there is a 123 folder, create a 234 folder, otherwise delete the 345 folder, regardless of the result, the current directory tree will be displayed screen by screen. The key point is the flexible use of pipeline commands and logical connectors.
2. The wonderful use of handles
@ echo off 2 > nul 3 > nul
This command does not exist.
What about the echo error echo?
Pause
Handle backup, which can be used to block all correct or error echoes
Cd. > 1.txt 2 > 2.txt 3 > 3.txt 4 > 4.txt 5 > 5.txt 6 > 6.txt 7 > 7.txt 8 > 8.txt 9 > 9.txt
:: create 9 files with one command, the efficiency is naturally improved
@ echo off
(for / r% an in (*. *) do del / f / s "% ~ nxa" 3 > > "% a") 2 > nul 4 > > 0
Pause
:: efficiently delete duplicate files by using the feature that the write handle will occupy the file
Thank you for your reading. The above is the content of "what batch orders are there?" after the study of this article, I believe you have a deeper understanding of the problem of which batch orders are available, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.