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



在过去,我通过搜索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

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

@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作为传入字符串的前缀,然后去掉每个数字。如果结果是字符串不为空,则说明字符串中还有其他字符,因此忽略它。

否则,如果字符串的值为>=当前下一值,则将下一值设置为比字符串值大1的值。

9在字符串前加上前缀,以便在最后一次替换之前,字符串永远不会变为空。

所以多亏了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

相关内容

  • 没有找到相关文章

最新更新