How do I exclude specific file names from an MS DOS dir list?

18,926

Solution 1

@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

Solution 2

attrib +h autoexec.bat

should hide autoexec.bat and it should thus not appear in the list

Solution 3

DIR "*.bat" /B /P | find /v "autoexec" | for %i in (*.bat) do @echo %~ni
Share:
18,926
Bit Fracture
Author by

Bit Fracture

Updated on June 19, 2022

Comments

  • Bit Fracture
    Bit Fracture almost 2 years

    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: There is plenty of information on the internet about doing this, but it is all in the windows command processor, not MS DOS. Please understand that DOS and the Command Prompt are not the same thing.

  • Bit Fracture
    Bit Fracture about 11 years
    Little problem here, I can't find the "find.exe" utility for my ME DOS installation. I have found a plethora of incorrect versions however.
  • Hernán
    Hernán about 11 years
    Unless you have seriously damaged your OS, FIND.EXE is a MS-DOS system command en.wikipedia.org/wiki/List_of_MS-DOS_commands and it should be in the Windows System32 directory, e.g.: C:\Windows\System32 if you cannot find it, may be you should repair your OS installation
  • jeremy
    jeremy about 11 years
    Please add some information and explanation that will be beneficial to the OP and other people having similar problems. As of right now, this answer only stands as a block of code that might fix the solution to a localized issue, but not for future, similar issues.
  • Bit Fracture
    Bit Fracture about 11 years
    I am unable to test your code right now, but it looks promising! I will approve your answer as soon as I can confirm it works. Thanks!
  • Bit Fracture
    Bit Fracture about 11 years
    I must also mention, this is in DOS version Windows Millennium. There is no CMD.exe. There is only internal commands (Command.com) which I assume is similar to CMD.exe.
  • Bit Fracture
    Bit Fracture about 11 years
    Alright! This code is almost there. I need one more thing, I want to display this one page at a time, allowing the user to press any key until it has all shown. Even better would be if I could get it into columns as well as pages. I want to make sure it is all shown and in as few pages as possible.
  • Bit Fracture
    Bit Fracture about 11 years
    MS DOS doesn't have Find.exe built in. It is an external program and completely optional. As it appears, using find.exe became no longer needed because other code could do exactly what it could. Regardless, it is no longer included with the DOS ME boot disks, even those with all utilities. In more modern dos installations, loads of those old programs are nowhere to be found.
  • Bit Fracture
    Bit Fracture about 11 years
    This did not work. I still get exactly the same results as if I had not added the code. This is also visible when using the windows console to test it.
  • Bit Fracture
    Bit Fracture about 11 years
    This prints out all of the batch text for every file in the folder, along with the first file name. Definitely not working.
  • Bit Fracture
    Bit Fracture about 11 years
    This program works amazing on windows, but I get "Bad Command or File Name" on dos repeated over and over again. And syntax errors for set name=%%~Na. I think I am going to have to figure out some way to get a newer DOS platform. XP maybe. Anyway, I appreciate your help, and your solution theoretically should work. Thank you.
  • m3nda
    m3nda over 9 years
    It works for me. Just needing a way to easily exclude 1 file from processing. Like this way to exclude files, perfect for little amount of them.
  • dgo
    dgo about 9 years
    This one theoretically is the the most compact and simplest. But it can be better - you. Unfortunately for OP this won't work, because of DOS - but dir *.bat|find /v "autoexec". you don't need the the additional loop - not sure why you'd want it.