Windows批处理文件有哪些不太为人所知,但很重要和有用的特性?

指南:

每个答案一个特征 给出特性的简短描述和示例,而不仅仅是文档链接 将答案限制在本地功能,即不需要额外的软件,如Windows资源包

澄清:这里我们指的是由cmd.exe处理的脚本,这是WinNT变体的默认值。

(请参见:Windows批处理文件:.bat vs .cmd?)


当前回答

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 !!

该死,我想我刚杀了人。对不起!

其他回答

一行中包含多个命令,在很多情况下都很有用:

&用于组合两个命令,执行command1和command2 &&一个条件组合,如果command1成功完成,则执行command2 仅当command1未成功完成时才执行Command2。

例子:

:: ** Edit the most recent .TXT file and exit, useful in a .CMD / .BAT **
FOR /F %%I IN ('DIR *.TXT /B /O:-N') DO NOTEPAD %%I & EXIT


:: ** If exist any .TXT file, display the list in NOTEPAD, if not it 
:: ** exits without any error (note the && and the 2> error redirection)
DIR *.TXT > TXT.LST 2> NUL && NOTEPAD TXT.LST

批处理脚本最常见的需求之一是记录生成的输出,以供以后检查。是的,您可以将stdout和stderr重定向到一个文件,但是您无法看到发生了什么,除非您跟踪日志文件。

因此,考虑使用stdout/stderr日志记录工具运行您的批处理脚本,例如logger,它将使用时间戳记录输出,并且您仍然能够看到脚本进程。

另一个stdout/stderr日志记录实用程序

Yet another stdout/stderr logging utility [2010-08-05]
Copyright (C) 2010 LoRd_MuldeR <MuldeR2@GMX.de>
Released under the terms of the GNU General Public License (see License.txt)

Usage:
  logger.exe [logger options] : program.exe [program arguments]
  program.exe [program arguments] | logger.exe [logger options] : -

Options:
  -log <file name>  Name of the log file to create (default: "<program> <time>.log")
  -append           Append to the log file instead of replacing the existing file
  -mode <mode>      Write 'stdout' or 'stderr' or 'both' to log file (default: 'both')
  -format <format>  Format of log file, 'raw' or 'time' or 'full' (default: 'time')
  -filter <filter>  Don't write lines to log file that contain this string
  -invert           Invert filter, i.e. write only lines to log file that match filter
  -ignorecase       Apply filter in a case-insensitive way (default: case-sensitive)
  -nojobctrl        Don't add child process to job object (applies to Win2k and later)
  -noescape         Don't escape double quotes when forwarding command-line arguments
  -silent           Don't print additional information to the console
  -priority <flag>  Change process priority (idle/belownormal/normal/abovenormal/high)
  -inputcp <cpid>   Use the specified codepage for input processing (default: 'utf8')
  -outputcp <cpid>  Use the specified codepage for log file output (default: 'utf8')

狡猾的等待N秒的技巧(不是cmd.exe的一部分,但不是额外的软件,因为它是Windows自带的),参见ping行。您需要N+1个ping,因为第一个ping没有延迟。

    echo %time%
    call :waitfor 5
    echo %time%
    goto :eof
:waitfor
    setlocal
    set /a "t = %1 + 1"
    >nul ping 127.0.0.1 -n %t%
    endlocal
    goto :eof

我总是发现很难阅读每行用关键字标记的注释:

REM blah blah blah

易于阅读:

:: blah blah blah

稍后可以使用call对名称求值,从而得到一些有用的属性。

call set SomeEnvVariable_%extension%=%%%somevalue%%%

使用调用来设置名称依赖于其他变量的变量。如果与某些变量命名规则一起使用,则可以通过使用谨慎的命名规则来模拟数组或字典等数据集合。somevalue周围的三个%是这样的,它将在调用之后和调用set之前计算为一个变量名,周围是一个%。这意味着一行中的两个%转义为一个%字符,然后它将再次展开它,因此somevalue实际上是一个名称指针。

call set TempVar=%%SomeEnvVariable_%extension%%%

将它与一个临时变量一起使用以检索值,然后可以在逻辑中使用该值。这在与延迟变量展开一起使用时非常有用。

要正确使用此方法,需要启用延迟变量扩展。因为它在默认情况下是关闭的,所以最好在脚本中启用它,将其作为第一个指令之一:

setlocal EnableDelayedExpansion