Danil Mironov
Danil Mironov

Reputation: 199

Generate help files

I wanted to make help generator for all command in help in Windows cmd and write it to separate files. So you are asking /? on all commands that are on the list when you type help in cmd. Here are the main part of my code:

rem mypath - it's a folder where I put my results
rem In help all command are written by capitals letters
for /f "tokens=1 usebackq" %%i in (`help^|findstr /B /R "[QWERTYUIOPASDFGHJKLZXCVBNM][QWERTYUIOPASDFGHJKLZXCVBNM]"`) do (
  if NOT "%%i"=="GRAFTABL" (
    if NOT "%%i"=="DISKPART" (
      if NOT "%%i"=="SC" (
        help %%i > !mypath!\%%i.txt
      )
    )
  )
)

I use all sequence from [Q..M] in my Regular exp because there are some problems with just set of [A-Z] But the problem is that in my FOR and IF files - there are help for REM command. Does anyone have any idea why is it so ? To fix it I use:

FOR/? >%mypath%\FOR.txt
IF/? >%mypath%\IF.txt

But I can't understand why it is so.

Upvotes: 1

Views: 176

Answers (2)

dbenham
dbenham

Reputation: 130869

The code you posted in your question gives the correct result for me (even before I reformatted it a bit).

See Why does findstr not handle case properly (in some circumstances)? for an explanation of why [A-Z] does not work properly with FINDSTR. For an exhaustive list of known FINDSTR quirks, see What are the undocumented features and limitations of the Windows FINDSTR command?

A better way to filter out command names is to look for lines that begin with at least one non-space character, followed by any number of additional non-space characters, followed by 2 spaces.

If you want to ignore certain commands, you can simply use an additional FINDSTR with the /V option.

The solution becomes a reasonable one liner that can run from the command prompt without a batch script:

for /f %A in ('help^|findstr /rc:"^[^ ][^ ]*  "^|findstr /v "GRAFTABL DISKPART SC"') do @help %A >%A.txt

Or as code that can be plugged into your script:

for /f %%A in (
  'help^|findstr /rc:"^[^ ][^ ]*  "^|findstr /v "GRAFTABL DISKPART SC"'
) do help %%A >"!mypath!\%%A.txt"


EDIT - 2015-10-11

The first and last lines of HELP output start with the word For (mixed case), on my English machine. That word happens to be a valid command with help, so the FOR.TXT file gets created 3 times.

I presume that all languages use mixed case for the first and last line. It is not hard to refine the FINDSTR filter to exclude any line where the 2nd character is space or a lower case character:

for /f %A in ('help^|findstr /rvc:"^.[abcdefghijklmnopqrstuvwxyz ]"^|findstr /v "GRAFTABL DISKPART SC"') do @help %A >%A.txt

Upvotes: 2

Mark Tolonen
Mark Tolonen

Reputation: 177891

I couldn't reproduce your issue, but this worked correctly for me and I found a simpler regular expression:

@echo off
for /f %%i in ('help^|findstr /B /R [A-Z][^^^^o]') do (
if NOT "%%i"=="GRAFTABL" (
    if NOT "%%i"=="DISKPART" (
        if NOT "%%i"=="SC" (
            help %%i > %%i.txt
            )
        )
    )
)

Upvotes: 1

Related Questions