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

  1. Degree says:

    You completed various good points there. I did a search on the theme and found a good number of persons will agree with your blog.

  2. Soup Recipes says:

    Hey very cool blog!! Man .. Beautiful .. Superb .. I will bookmark your web site and take the feeds additionallyΒ‘KI am satisfied to seek out numerous helpful information right here in the post, we need work out extra techniques in this regard, thank you for sharing. . . . . .

  3. You completed several fine points there. I did a search on the matter and found the majority of folks will have the same opinion with your blog.

  4. I just want to say I am just all new to weblog and definitely savored this blog. More than likely I’m likely to bookmark your blog post . You really have terrific writings. Thanks for sharing with us your blog site.

  5. Anyways many thanks for your ideas. I value it. Please make even more articles connected to this

  6. market news says:

    I have been exploring for a little bit for any high quality articles or weblog posts on this kind of area . Exploring in Yahoo I finally stumbled upon this website. Reading this information So i‘¦m happy to express that I have an incredibly good uncanny feeling I found out exactly what I needed. I so much indubitably will make sure to don‘¦t overlook this website and give it a look regularly.

  7. Racing Games says:

    Hey very nice website!! Man .. Beautiful .. Wonderful .. I’ll bookmark your site and take the feeds additionallyΒ‘KI’m happy to seek out numerous helpful info right here in the publish, we’d like work out more strategies on this regard, thank you for sharing. . . . . .

  8. Definitely believe that which you said. Your favorite reason appeared to be on the web the easiest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they plainly do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks

  9. Valuable information. Fortunate me I discovered your website by accident, and I am surprised why this coincidence did not happened earlier! I bookmarked it.

  10. Hello very cool blog!! Man .. Excellent .. Superb .. I’ll bookmark your site and take the feeds alsoΒ‘KI’m happy to seek out numerous helpful information right here within the put up, we’d like develop extra techniques in this regard, thank you for sharing. . . . . .

  11. Pet Finder says:

    I have been exploring for a bit for any high quality articles or weblog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this web site. Reading this info So i‘¦m happy to exhibit that I have a very excellent uncanny feeling I discovered exactly what I needed. I most definitely will make sure to don‘¦t omit this site and give it a look on a continuing basis.

  12. Airlines says:

    I just wanted to type a comment in order to appreciate you for all of the magnificent steps you are giving out at this site. My considerable internet lookup has now been compensated with high-quality information to write about with my great friends. I ‘d assume that many of us readers are extremely fortunate to live in a remarkable website with so many outstanding professionals with beneficial solutions. I feel pretty blessed to have used your web pages and look forward to many more brilliant times reading here. Thanks once again for everything.

  13. tech says:

    I have been checking out a few of your articles and i must say pretty clever stuff. I will surely bookmark your site.

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>