bat脚本 字符串查找函数和调用, 实现文件批量重命名

@echo off&setlocal enabledelayedexpansion
setlocal enabledelayedexpansion

set strName=curLens_
set strEndChar=_
set  /a startPos=86
set  /a stopPos=711

set /a startFlag=0
set /a stopFlag=0
set lastRawName=
set lastPos=
md fullsweep_raw

for  %%s in (*.raw) do ( 
	set  rawName=%%s 
	set lensPos=!rawName!
	::echo !rawName!
	call :findStringAndCutDesStr lensPos strName strEndChar
	echo lensPos=!lensPos!  startPos=!startPos!  stopPos=!stopPos! 
	
	if not !lensPos!==!lastPos! (
		if !startFlag!==1 (
			echo record last lens pos !lastPos!
			ren !lastRawName! pdaf_input_single_!lastPos!.raw
			echo last raw name = !lastRawName!
			if !stopFlag!==1 (
				goto :stopParse
			)
		)
	)
	
	if !lensPos!==!startPos! (
		echo detect start pos !lensPos! ---------------------------
		set /a startFlag = 1
	)
	if !lensPos!==!stopPos! (
		if !startFlag!==1 (
			echo detect stop pos !lensPos! ---------------------------
			set /a stopFlag = 1
		)
	)
	
	set lastRawName=!rawName!
	set lastPos=!lensPos!
)

:stopParse
move pdaf_input_single_*.raw fullsweep_raw
del *.raw

pause



:: 查找指定字符串函数,并截取关键字后,特定字符前的字符串  =====================================================
:: 入参 1 源字符串     入参2 关键字    入参3 结束字符

:findStringAndCutDesStr
::echo findStringAndCutDesStr
set sourStr=!%1!
set findStr=!%2!
set endStr=!%3!

::echo sourStr:!sourStr!
::echo findStr:!findStr!
::echo endStr:!endStr!

set str=!sourStr!
set ch1=%findStr:~0,1%
set /a count=1

:loop

if !count%! EQU 256 (
	echo ************* count is equ !count%!  end **********************
	goto :end
)
if "!str!"=="" (
	echo ************* str = null end **********************
	goto :end
)
if "!str:~0,1!" == "!ch1!" (
	:: 找到第一个字符,确认剩下的字符是否相同 (关键字最大64个字节)
	set desStr=!findStr!
	
	for /l %%c in (1 1 32) do (
		set str=!str:~1!
		set desStr=!desStr:~1!
		set tmpCh2=!desStr:~0,1!
		set /a count=!count!+1
		
		if "!desStr!"=="" (
			goto :findKeyWord
		)
		if not "!str:~0,1!" == "!tmpCh2!" (
			goto :continue
		)
	)
)

:continue
set str=!str:~1!
set /a count=!count!+1

goto loop:

:: 找到关键字后,截取关键字后,特定字符之前的字符,即目标字符
:findKeyWord
::echo ************  end count = !count!     find the key word  str=!str!     ******************
set tmpStr=!str!
set /a keyWordCount=0
set keyWord=

for /l %%d in (1 1 32) do (
::	echo tmpStr=!tmpStr!  endStr=!endStr!
	set tmpKeyWordChar=!tmpStr:~0,1!
	if "!tmpKeyWordChar!"=="!endStr!" (
		goto :KeyWordOK
	)
	set keyWord=!keyWord!!tmpKeyWordChar!
	set /a keyWordCount=!keyWordCount!+1
	set tmpStr=!tmpStr:~1!
)
echo  Key word error!!
goto :end

:KeyWordOK
::echo ************************    cut the key word : !keyWord!     ******************

::set "%~1=%2%"
::set %2=333333

:end
::echo ************  end count = !count! ******************
set %1=!keyWord!
goto:eof
:: 查找指定字符串函数  =====================================================
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值