{"id":20848,"date":"2016-03-27T03:01:56","date_gmt":"2016-03-26T17:01:56","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=20848"},"modified":"2016-03-29T11:57:43","modified_gmt":"2016-03-29T01:57:43","slug":"windows-command-line-batch-filespec-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/windows-command-line-batch-filespec-tutorial\/","title":{"rendered":"Windows Command Line Batch Filespec Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Windows\/findthis.jpg\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"Windows Command Line Batch Filespec Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Windows\/findthis.jpg\" title=\"Windows Command Line Batch Filespec Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Windows Command Line Batch Filespec Tutorial<\/p><\/div>\n<p>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 &#8220;didn&#8217;t cut it&#8221; with its <a target=_blank title='Interpreted language information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Interpreted_language'>interpretive<\/a> approach, what I&#8217;d use is <a target=_blank title='Digital Mars' href='http:\/\/www.digitalmars.com\/'>Digital Mars<\/a> C for that type of job, but let&#8217;s go over what we want to do with today&#8217;s DOS Batch file job.<\/p>\n<p>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&#8217;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.<\/p>\n<p>We decided that the rules about parameters (or arguments) should be &#8230; <\/p>\n<ol>\n<li>Optionally parameter 1 is that first XML element type to filter by &#8230;<\/li>\n<li>Optionally next parameter is that other XML element type to filter by &#8230;<\/li>\n<li>Next parameter is the string to search for &#8230;<\/li>\n<li>Last parameters are a filename or file specification (commonly know as a filespec) or a space separated list of filenames<\/li>\n<\/ol>\n<p> &#8230; can you see why we use this order?  Well, we can &#8220;hang our hat&#8221; 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&#8217;ll find with DOS Batch logic, the parameter design and coding takes a lot of the time and thought.<\/p>\n<p>Coding points of interest in our DOS Batch file you could call <a target=_blank title='findthis.bat' href='http:\/\/www.rjmprogramming.com.au\/Windows\/findthis.bat_GETME'>findthis.bat<\/a> are &#8230;<\/p>\n<ul>\n<li>\n<code><br \/>\nrem <i>for \/F <\/i> for the case of a string<br \/>\n@<a target=_blank title='for information from Computerhope' href='http:\/\/www.computerhope.com\/forhlp.htm'>for<\/a> \/F \"delims=. tokens=1*\" %%x in (\"%2\") do @set xdot=%%y<br \/>\n<\/code>\n<\/li>\n<li>\n<code><br \/>\nrem <i>for \/F <\/i> for the case of a file(spec)<br \/>\n@for \/f \"delims=\\n tokens=1*\" %%x in (n%1.jnk) do (@for %%i  in (%%x %%y) do echo %%i %%j) >> ~%1~<br \/>\n<\/code>\n<\/li>\n<li>\n<code><br \/>\nrem if == example<br \/>\n<a target=_blank title='if information from Computerhope' href='http:\/\/www.computerhope.com\/if.htm'>if<\/a> JUNK%xdot%==JUNK goto domore1<br \/>\n<\/code>\n<\/li>\n<li>\n<code><br \/>\nrem if errorlevel checking<br \/>\nif errorlevel 1 goto aster<br \/>\n<\/code>\n<\/li>\n<li>\n<code><br \/>\nrem if [not] exist (file) checking<br \/>\nif not exist n%1.jnk goto aster<br \/>\n<\/code>\n<\/li>\n<li>\n<code><br \/>\nrem main action is to <i>find<\/i> our designated search string in filename<br \/>\n<a target=_blank title='find information from Computerhope' href='http:\/\/www.computerhope.com\/findhlp.htm'>find<\/a> \/c \"%fstr%\" ~%1~<br \/>\n<\/code>\n<\/li>\n<li>\n<code><br \/>\nrem example of a label than can be gone to from a <i>goto<\/i> statement<br \/>\n:top<br \/>\n<\/code>\n<\/li>\n<\/ul>\n<p> &#8230; to result in a Windows Command Line scenario where a command like &#8230;<\/p>\n<p><code><br \/>\nfindthis.bat section chapter divid *.xml<br \/>\n<\/code><\/p>\n<p> &#8230; as shown in today&#8217;s tutorial picture will look for XML records containing <i>section<\/i> or <i>chapter<\/i> and count the total findings for string <i>&#8220;divid&#8221;<\/i> there &#8230;<\/p>\n<p> &#8230; 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 &#8220;flavour&#8221; of Windows allowing Command Line windows.  Copying &#8220;findthis.bat&#8221; to a directory on your PATH &#8230;<\/p>\n<p><code><br \/>\necho %PATH%<br \/>\n<\/code><\/p>\n<p> &#8230; can mean you can be in any directory using this functionality with command further above.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d20848' onclick='var dv=document.getElementById(\"d20848\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/file\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d20848' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/windows-command-line-batch-filespec-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[1835,135,234,358,418,997,1319,1435],"class_list":["post-20848","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-arguments","tag-batch","tag-command-line","tag-dos","tag-file","tag-programming","tag-tutorial","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/20848"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=20848"}],"version-history":[{"count":10,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/20848\/revisions"}],"predecessor-version":[{"id":20938,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/20848\/revisions\/20938"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=20848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=20848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=20848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}