Windows Command Line Batch Filespec Tutorial

Windows Command Line Batch Filespec Tutorial

Windows Command Line Batch Filespec Tutorial

To get small desktop jobs with ascii files done on Windows, you may laugh at the suggestion to do it in DOS (or Command Line), but the fact is that you can do an amazing amount of the things you need to do, using this method, and you, consequently, do not need any compilers to create C programs, for instance. If it was me, and the small job involved files, and DOS Batch just “didn’t cut it” with its interpretive approach, what I’d use is Digital Mars C for that type of job, but let’s go over what we want to do with today’s DOS Batch file job.

We have some XML files all in the one folder that we want to count the number of times a particular string is found. There’s one parameter. We allow for two others, being two XML element types to further filter by. The last parameter, designed for use with a single command having several parameters, is a filename or file specification (commonly know as a filespec) or a space separated list of filenames.

We decided that the rules about parameters (or arguments) should be …

  1. Optionally parameter 1 is that first XML element type to filter by …
  2. Optionally next parameter is that other XML element type to filter by …
  3. Next parameter is the string to search for …
  4. Last parameters are a filename or file specification (commonly know as a filespec) or a space separated list of filenames

… can you see why we use this order? Well, we can “hang our hat” on the fact that filenames should contain a dot (or period), and so we can work out the rest backwards from that, about what the user entered. You’ll find with DOS Batch logic, the parameter design and coding takes a lot of the time and thought.

Coding points of interest in our DOS Batch file you could call findthis.bat are …


  • rem for /F for the case of a string
    @for /F "delims=. tokens=1*" %%x in ("%2") do @set xdot=%%y

  • rem for /F for the case of a file(spec)
    @for /f "delims=\n tokens=1*" %%x in (n%1.jnk) do (@for %%i in (%%x %%y) do echo %%i %%j) >> ~%1~

  • rem if == example
    if JUNK%xdot%==JUNK goto domore1

  • rem if errorlevel checking
    if errorlevel 1 goto aster

  • rem if [not] exist (file) checking
    if not exist n%1.jnk goto aster

  • rem main action is to find our designated search string in filename
    find /c "%fstr%" ~%1~

  • rem example of a label than can be gone to from a goto statement
    :top

… to result in a Windows Command Line scenario where a command like …


findthis.bat section chapter divid *.xml

… as shown in today’s tutorial picture will look for XML records containing section or chapter and count the total findings for string “divid” there …

… and so, as you can see, DOS Batch syntax is not the friendliest code, but can be very effective, and will work the same way, if not too complex, on any “flavour” of Windows allowing Command Line windows. Copying “findthis.bat” to a directory on your PATH …


echo %PATH%

… can mean you can be in any directory using this functionality with command further above.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , . Bookmark the permalink.

13 Responses to Windows Command Line Batch Filespec Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *