批处理脚本修改需求:扩展字符集并实现3列文件夹菜单
批处理文件夹多列导航工具实现方案
以下是满足所有需求的修改后代码:
@echo off setlocal enabledelayedexpansion rem 配置项:字符集、列条目数、文件名截断长度 set "char=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" set "col1=21" set "col2=21" set "col3=20" set /A pageSize=col1+col2+col3 set "truncLen=20" rem 加载当前目录下的文件夹列表 set "numNames=0" for /D %%a in (*) do ( set /A numNames+=1 rem 截断过长的文件夹名 set "truncName=%%a" if "!truncName:~%truncLen%!" neq "" set "truncName=!truncName:~0,%truncLen%-3!..." set "name[!numNames!]=!truncName!" ) rem 计算总页数 if %numNames% equ 0 ( echo 当前目录无文件夹 pause >nul exit /b ) set /A numPages=(numNames-1)/pageSize+1 set "start=1" :ShowPage rem 计算当前页的起始、结束索引及页码 set /A page=(start-1)/pageSize+1, end=start+pageSize-1 if %end% gtr %numNames% set end=%numNames% cls echo 第 %page%/%numPages% 页 当前路径:%CD% echo/ rem 生成三列布局的显示内容 set "idx=!start!" rem 第一列(21项) for /L %%c in (1,1,%col1%) do ( if !idx! gtr !end! goto :ShowCol2 set "charIdx=!idx!-!start!+1" call set "currChar=%%char:~!charIdx!,1%%" echo !currChar! - !name[!idx!]!^| set /A idx+=1 ) :ShowCol2 rem 第二列(21项) for /L %%c in (1,1,%col2%) do ( if !idx! gtr !end! goto :ShowCol3 set "charIdx=!idx!-!start!+1" call set "currChar=%%char:~!charIdx!,1%%" echo !currChar! - !name[!idx!]!^| set /A idx+=1 ) :ShowCol3 rem 第三列(20项) for /L %%c in (1,1,%col3%) do ( if !idx! gtr !end! goto :EndShow set "charIdx=!idx!-!start!+1" call set "currChar=%%char:~!charIdx!,1%%" echo !currChar! - !name[!idx!]! set /A idx+=1 ) :EndShow echo/ rem 构建选择提示信息 set "mssg=(B-返回上级, " if %end% lss %numNames% set "mssg=!mssg!N-下一页, " if %start% gtr 1 set "mssg=!mssg!P-上一页, " rem 移除最后一个逗号和空格 set "mssg=!mssg:~0,-2!): " :GetOption rem 生成choice命令的字符集:当前页可用字符 + B/N/P set "choiceChars=" for /L %%i in (!start!,1,!end!) do ( set "charIdx=%%i-!start!+1" call set "choiceChars=!choiceChars!%%char:~!charIdx!,1%%" ) set "choiceChars=!choiceChars!BNP" choice /C !choiceChars! /N /M "请选择操作%mssg%" rem 处理选择结果 set "selected=!errorlevel!" set /A selectedIdx=start+selected-1 rem 判断选择的是功能键还是文件夹选项 if !selected! gtr !end!-!start!+1 ( rem 功能键处理:B=返回上级,N=下一页,P=上一页 set "funcChar=!char:~!selected!-1,1!" if /I "!funcChar!" equ "B" ( cd .. goto :ShowPage ) else if /I "!funcChar!" equ "N" ( if %end% lss %numNames% set /A start=end+1 goto :ShowPage ) else if /I "!funcChar!" equ "P" ( if %start% gtr 1 set /A start=start-pageSize goto :ShowPage ) ) else ( rem 进入选中的文件夹 if !selectedIdx! leq !numNames! ( cd "!name[!selectedIdx!]!" rem 重新加载当前目录的文件夹列表 set "numNames=0" for /D %%a in (*) do ( set /A numNames+=1 set "truncName=%%a" if "!truncName:~%truncLen%!" neq "" set "truncName=!truncName:~0,%truncLen%-3!..." set "name[!numNames!]=!truncName!" ) set /A numPages=(numNames-1)/pageSize+1 set "start=1" goto :ShowPage ) ) goto :GetOption endlocal
关键修改说明
- 扩展字符集:将
char变量更新为包含数字、大小写字母的完整序列,支持最多62个文件夹条目选择,覆盖单页最大容量。 - 三列布局实现:定义三列条目数分别为21、21、20,通过三次循环分列渲染内容,每列条目对齐显示,保证布局规整。
- 文件名截断:添加长度判断逻辑,超过指定长度(默认20字符)的文件夹名将被截断并追加
...,避免长名称打乱列布局。 - 全程键盘操作:基于
choice命令实现纯键盘交互,支持选择文件夹进入、上/下翻页、返回上级目录,全程无需调用资源管理器,彻底避免键鼠切换。
内容的提问来源于stack exchange,提问作者fortissimo
相关产品推荐
相关产品推荐

