Bat操作 - ythy/blog GitHub Wiki

svnlook

svnlook是检验Subversion版本库不同方面的命令行工具,它不会对版本库有任何修改—它只是用来“看”。svnlook通常被版本库钩子使用,但是版本库管理也会发现它在诊断目的上也非常有用。

svnlook选项
svnlook的选项是全局的,就像svn和svnadmin;然而,大多数选项只会应用到一个子命令,因为svnlook的功能是(有意的)限制在一定范围的。

  • --revision (-r) 指定要进行检查的特定修订版本。
  • --transaction (-t) 指定一个希望检查的特定事物ID。

svnlook cat 导出提交的文件: svnlook cat -t $TXN $REPOS $FILE > %filePath% 注意 3个参数都要传

findstr

  • /I 指定搜索不分大小写。
  • /R 将搜索字符串作为正则表达式使用。
    findstr /r "icq msn" 123.txt
    在123.txt中查找包含有 “icq”或“msn”的行,查找的多个字符串间用空格隔格开
  • /C:string 使用指定字符串作为文字搜索字符串。

是否包含字符串

set EXISTS_FLAG=false  
echo %JAVA_OPTS%|find "Xdebug">nul&&set EXISTS_FLAG=true  
if "%EXISTS_FLAG%"=="true" (  
    echo 1  
) else (  
    echo 2  
) 

%ERRORLEVEL%

ERRORLEVEL,正常执行为0,执行出错>0
在()内返回的errorlevel 通过%errorlevel%取不到 需要!errorlevel!获取

for

FOR /F ["options"] %%i IN (file) DO command
FOR /F ["options"] %%i IN ("string") DO command
FOR /F ["options"] %%i IN ('command') DO command

主要用来处理文件和一些命令的输出结果。

  • file代表一个或多个文件
  • string 代表字符串
  • command代表命令
for /f "tokens=2,* delims= " %%i in (a.txt) do ( echo %%i %%j )

以上例子 每行以空格作为分割符,输出第2列和剩余列,可以是区间, 2-6 代表第2至6列

  • delims 用来告诉for每一行应该拿什么作为分隔符,默认的分隔符是空格和tab键
    因此 当循环后的每一行里面包含空格时,for /f %%i in (a.txt) do echo %%i只能输出空格前的字符
  • tokens 当通过delims将每一行分为更小的元素时,由它来控制要取哪一个或哪几个

set

set flag="A" flag的值为"A" 包含双引号
set flag ="A" flag空格的值为"A" flag是没有值的
"A"A 是2个不同的值
When using parenthesis the CMD shell will expand [read] all the variables at the beginning of the code block and use those values even if the variables value has just been changed. Turning on DelayedExpansion will force the shell to read variables at the start of every line.

文件操作

rd 删除文件夹

/s参数表示删除该文件夹及其下面的子目录和文件
/q参数表示,不需要确认

md 创建文件夹

if not exist "C:\VTS\" mkdir C:\VTS

字符串操作

截取字符串

%string:~%start%,%len%%

C:\Users\John>set string=Abc_123
C:\Users\John>echo %string%
Abc_123
C:\Users\John>echo %string:~4,3%
123

时间戳

set timestamp=%date:~0,10%%time%
echo %timestamp%

> nul @echo off

@echo off supresses the script from being displayed. it still displays any output generated by the script. >nul displays the script but supresses any output generated.

copy a:\*.* b:\*.*

@echo off would prevent "C:\>copy a:\*.* b:\*.*" from being shown, while >nul would suppress its output (which would be either "X files copied successfully" or an error message).

lastIndexof

@echo off
setlocal enableDelayedExpansion 

set S=/name1/name2/name3
set I=0
set L=-1
:l
if "!S:~%I%,1!"=="" goto ld
if "!S:~%I%,1!"=="/" set L=%I%
set /a I+=1
goto l
:ld
echo !S:~0,%L%!

EnableDelayedExpansion

Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command.

Variable expansion means replacing a variable (e.g. %windir%) with its value C:\WINDOWS

  • By default expansion will happen just once, before each line is executed.
  • The !delayed! expansion is performed each time the line is executed, or for each loop in a FOR looping command.

For simple commands this will make no noticable difference, but with loop commands like FOR, compound or bracketed expressions delayed expansion will allow you to always see the current value of the variable.

When delayed expansion is in effect, variables can be immediately read using !variable_name! you can still read and use %variable_name% but that will continue to show the initial value (expanded at the beginning of the line).

call

Call one batch program from another, or call a subroutine.

CALL a second batch file

The CALL command will launch a new batch file context along with any specified parameters. When the end of the second batch file is reached (or if EXIT is used), control will return to just after the initial CALL statement.
In many cases you will also want to use SETLOCAL and ENDLOCAL to keep variables in different batch files completely separate, this will avoid any potential problems if two scripts use the same variable name.

CALL a subroutine (:label)

The CALL command will pass control to the statement after the label specified along with any specified parameters. To exit the subroutine specify GOTO:eof this will transfer control to the end of the current subroutine.

A label is defined by a single colon followed by a name. This is the basis of a batch file function.

 CALL :sub_display 123
 CALL :sub_display 456
 ECHO All Done
 GOTO :eof

 :sub_display
 ECHO The result is %1
 EXIT /B

At the end of the subroutine an EXIT /B will return to the position where you used CALL (GOTO :eof can also be used for this)

Setlocal

Starts localization of environment variables in a batch file. Localization continues until a matching endlocal command is encountered or the end of the batch file is reached.

Syntax

setlocal {enableextensions | disableextensions} {enabledelayedexpansion | disabledelayedexpansion}

conclusion

  1. 参数是可选的,有没有参数都会启动变量本地化
  2. enableextensions 启用命令行扩展 包括一些类似set的命令能否使用

IF statements

IF statements do not support logical operators. You can implement a logical OR as below:

set result=false
if %a% == 1 set result=true
if %b% == 1 set result=true
if "%result%" == "true" (
    do something
)

如果用else, else左右要有空格 ) else ( 不能 )else(