- 1 year ago
Windows batch: Check for highest folder prefix and create new project folder
Comment
1 / 1
To check for the highest folder prefix in a directory and create a new project folder with the next sequential prefix, you can use a batch script with the following steps:
- Set the root directory where your project folders are located using the
set
command:
arduinoset "root=C:\Projects"
- Change to the root directory using the
cd
command:
bashcd %root%
- Use the
dir /ad /b
command to list all directories in the root directory (excluding files) and thefindstr
command to filter for directories that have a prefix matching a certain pattern (e.g., "Project-"):
bashfor /f "delims=" %%a in ('dir /ad /b ^| findstr /b "Project-"') do (
set "lastFolder=%%a"
)
- Extract the number from the highest numbered folder prefix using the
set
andsubstr
commands:
bashset "prefix=%lastFolder:~8%"
set /a "number=prefix + 1"
- Create the new project folder using the
md
command with the new prefix:
arduinoset "newFolder=Project-%number%"
md %newFolder%
Here's the full batch script:
bash@echo off
set "root=C:\Projects"
cd %root%
for /f "delims=" %%a in ('dir /ad /b ^| findstr /b "Project-"') do (
set "lastFolder=%%a"
)
set "prefix=%lastFolder:~8%"
set /a "number=prefix + 1"
set "newFolder=Project-%number%"
md %newFolder%
echo Created new project folder: %newFolder%
This script assumes that your project folders are named with a prefix followed by a hyphen, such as "Project-1", "Project-2", etc. You can adjust the pattern used by findstr
to match your folder naming convention. The script will create a new folder with the next sequential prefix, such as "Project-3" if the highest numbered folder is "Project-2".