Windows批处理文件有哪些不太为人所知,但很重要和有用的特性?
指南:
每个答案一个特征 给出特性的简短描述和示例,而不仅仅是文档链接 将答案限制在本地功能,即不需要额外的软件,如Windows资源包
澄清:这里我们指的是由cmd.exe处理的脚本,这是WinNT变体的默认值。
(请参见:Windows批处理文件:.bat vs .cmd?)
Windows批处理文件有哪些不太为人所知,但很重要和有用的特性?
指南:
每个答案一个特征 给出特性的简短描述和示例,而不仅仅是文档链接 将答案限制在本地功能,即不需要额外的软件,如Windows资源包
澄清:这里我们指的是由cmd.exe处理的脚本,这是WinNT变体的默认值。
(请参见:Windows批处理文件:.bat vs .cmd?)
当前回答
抽取随机的文本行
@echo off
:: Get time (alas, it's only HH:MM xM
for /f %%a in ('time /t') do set zD1=%%a
:: Get last digit of MM
set zD2=%zD1:~4,1%
:: Seed the randomizer, if needed
if not defined zNUM1 set /a zNUM1=%zD2%
:: Get a kinda random number
set /a zNUM1=zNUM1 * 214013 + 2531011
set /a zNUM2=zNUM1 ^>^> 16 ^& 0x7fff
:: Pull off the first digit
:: (Last digit would be better, but it's late, and I'm tired)
set zIDX=%zNUM2:~0,1%
:: Map it down to 0-3
set /a zIDX=zIDX/3
:: Finally, we can set do some proper initialization
set /a zIIDX=0
set zLO=
set zLL=""
:: Step through each line in the file, looking for line zIDX
for /f "delims=@" %%a in (c:\lines.txt) do call :zoo %zIDX% %%a
:: If line zIDX wasn't found, we'll settle for zee LastLine
if "%zLO%"=="" set zLO=%zLL%
goto awdun
:: See if the current line is line zIDX
:zoo
:: Save string of all parms
set zALL=%*
:: Strip off the first parm (sure hope lines aren't longer than 254 chars)
set zWORDS=%zALL:~2,255%
:: Make this line zee LastLine
set zLL=%zWORDS%
:: If this is the line we're looking for, make it zee LineOut
if {%1}=={%zIIDX%} set zLO=%zWORDS%
:: Keep track of line numbers
set /a zIIDX=%zIIDX% + 1
goto :eof
:awdun
echo ==%zLO%==
:: Be socially responsible
set zALL=
set zD1=
set zD2=
set zIDX=
set zIIDX=
set zLL=
set zLO=
:: But don't mess with seed
::set zNUM1=
set zNUM2=
set zWORDS=
其他回答
脚本所在的路径(带驱动器):~dp0
set BAT_HOME=%~dp0
echo %BAT_HOME%
cd %BAT_HOME%
抽取随机的文本行
@echo off
:: Get time (alas, it's only HH:MM xM
for /f %%a in ('time /t') do set zD1=%%a
:: Get last digit of MM
set zD2=%zD1:~4,1%
:: Seed the randomizer, if needed
if not defined zNUM1 set /a zNUM1=%zD2%
:: Get a kinda random number
set /a zNUM1=zNUM1 * 214013 + 2531011
set /a zNUM2=zNUM1 ^>^> 16 ^& 0x7fff
:: Pull off the first digit
:: (Last digit would be better, but it's late, and I'm tired)
set zIDX=%zNUM2:~0,1%
:: Map it down to 0-3
set /a zIDX=zIDX/3
:: Finally, we can set do some proper initialization
set /a zIIDX=0
set zLO=
set zLL=""
:: Step through each line in the file, looking for line zIDX
for /f "delims=@" %%a in (c:\lines.txt) do call :zoo %zIDX% %%a
:: If line zIDX wasn't found, we'll settle for zee LastLine
if "%zLO%"=="" set zLO=%zLL%
goto awdun
:: See if the current line is line zIDX
:zoo
:: Save string of all parms
set zALL=%*
:: Strip off the first parm (sure hope lines aren't longer than 254 chars)
set zWORDS=%zALL:~2,255%
:: Make this line zee LastLine
set zLL=%zWORDS%
:: If this is the line we're looking for, make it zee LineOut
if {%1}=={%zIIDX%} set zLO=%zWORDS%
:: Keep track of line numbers
set /a zIIDX=%zIIDX% + 1
goto :eof
:awdun
echo ==%zLO%==
:: Be socially responsible
set zALL=
set zD1=
set zD2=
set zIDX=
set zIIDX=
set zLL=
set zLO=
:: But don't mess with seed
::set zNUM1=
set zNUM2=
set zWORDS=
我真的很喜欢这个Windows XP命令参考,以及在顶部的语法链接;它涵盖了许多已经在其他答案中找到的技巧和技巧。
调用集——将环境变量展开几个层次。
在http://ss64.com/nt/call.html#advanced从另一个SO问题的答案中发现这个批处理文件变量在for循环中初始化
set VarName=Param
set Param=This
call set Answer=%%%Varname%%%
Echo %Answer%
给了
set VarName=Param
set Param=This
call set Answer=%Param%
Echo This
This
cmd.exe中的快速编辑模式是我的最爱。这有点偏离主题,但是当与命令shell交互时,它可以是一个救星。不,我并不是在夸张——在你死之前,你只会看到一定次数的* * * * * v;你看得越多,死得越快。
打开regedit(小心,不是我的 故障、蓝屏等) 进入HKCU/控制台 “quickkedit”设置为“1”
(你也可以从UI设置,这可能是更好的方法。有关说明,请参阅注释。还有一个很好的一行脚本也可以做到这一点。)
现在,要复制,只需左键单击并拖动选择,然后右键单击复制。要粘贴,只需右击。
不再有^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v ^ v !!
该死,我想我刚杀了人。对不起!