Windows Command Line C Filespec Tutorial

Windows Command Line C Filespec Tutorial

Windows Command Line C Filespec Tutorial

You may recall from our previous Windows Command Line Batch Filespec Tutorial as shown below a solution we developed using a Batch file, but we did say at the time …

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.

… well … the DOS Batch approach was okay for summaries but not to list details about the finds, so we set out to do the C findthis.exe approach with the Digital Mars C compiler, featuring some C programming code you could call findthis.c … and compiled using …


dmc findthis.c

… using the wonderful qsort method to sort the results shown.

And we made some small amendments to findthis.bat that changed this way to allow for findthis.exe incorporation and supervision, allowing for wildcard filespec usage via Command Line calls to the Batch file.


Previous relevant Windows Command Line Batch Filespec Tutorial is shown below.

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.


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

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

10 Responses to Windows Command Line C Filespec Tutorial

Leave a Reply

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