检查字符串/文件夹名称是否包含特定字符正好X次

时间:2019-06-13 15:35:22

标签: windows batch-file

我试图制作一个批处理文件,该文件:

  • 查找包含特定文件(_extern.inf)的所有文件夹<-工作
  • 检查文件夹名称是否恰好包含定界符的X号。否则忽略文件(即,如果文件夹名称不是预期的格式,则忽略该文件夹)<-不起作用
  • 用定界符分隔文件夹名称,并使用子字符串之一创建将文件夹移至<-工作中的路径

除了支票,我一切正常。 我见过Batch File Count all occurrences of a character within a string,但似乎对我不起作用(%count%为空)。我猜是因为嵌套的for循环。 看来,打破不符合要求的文件的循环也不是一件容易的事……

我要检查%%~ni是否包含%expect_delims%次出现的%delim%。如果没有,请继续下一个文件夹。

@echo off
REM No trailing spaces!
set "infolder=input_path"
set "outfolder=output_path"
set "delim=_"
set "expect_delims=2"


REM Resursively look for folders with _extern.inf files
for /d /r "%infolder%" %%i in (*) do  @if exist %%i\_extern.inf (

    REM echo the folder is %%i


    REM tokens decide which part to take of the split string.
    REM "tokens=1,3" would take first and third and make %%b available

    for /F "tokens=1 delims=%delim%" %%a in ("%%~ni") do (

        REM Create project folder if doesn't exist
        if not exist "%outfolder%\%%a.raw\Data\" (
            echo creating folder %outfolder%\%%a.raw\Data
            mkdir "%outfolder%\%%a.raw\Data"
        )

        REM move folder if doesn't exist
        if exist "%outfolder%\%%a.raw\Data\%%~nxi" echo raw folder already exists!

        if not exist "%outfolder%\%%a.raw\Data\%%~nxi" (
            echo Moving "%%~fi" to "%outfolder%\%%a.raw\Data\%%~nxi"
            move "%%~fi" "%outfolder%\%%a.raw\Data\%%~nxi"
        )
    )

)

1 个答案:

答案 0 :(得分:1)

基于@aschipfl的想法,我找到了一种使其工作的方法。

跨块边界传输变量基于此:Windows batch setlocal enabledelayedexpansion call return value

@echo off

REM ****** SETTINGS ******
REM No trailing spaces!
set "infolder=input_dir"
set "outfolder=output_dir"
set "delim=_"
set "expect_delims=4"


REM SCRIPT STARTS HERE
setlocal
set /a "token_start=%expect_delims%+1"
set /a "token_end=%expect_delims%+2"


REM Resursively look for folders with _extern.inf files
for /d /r "%infolder%" %%i in (*) do  @if exist %%i\_extern.inf (

    echo ****** Start processing ******
    echo File: "%%~i"


    for /F "tokens=1,%token_start%,%token_end% delims=%delim%" %%a in ("%%~ni") do (
        REM echo the raw folder is %%i
        if "%%b" == "" (echo Filename check: Too few delimiters. File ignored.) else (
            if not "%%c" == "" (echo Filename check: Too many delimiters. File ignored.) else (

                echo Filename check: OK

                REM Create project folder if doesn't exist
                if not exist "%outfolder%\%%a.raw\Data" (
                    echo creating folder "%outfolder%\%%a.raw\Data"
                    mkdir "%outfolder%\%%a.raw\Data"
                )

                REM move raw folder if doesn't exist
                if exist "%outfolder%\%%a.raw\Data\%%~nxi" echo raw folder already exists! File ignored.

                if not exist "%outfolder%\%%a.raw\Data\%%~nxi\" (
                    echo Moving "%%~fi" to "%outfolder%\%%a.raw\Data\%%~nxi"
                    move  "%%~fi" "%outfolder%\%%a.raw\Data\%%~nxi"
                )

            )    
        )

        echo ****** End processing ******
        echo.
    )



)