批处理:测试变量中的子字符串

时间:2015-05-05 00:22:16

标签: batch-file

我找到了一种功能方法来识别变量中的子串(参见下面的===========)。执行简单的InStr()类型函数似乎是一种相当麻烦的方式。

有更好的方法吗?

@echo off
setlocal enabledelayedexpansion

::Metadata Creation ::
set File1=filelist.txt
set File2=filelist2.txt

cd\users\mark\downloads\media
::List current \Media files
dir /s/b *.mp4 *.mkv *.m4v > %File1%

::Write metadata
for /f "tokens=*" %%a in (%File1%) do ( 

  :: Full path, no filename
  set myPath=%%~dpa

  :: Parent folder
  for %%b in ("!myPath:~0,-1!") do set "myParent=%%~Nb"

rem ==InStr() type function============

  :: If myParent variable contains the substring, replace
  Set stemp=!myParent!
  Set sstr=photo
  Set pos=0
  :loop
  Set /a pos+=1
  echo !stemp!|FINDSTR /i /c:!sstr! >NUL
  if errorlevel 1 (
     Set stemp=!stemp:~1!
     if defined !stemp! GOTO loop
     Set pos=0
  )
  if !pos! NEQ 0 (set myParent=Photo_Podcast)

rem =============================

0 个答案:

没有答案