我正在尝试构建一个批处理脚本,我将使用它在运行它的文件夹中创建一个子目录。目标是创建一个(或后续使用的多个)名为“Alternate A”、“Alternate B”的文件夹、“Alternate C”等...我有一个粗略的启动脚本,可以创建带有数字计数(1,2,3,...)的文件夹,但将数字转换为基数 26 字符串,如here所示有点超出我的范围了。
这是我的工作批处理文件。
setlocal EnableDelayedExpansion
set "folderCount=1"
echo Checking existing "Alternate*" folders...
for /f "tokens=*" %%a in ('dir /b /ad "Alternate*"') do (
set /a "folderCount+=1"
echo Found folder: %%a
)
:: Check for existing folder collision
if exist "Alternate %folderCount%" (
echo Folder "Alternate %folderCount%" already exists! Skipping creation.
) else (
echo Creating folder: "Alternate %folderCount%"
md "Alternate %folderCount%"
)
endlocal
这是我所做的一次尝试,输出一个名为“Alternate digital”的文件夹。
setlocal EnableDelayedExpansion
set "folderCount=1"
echo Checking existing "Alternate*" folders...
for /f "tokens=*" %%a in ('dir /b /ad "Alternate*"') do (
set /a "folderCount+=1"
echo Found folder: %%a
)
rem Initialize an empty string for the encoded folder name
set "alphaNumeration="
rem Convert folderCount to base 26
set /a num=%folderCount%
:loop
set /a digit=num%%26
set alphaNumeration=%alphaNumeration%%A + %digit%
set /a num/=26
if not %num%==0 goto loop
rem Create the folder with the encoded name
md "Alternate %alphaNumeration%"
:: Check for existing folder collision
if exist "Alternate %alphaNumeration%" (
echo Folder "Alternate %alphaNumeration%" already exists! Skipping creation.
) else (
echo Creating folder: "Alternate %alphaNumeration%"
md "Alternate %alphaNumeration%"
)
endlocal
这是我进行的第二次尝试,这一次会生成一个名为“Alternate ABCDEFGHIJKLMNOPQRSTUVWXYZ”的文件夹
setlocal EnableDelayedExpansion
set "folderCount=0"
echo Checking existing "Alternate*" folders...
for /f "tokens=*" %%a in ('dir /b /ad "Alternate*"') do (
set /a "folderCount+=1"
echo Found folder: %%a
)
:: Convert to bijective base 26
set "digits=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "count=%folderCount%"
set "BB26="
:loop
if %count% LEQ 25 (
set "remainder=%count%%26"
set "BB26=%digits%%remainder%%BB26%"
set "count=%count%/26"
) else (
set "remainder=%count% MOD 26"
set "BB26=%digits%%remainder%%BB26%"
set "count=%count% / 26"
goto loop
)
:: Check for existing folder collision
if exist "Alternate %BB26%" (
echo Folder "Alternate %BB26%" already exists! Skipping creation.
) else (
echo Creating folder: "Alternate %BB26%"
md "Alternate %BB26%"
)
endlocal
我一直在使用人工智能来帮助我编写这些内容,但事实证明故障排除是有问题的。我理解一些逻辑并自己编写了原始工作文件,但主要依赖于谷歌,而不是真正的理解,所以任何帮助将不胜感激。
我最终简化了解决方案并取得了一些成果。我没有尝试通过数学运算进行转换,而是简单地将字母表存储为变量,并使用调用集来隔离字母表的第 N 个字符,其中 N 是计数。
setlocal EnableDelayedExpansion
set "folderCount=0"
echo Checking existing "Alternate*" folders...
for /f "tokens=*" %%a in ('dir /b /ad "Alternate*"') do (
set /a "folderCount+=1"
echo Found folder: %%a
)
:: Convert to bijective base 26
set "alphabetUpper=BCDEFGHIJKLMNOPQRSTUVWXYZ"
:loop
call set "BB26=%%alphabetUpper:~%folderCount%,1%%"
:: Check for existing folder collision
if exist "Alternate %BB26%" (
if %folderCount% LEQ 0 (
echo Maximum alternate folder count reached!
) else (
set /a "folderCount+=-1"
goto:loop
)
) else (
echo Creating folder: "Alternate %BB26%"
md "Alternate %BB26%"
)
endlocal