我正在创建一个MS DOS批处理脚本,需要列出当前目录中的每个.bat文件,但不显示autoexec.bat不应由用户运行的show 或其他实用程序或系统.bat文件.
我现在有 DIR "*.bat" /B /P
这会适当地列出所有.bat文件,但它会显示autoexec.bat.我该如何从列表中排除它?同样有点重要的是,如何切断文件扩展名并显示超过7个字符的DOS限制文件?
约束:我无法使用WinME以上的DOS版本.那是我正在使用的版本.
谢谢你的帮助.
编辑: 互联网上有很多关于这样做的信息,但它都在Windows命令处理器中,而不是 MS DOS.请理解DOS和命令提示符不是一回事.
@echo off
setlocal EnableDelayedExpansion
rem Add more names separated with slashes here:
set exclude=/autoexec/
for %%a in (*.bat) do (
if "!exclude:/%%~Na/=!" equ "%exclude%" (
echo %%~Na
)
)
Run Code Online (Sandbox Code Playgroud)
编辑:添加了一些解释
批处理文件处理速度很慢,因此您应该使用允许批处理文件更快运行的技术.例如:
find,findstr,fc,等特别是如果他们对少量数据的工作; 请改用if命令.for %%a in (*.bat)...而不是for /F %%a in ('dir /B *.bat')....第二种方法需要执行cmd.exe并将其输出存储在文件中,然后for命令才能处理其行.if "!variable:%string%=!" neq "%variable%" echo The string is in the variable.set list=one two three,if "!list:%variable%=!" neq "%list%" echo The variable have one value from the list.如果列表的值可能包含空格,则它们必须用另一个分隔符分隔.编辑:添加新版本作为新评论的答案
一次暂停一个页面的最简单方法是以more这种方式使用过滤器:
theBatchFile | more
Run Code Online (Sandbox Code Playgroud)
但是,程序必须重新排序输出才能在列中显示它.下面的新版本实现了这两个目标,因此它不需要more过滤; 您只需要为每页设置所需的列数和行数.
@echo off
setlocal EnableDelayedExpansion
rem Add more names separated with slashes here:
set exclude=/autoexec/
rem Set the first two next variables as desired:
set /A columns=5, rows=41, wide=(80-columns)/columns, col=0, row=0
rem Create filling spaces to align columns
set spaces=
for /L %%a in (1,1,%wide%) do set spaces= !spaces!
set line=
for %%a in (*.bat) do (
if "!exclude:/%%~Na/=!" equ "%exclude%" (
rem If this column is less than the limit...
set /A col+=1
if !col! lss %columns% (
rem ... add it to current line
set name=%%~Na%spaces%
set "line=!line!!name:~0,%wide%! "
) else (
rem ... show current line and reset it
set name=%%~Na
echo !line!!name:~0,%wide%!
set line=
set /a col=0, row+=1
rem If this row is equal to the limit...
if !row! equ %rows% (
rem ...do a pause and reset row
pause
set row=0
)
)
)
)
rem Show last line, if any
if defined line echo %line%
Run Code Online (Sandbox Code Playgroud)
安东尼奥
| 归档时间: |
|
| 查看次数: |
16206 次 |
| 最近记录: |