如何从MS DOS目录列表中排除特定的文件名?

Bit*_*ure 2 dos batch-file

我正在创建一个MS DOS批处理脚本,需要列出当前目录中的每个.bat文件,但不显示autoexec.bat不应由用户运行的show 或其他实用程序或系统.bat文件.

我现在有 DIR "*.bat" /B /P

这会适当地列出所有.bat文件,但它会显示autoexec.bat.我该如何从列表中排除它?同样有点重要的是,如何切断文件扩展名并显示超过7个字符的DOS限制文件?

约束:我无法使用WinME以上的DOS版本.那是我正在使用的版本.

谢谢你的帮助.

编辑: 互联网上有很多关于这样做的信息,但它都在Windows命令处理器中,而不是 MS DOS.请理解DOS和命令提示符不是一回事.

Aac*_*ini 7

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

编辑:添加了一些解释

批处理文件处理速度很慢,因此您应该使用允许批处理文件更快运行的技术.例如:

  • 尝试使用最小行/命令来实现某个结果.尽量避免外部命令(*.exe文件),如find,findstr,fc,等特别是如果他们对少量数据的工作; 请改用if命令.
  • for %%a in (*.bat)...而不是for /F %%a in ('dir /B *.bat')....第二种方法需要执行cmd.exe并将其输出存储在文件中,然后for命令才能处理其行.
  • 避免使用管道并改用重定向.管道需要执行两个cmd.exe副本才能处理管道两侧的命令.
  • 检查变量是否包含给定字符串的简单方法是尝试从变量中删除字符串:如果结果不同,则字符串存在于变量中: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)

安东尼奥