如何排除从MS-DOS目录列表中指定的文件名? [英] How do I exclude specific file names from an MS DOS dir list?

查看:212
本文介绍了如何排除从MS-DOS目录列表中指定的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建需要列出当前目录下的每个.bat文件的MS-DOS批处理脚本,但没有表现出 autoexec.bat文件或其他实用程序或系统.BAT文件不应该由用户运行

我目前有 DIR* .bat扩展/ B / P

这适当列出所有.bat文件,但它显示的autoexec.bat。如何将我排除在名单?也略有重要的是,我怎么能砍掉的文件扩展名,并表现出比7-DOS字符限制的文件吗?

约束:我不能够使用上面WinME的一个DOS版本。这是我使用的版本。

感谢您的帮助。

编辑:
请认识到,有很多关于做这种在互联网上的东西,但它是所有在Windows命令处理器的不可以 MS DOS。我曾经做过大量的研究。为什么我添加呢?有人向下投我的制作精良的问题,这就是原因。请明白,DOS和命令提示符是不一样的事情。


解决方案

 关闭@echo
SETLOCAL EnableDelayedExpansion
REM添加这里斜杠分隔多个名称:
集中排除= /自动执行/
对于一个%%中(* .bat)的形式做(
   如果!排除:/ %%〜NA / = EQU%排除%(
      回声%%〜娜
   )

修改补充几点说明

批处理文件的处理速度很慢,所以你应该使用的技术,它允许一个批处理文件来运行得更快。例如:


  • 尝试用最小的线/命令来实现某种结果。尽量避免外部命令(* .exe文件),如找到 FINDSTR FC 等,特别是如果他们对少量数据的工作;使用如果命令。

  • 使用为%%一中(* .bat)的形式... 而不是 FOR / F %%一个在('DIR / B * .BAT)... 。第二种方法要求在执行CMD.EXE和存储其在文件输出命令可以处理其行。

  • 避免管道和使用重定向来代替。一个管道需要的cmd.exe的两个副本的执行在管道的每一边处理命令。

  • 一个简单的方法来检查,如果一个变量包含一个给定的字符串试图删除该变量的字符串:如果结果的不同的的那么字符串存在的变量:如果变量:%字符串%=! NEQ%变量%呼应字符串是在变量

  • previous方法也可用于检查一个变量是否有值列表的人:设置列表=一二三如果列表:!%变量%= NEQ名单%%回声变量从列表中有一个值。如果列表的值可能有空格,它们必须由另一个分隔符分隔。

修改新版本中添加的答案新评论

在一个时间暂停一个页面的最简单的方法是使用更多过滤这种方式:

  theBatchFile |更多

但是,该程序必须重新排序,以便输出到它显示在列。下面的新版本实现双方的事情,所以它不需要更多过滤器;你只需要设置每页的行和列的所需数量。

 关闭@echo
SETLOCAL EnableDelayedExpansion
REM添加这里斜杠分隔多个名称:
集中排除= /自动执行/
根据需要,REM SET前两个变量旁边:
SET / A柱= 5,行数= 41,宽=(80列)/列,列= 0,行= 0
REM创建填空格对齐列
集空间=
对/ L %%一个在(1,1,宽%%)做一套空间=!空间!
集线=
对于一个%%中(* .bat)的形式做(
   如果!排除:/ %%〜NA / = EQU%排除%(
      REM如果此列小于限制...
      集/ A山坳+ = 1
      如果!山坳! LSS%列%(
         REM ......它添加到当前行
         集名称= %%〜娜%的空间%
         集!行=行!!名:〜0,宽%%!
      )其他(
         REM ...显示当前行并重置
         集名称= %%〜娜
         回声线!!名称:!〜0%宽%!
         集线=
         集/一个山坳= 0,行+ = 1
         REM如果该行是等于限制...
         如果!行! EQU%列%(
            REM ......做一个暂停和重新排
            暂停
            集行= 0
         )
      )
   )

REM显示最后一行,如果有的话
如果定义了线路回声%行%

安东尼奥

I am creating an MS DOS batch script that needs to list every .bat file in the current directory, but not show autoexec.bat or other utilities or systems .bat files that shouldn't be run by the user.

I currently have DIR "*.bat" /B /P

This lists all .bat files appropriately, but it shows autoexec.bat. How would I exclude that from the list? Also slightly important, how could I chop off the file extensions and show more than the 7-characters DOS limits files to?

Constraints: I am not able to use a DOS version above WinME. That is the version I am using.

Thanks for any help.

EDIT: Please realize that there is plenty about doing this sort of thing on the internet, but it is all in the windows command processor, not MS DOS. I have done plenty of research. Why did I add this? Someone down-voted my well-made question, that's why. Please do understand that DOS and the Command Prompt are not the same thing.

解决方案

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

EDIT: Some explanations added

Batch file processing is slow, so you should use techniques that allows a Batch file to run faster. For example:

  • Try to use the minimum lines/commands to achieve a certain result. Try to avoid external commands (*.exe files) like find, findstr, fc, etc. specially if they work on small amounts of data; use if command instead.
  • Use for %%a in (*.bat)... instead of for /F %%a in ('dir /B *.bat').... The second method requires to execute cmd.exe and store its output in a file before for command can process its lines.
  • Avoid pipes and use redirections instead. A pipe require the execution of two copies of cmd.exe to process the command at each side of the pipe.
  • A simple way to check if a variable contain a given string is trying to delete the string from the variable: if the result is different then the string exists in the variable: if "!variable:%string%=!" neq "%variable%" echo The string is in the variable.
  • Previous method may also be used to check if a variable have anyone of a list of values: set list=one two three, if "!list:%variable%=!" neq "%list%" echo The variable have one value from the list. If the values of the list may have spaces, they must be separated by another delimiter.

EDIT: New version added as answer to new comments

The easiest way to pause one page at a time is to use more filter this way:

theBatchFile | more

However, the program must reorder the output in order to show it in columns. The new version below achieve both things, so it does not require more filter; you just need to set the desired number of columns and rows per page.

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

Antonio

这篇关于如何排除从MS-DOS目录列表中指定的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆