Windows cmd

Windows cmdshell

对于跨平台的程序,一般会提供一些有用的命令行工具,所以shell脚本、bat脚本文件就必不可少了。网络上shell的书、文章都很多的,因此了解起来会相对容易的多,而windows下的bat网上则少有涉及。这里不打算写windows bat 编程大全,而是简单对bat作一个简单的入门级的学习。express

不论在写shell,仍是bat,它们的设计都遵照这样一条原则:一切都是命令。Windows下命令是大小写不敏感的。编程

 

基本命令(rem, echo, @,/?)

rem:注释 (comment, remarks)。参数能够是任何内容。windows

echo :它有两个功能:打印消息、调试开关。若是参数是on 或者off,表明打开、关闭调试,若是后面是其它内容,则参数表明要输出的消息。为何说是debug开关呢?若是设置了echo on,随后执行的任何命令及其执行结果都会输出到标准输出流。网络

@用于关闭某个命令的调试信息,意思是说使用@标注的命令不会打出命令自己、执行结果。工具

/? 查看命令帮助学习

例如:spa

REM open the cmd echo
@echo on
echo hello, windows cmd 
@echo hello, windows cmd 

REM close the cmd echo
@echo off
echo hello, windows cmd 
@echo hello, windows cmd

 

执行结果: 命令行

D:\Note\windows cmd>REM open the cmd echo

D:\Note\windows cmd>echo hello, windows cmd
hello, windows cmd
hello, windows cmd

D:\Note\windows cmd>REM close the cmd echo
hello, windows cmd
hello, windows cmd

 


对于REM的命令,也是会打到STD里,若是不但愿看到,就可使用@标注。 debug

 

 

控制命令(if-else, for-in-do, goto)

 

if-else

 

if [noterrorlevel number command [else expression] 基于上一个命令执行的结果进行断定操做

if [notstring1==string2 command [else expression] 断定两个字符串是否相等

if [notexist FileName command [else expression]  断定指定的文件是否存在

If command extensions are enabled, use the following syntax:

if [/istring1 CompareOp string2 command [else expression] 进行字符串比较 (equ, neq, lss, leq, gtr, geq)

if cmdextversion number command [else expression]

if defined variable command [else expression]

 

 

 

For-in-do

循环执行,命令语法:

for {%variable | %%variable} in (set) do command [CommandLineOptions]

1)For, in, do 是基本结构,必不可少;

2){%variable | %%variable} 必要的,变量大小写敏感。

在命令提示符中执行for时,for中引用变量时,使用%

在批处理文件中执行for时,for中引用变量时,使用%%

此外,为了不与bat文件的参数 %0到 %9相冲突,因此变量不能是0-9的数字

3)( set ) 必要的。用于指定多个 files, directories, range of values, textstrings。括号不能省。

 

4)command 必要的,表明要执行的命令。

5)commandLineOptions, 执行command时所需的参数

 

更多用法,可能参考:https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v%3dtechnet.10)

 

 

goto

语法:goto label

跳转到指定的label。若是指定的label不存在,就继续执行下一条命令。若是找到label,就从label处继续执行。若是程序以正常顺序执行到一个label处,而不是经过goto跳转到label,label下的语句仍旧以正常顺序执行。

 

 

 

 

想要了解更多指令参见:

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490890(v%3dtechnet.10)

相关文章
相关标签/搜索