I am trying to make a batch files that:
- finds all folders that contain a specific file (_extern.inf) <- WORKING
- Check that the folder name contains exactly X number of a delimiter. Otherwise ignore file (i.e. if the folder name is not the expected format ignore the folder) <-- NOT WORKING
- Splits the folder name by a delimiter and uses one of the substrings to make the path to move the folder to <- WORKING
I have everything working except for the check. I have seen Batch File Count all occurrences of a character within a string but it doesn't seem to work for me (%count% is empty). I guess because of the nested for loop. It also seems that breaking the loop for files that don't conform is not trivial...
It is %%~ni
that I would like to check if contains %expect_delims%
occurrences of %delim%
. If not continue to next folder.
@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"
)
)
)