我试图制作一个批处理文件,该文件:
除了支票,我一切正常。 我见过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"
)
)
)
答案 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.
)
)