This small batch file demonstrates how to identify subdirectories with string Part anywhere within directory name.
@echo off
setlocal enabledelayedexpansion
cd /d "C:\testfolder\test\test"
for /f "delims=" %%a in ('dir /b /ad /o-d ') do (
set "DirName=%%a"
if "!DirName:Part=!" == "!DirName!" (
echo Directory "%%a" does not contain string "Part".
) else (
echo Directory "%%a" contains string "Part".
)
)
endlocal
echo.
pause
!DirName:Part=!
creates a string from value of environment variable DirName where the string Part is replaced by an empty string. This string is compared with unmodified value of environment variable DirName. If both strings are identical, the directory name does not contain Part.
Replace the 2 echo
commands with whatever you need for your task.
This second example code searches in directory C:\testfolder\test\test
for any subdirectory containing the string Part in directory name in order from newest to oldest directory. The loop is exit immediately on first subdirectory with Part in name found. The environment variable DirName contains in this case the name of this subdirectory.
@echo off
cls
setlocal enabledelayedexpansion
cd /d "C:\testfolder\test\test"
for /f "delims=" %%a in ('dir /b /ad /o-d ') do (
set "DirName=%%a"
if not "!DirName:Part=!" == "!DirName!" goto PartDirFound
)
DirName=
endlocal
echo No subdirectory contains "Part" in name.
echo.
pause
rem Exit this batch file as nothing do.
goto :EOF
:PartDirFound
rem Insert your code here instead of commands echo and pause.
echo "!DirName!" contains the string "Part" in name.
echo.
pause
DirName=
endlocal
But match faster would be following batch with same execution behavior:
@echo off
cd /d "C:\testfolder\test\test"
for /f "delims=" %%a in ('dir *part* /b /ad /o-d ') do (
set "DirName=%%a"
goto PartDirFound
)
cls
echo No subdirectory contains "Part" in name.
echo.
pause
rem Exit this batch file as nothing do.
goto :EOF
:PartDirFound
rem Insert your code here instead of commands echo and pause.
echo "%DirName%" contains the string "Part" in name.
echo.
pause
DirName=