Windows批次:检查最高文件夹前缀并创建新项目文件夹

hgtggwj0  于 2023-01-21  发布在  Windows
关注(0)|答案(2)|浏览(173)

在过去,我通过搜索StackOverflow的讨论历史找到了很多解决方案,然而,我被这个批处理文件卡住了,(因为Windows脚本不是我通常做的事情)。
当前目录包含许多子文件夹(实际上有数千个),请参见以下示例:

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),

因此,在上例中,下一个项目应具有前缀128
我希望脚本检查所有现有文件夹的数字前缀。这些前缀可以是199999,即最多五个数字字符,并通过字符-与名称的其余部分分隔。(某些文件夹可能没有任何数字前缀,应忽略这些前缀)
然后,脚本应该创建一个新文件夹,并将下一个"可用"前缀作为项目编号。
下面是我的尝试:

@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

我的前缀"检测"方法似乎不起作用,因此正确的创建也失败了。

t3psigkw

t3psigkw1#

@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

注意,这个例程只计算下一个前缀。
列出目录,抓取-前面的名称部分,用引号引起来并传递给:process
:process中,用9作为传递字符串的前缀,然后删除每个数字。如果结果是字符串不为空,则字符串中存在其他字符,因此忽略它。
否则,如果字符串的值〉= current next-value,则将next-value设置为比字符串的值大1。
9作为字符串的前缀,以便字符串在最后一次替换之前不会变为空。

qxsslcnc

qxsslcnc2#

因此,感谢Magoo我得到了我的批处理工作。我张贴下面的编辑代码,也许有人用它。再次感谢!

@echo off

rem Codepage Einstellung auf ANSI für deutsche Umlaute
chcp 1252>nul

echo.
echo.
echo M&A Filestruktur Generator
echo Dr. Hans-Friedrich Fuge, Stand 10/01/2023
echo.
echo.


setlocal enabledelayedexpansion

rem Find the next "free" Prefix as new project number

SET /a nextprefix=1

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

GOTO :Neuprojekt

: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

:Neuprojekt
rem Ask for new project name
set /p projectname=Wie lautet das neue Projekt (idealerweise Format PROJEKTNAME-UNTERNEHMENSNAME):

rem Reject input if empty
if "%projectname%" == "" (
echo.
echo Ungültige Eingabe, es muß ein Projektname eingegeben werden.
echo.
echo.
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.
echo.
echo Der Projektname exitiert bereits als %%F.
pause
exit
)
)
)

rem Create new folder with incremented Prefix and project name
md %nextprefix%-"%projectname%"
cd %nextprefix%-"%projectname%"

rem Erstellung der Unterordner Struktur

md Prozess
md Offers
md "Due diligence"
md Financials
md Legal
md alt

cd Prozess
md NDA
md IM
md MP
md "Process letter"
cd..

cd Offers
md "Indicative offer"
md "Binding offer"
cd Indicative offer
md alt
cd..
cd Binding offer
md alt
cd..
cd..

cd Due diligence
md Templates
md Research
md "Data room"
cd..

cd Financials
md Dealsheet
md Model
cd Model
md alt
cd..
md "Other financials"
cd..

cd legal
md SPA
cd SPA
md alt
cd..
md "Other legal"
cd..

echo.
echo Es wurde eine neue Projektstruktur unter %nextprefix%-"%projectname%" erstellt.
echo.
endlocal
pause

相关问题