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

  1. bulk kratom says:

    I am always thought about this, appreciate it for posting .

  2. May Tania says:

    As a Newbie, I am constantly browsing online for articles that can help me. Thank you

  3. Most I can state is, I don’t know what to comment! Except needless to say, for the superb guidelines which are shared with this blog. I’ll believe of a thousand fun methods to read the posts on this site. I do believe I will lastly take a step along with your guidelines on areas I could never have been able to manage alone. You are so careful to permit me to be one of those to benefit from your beneficial information. Please recognize how significantly I’m thankful.

  4. that you are in point of fact a excellent webmaster. The internet site loading pace is incredible. It seems that you are doing any exclusive trick. Also, The contents are masterpiece. you’ve done a outstanding job in this subject!

  5. HOME ADVISOR says:

    You need to get involved in a contest very first of the greatest blogs over the internet. I’ll recommend this page!

  6. The Twitter application page will open. This really is excellent if you’ve got some thousand followers, but as you get far more and much more the usefulness of this tool is downgraded.

  7. Just wanna input on few common things, The internet site layout is perfect, the articles is quite superb : D.

  8. hi, your site is genuinely very good. I truly do appreciate your give very good outcomes

  9. my website says:

    Terrific paintings! That could be the type of info that are meant to be shared around the net. Shame on the seek for no longer positioning this publish higher! Come on over and consult with my web site . Thank you =)

    http://www.unsigned.com/fans/widaulfatum

  10. Click Home says:

    whoah this weblog is magnificent i like reading your posts. Keep up the great work! You recognize, a lot of persons are hunting around for this info, you can aid them greatly. |

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>