Three patterns to use “if else” statement in dos/batch scripts

  • Pattern #1
@echo off
set var=b
if "%var%" EQU "a" (echo 1) else if "%var%" EQU "b" (  echo 2) else if "%var%" EQU "c" (  echo 3)
  • Pattern #2
@echo off
set var=b
if "%var%" EQU "a" (
  echo 1
) else if "%var%" EQU "b" (
  echo 2
) else if "%var%" EQU "c" (
  echo 3
)
  • Pattern #3
@echo off
set var=b
if "%var%" EQU "a" (
  echo 1
) ^
else if "%var%" EQU "b" (
  echo 2
) ^
else if "%var%" EQU "c" (
  echo 3
)

Two ways to retrieve process id while startup a process via Windows command line

一个特殊的机缘, 需要通过cmd.exe启动一个进程, 并且获取该启动后进程的Process ID, 搜罗到两种方法:

1. 通过wmic process call create

  • wmic如果创建进程成功,将返回一个ReturnValue为0类JSON结构的输出, 从中获取ProcessId:
C:\app>cmd.exe /C wmic process call create "c:\app\cluster\GatewayServer.exe start -id 6", "c:\app"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ProcessId = 19420;
        ReturnValue = 0;
};
  • wmic如果创建进程失败, 将返回一个ReturnValue非0的输出:
C:\app>wmic process call create "cluster\GatewayServer.exe start -id 6", "c:\app"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ReturnValue = 9;
};

但wmic有一个很大的问题: 不使用当前用户上下文和系统的环境变量.

2. 通过powershell的Start-Process启动进程, 然后取Start-Process返回对像的Id属于得到Process ID:

C:\app>cmd.exe /C powershell -Command "try{$app = Start-Process -PassThru -FilePath \"cluster\GatewayServer.exe\" -WorkingDirectory \"C:\app\" -ArgumentList \"start -id 5\";echo $app.Id} catch {throw}"

Close TCP and UDP ports via windows command line

The CurrPorts tool from Nirsoft can easily close a TCP/UDP connection without kill the Process via Windows command line

cports.exe /close <Local Address> <Local Port> <Remote Address> <Remote Port>

Example:

cports.exe /close 172.24.3.102 51512 172.25.1.206 8007

Download: CurrPorts
Refer: https://superuser.com/questions/384758/how-to-kill-a-particular-tcp-connection-in-windows/384761#384761

DOS中通过chcp修改console code page

UTF-8格式的日志文件, DOS中通过type命令查看时显示乱码:

C:\app\Manager\Log\RootManagerServer>type Root_20200527_135730_Trace.html
1590559051 0 0 Trace 0 0 Bind Port:Listen:8800<br>                                                                                                                                                                                                                
1590559051 0 0 Trace 0 0 鏈嶅姟鍣ㄧ洃鍚鍙?8800<br>                                                                                                                                                                                                              
1590559051 0 0 Trace 0 0 Bind Port:Listen:9800<br>                                                                                                                                                                                                                
1590559051 0 0 Trace 0 0 瀹㈡埛绔洃鍚鍙?9800<br>

通过chcp命令修改code page为65001后显示正常:

C:\app\Manager\Log\RootManagerServer>type Root_20200527_135730_Trace.html
1590559051 0 0 Trace 0 0 Bind Port:Listen:8800<br>                                                                                                                                                                                                                
1590559051 0 0 Trace 0 0 服务器监听端口:8800<br>                                                                                                                                                                                                                  
1590559051 0 0 Trace 0 0 Bind Port:Listen:9800<br>                                                                                                                                                                                                                
1590559051 0 0 Trace 0 0 客户端监听端口:9800<br>

 

DOS命令行中支持的Code Page:

代码页 国家/地区或语言
437 United States
850 多语言 (拉丁文我)
852 西里尔语 (俄语)
855 西里尔语 (俄语)
857 土耳其语
860 葡萄牙语
861 冰岛语
863 加拿大法语
865 北欧
866 俄语
869 现代希腊语
65001 UTF-8

chcp文档: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/chcp