Home > database >  Windows batch: Check for highest folder prefix and create new project folder
Windows batch: Check for highest folder prefix and create new project folder

Time:01-19

In the past I found plenty of solutions just by searching StackOverflow's discussion history. However, I am stuck with this batch file, (as Windows scripting is not something I generally do).

The current directory contains many subfolders (in reality many thousands), see this example:

18.01.2023  10:05    <DIR>          .
18.01.2023  08:49    <DIR>          ..
18.01.2023  10:02    <DIR>          1-Lion
18.01.2023  10:02    <DIR>          2-Fox
18.01.2023  10:03    <DIR>          126-Orion (prior folders deleted)
18.01.2023  10:03    <DIR>          127-Future
18.01.2023  10:05    <DIR>          Random folder without prefix
18.01.2023  10:01               708 test.bat
               1 Datei(en),            708 Bytes
               7 Verzeichnis(se), 

The next project hence shall have the prefix 128, in the above example.

I want the script to check all existing folders for the numeric prefixes. Those prefixes can be 1 to 99999, i.e. up to five numeric characters and is separated from the rest of the name by the character -. (Some folders might not have any numeric prefix, those should be ignored).

The script should then create a new folder with the next "available" prefix as project number.

Here is what I tried:

@echo off
setlocal enabledelayedexpansion

rem Set initial value for MaxPrefix variable
set MaxPrefix=0

rem Check all existing folders in current directory
for /D %%F in (*) do (
    rem Check if folder name has numeric prefix
    for /F "tokens=1 delims=-" %%P in ("%%F") do (
        rem Check if prefix is higher than current MaxPrefix value
        if %%P GTR %MaxPrefix% set MaxPrefix=%%P
    )
)

rem Ask for new project name
set /p projectname=Enter new project name:

rem Reject input if empty
if "%projectname%" == "" (
    echo Invalid input, project name cannot be empty.
    pause
    exit
)

rem Check if project name already exists as folder label
for /D %%F in (*) do (
    for /F "tokens=2 delims=-" %%P in ("%%F") do (
        if /I "%%P" == "%projectname%" (
            echo Project already exists with project number %MaxPrefix%.
            pause
            exit
        )
    )
)

rem Create new folder with incremented MaxPrefix and project name
set /a newprefix=%MaxPrefix%   1
md %newprefix%-%projectname%

echo New project created with project number %newprefix%.
pause

My approach for the prefix "detection" does not seem to work, hence also the correct creation is failing, too.

CodePudding user response:

@ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

SET /a nextprefix=1

FOR /f "tokens=1delims=-" %%e IN (
 'dir /b /ad "%sourcedir%\*" '
 ) DO CALL :process "%%e"

ECHO Next prefix is %nextprefix%

GOTO :EOF

:process
SET "$=9%~1"
FOR /l %%z IN (0,1,9) DO CALL SET "$=%%$:%%z=%%"
IF DEFINED $ GOTO :eof
IF %~1 geq %nextprefix% SET /a nextprefix=%~1 1
GOTO :eof

Note that this routine only calculates the next prefix.

List the directories, grab the part of the name before the -, quote it and pass to :process.

Within :process, prefix the passed string with 9, then remove each of the digits. If the result is that the string is not empty, then there are other characters in the string, so ignore it.

Otherwise, if the string's value is >= current next-value, set next-value to 1 more than the value of the string.

9 is prefixed to the string in order that the string never becomes empty until the very last substitution.

  • Related