Skip to content

Windows bat 脚本编写示例

测试示例

bash
@echo off

echo 字符串匹配 ------------------------------
setLocal EnableDelayedExpansion
set str=machine-order-service
set matchStr=order
if not "x!str:%matchStr%=!"=="x%str%" (
    echo Y
) else (
    echo N
)
endlocal

echo 字符串截取------------------------------
set ifo=abcdefghijklmnopqrstuvwxyz0123456789
echo %ifo%
echo %ifo:~-5%

echo 遍历共享盘 ------------------------------
dir \\192.168.8.120\source

echo ------------------------------
for /f "skip=1 tokens=3,4*" %%i in ('dir \\192.168.8.120\source') do (
	echo %%i ----- %%j ----- %%k
)

echo ------------------------------
for %%i in (\\192.168.8.120\source\*.zip) do (
	echo %%i
)

echo ------------------------------
for %%i in (\\192.168.8.120\source\*.zip) do (
	echo %%~ni
)

echo ------------------------------
for %%i in (\\192.168.8.120\source\*.zip) do (
	echo %%~ni%%~xi
)

echo ------------------------------
for %%i in (\\192.168.8.120\source\*.zip) do (
	echo %%~nxi
)

echo 7z 解压 zip ------------------------------
for %%i in (\\192.168.8.120\source\*.zip) do (
	cd C:\Source\code\gitee\mwj-cms\mwj-cms-framework\src\main\resources\files\7z
	7z x -aoa %%i -oC:\download\%%~ni\

)

pause

遍历共享盘实战

需求: 遍历共享盘下的所有 zip 文件(里面包含一个 _.txt 和 一个 .log 文件) 其中,.log 文件的第一行内容为 _.txt 文件的带后缀的文件名, 要求检查 .txt,.log,和 *.log 存放的第一行内容,这三个名称要完全相同。

bash
@echo off

set DIR_SHARED=\\192.168.8.120\source
set DIR_LOCAL=C:\download
set DIR_7Z=C:\Source\code\gitee\mwj-cms\mwj-cms-framework\src\main\resources\files\7z

echo   > %DIR_LOCAL%\report.txt

for %%i in (%DIR_SHARED%\*.zip) do (

	cd %DIR_7Z%
	7z x -aoa %%i -o%DIR_LOCAL%\%%~ni\

	setlocal enabledelayedexpansion

	for %%a in (dir %DIR_LOCAL%\%%~ni\*.*) do (
		if "%%~xa"==".txt" (
			set TXT_NAME=%%~na
			set TXT_FULL_NAME=%%~nxa
		)
		if "%%~xa"==".log" (
			set LOG_NAME=%%~na
			set LOG_FULL_NAME=%%~nxa

			for /f "delims=" %%m in (%%a) do (
				set LOG_CONTEXT=%%m
			)
		)
	)

	echo TXT_NAME is !TXT_NAME!
	echo LOG_NAME is !LOG_NAME!
	echo LOG_CONTEXT is !LOG_CONTEXT!

	if not "x!LOG_CONTEXT:%TXT_NAME%=!"=="x%LOG_CONTEXT%" (
		set LOG_CONTEXT_MATCH=y
	) else (
		set LOG_CONTEXT_MATCH=n
	)

	if "!TXT_NAME!"=="!LOG_NAME!" (
		if "!LOG_CONTEXT_MATCH!"=="y" (
			echo SUCCESS %%i >> %DIR_LOCAL%\report.txt
		) else (
			echo ---------------------------------- >>%DIR_LOCAL%\report.txt
			echo ERROR %%i >>%DIR_LOCAL%\report.txt
			echo TXT_NAME is !TXT_NAME! >>%DIR_LOCAL%\report.txt
			echo LOG_NAME is !LOG_NAME! >>%DIR_LOCAL%\report.txt
			echo LOG_CONTEXT is !LOG_CONTEXT! >>%DIR_LOCAL%\report.txt
		)
	) else (
		echo ^{^ >>%DIR_LOCAL%\report.txt
		echo ERROR %%i >>%DIR_LOCAL%\report.txt
		echo -- TXT_NAME is !TXT_NAME! >>%DIR_LOCAL%\report.txt
		echo -- LOG_NAME is !LOG_NAME! >>%DIR_LOCAL%\report.txt
		echo -- LOG_CONTEXT is !LOG_CONTEXT! >>%DIR_LOCAL%\report.txt
		echo ^}^ >>%DIR_LOCAL%\report.txt
	)

	endlocal
)

pause

说明:

bash
cd %DIR_7Z%
REM 7z 解压到配置的本地电脑的 DIR_LOCAL 目录下
7z x -aoa %%i -o%DIR_LOCAL%\%%~ni\

REM 开启变量延迟,并且引用时变量要用一对叹号“!!”括起来。
setlocal enabledelayedexpansion

REM 依次遍历本地已经解压的每一个 zip 解压后的文件夹,寻找需要的 .txt .log 文件的名称
REM %%a = C:\download\2021-10-21\2021-10-21.txt
REM %%a = C:\download\2021-10-21\2021-10-21.log
for %%a in (dir %DIR_LOCAL%\%%~ni\*.*) do (
      REM %%~xa:文件的扩展名;
      REM %%~na:文件的名称(不带扩展名)
      REM %%~nxa: 文件的全部名称
      if "%%~xa"==".txt" (
         set TXT_NAME=%%~na
         set TXT_FULL_NAME=%%~nxa
     )
     if "%%~xa"==".log" (
         set LOG_NAME=%%~na
         set LOG_FULL_NAME=%%~nxa

         for /f "delims=" %%m in (%%a) do (
             set LOG_CONTEXT=%%m
         )
     )
 )

endlocal

参考博客

https://blog.csdn.net/xhhjin/article/details/7373524