{"id":55523,"date":"2008-05-21T01:45:00","date_gmt":"2008-05-21T01:45:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/05\/21\/hey-scripting-guy-how-can-i-compare-two-strings-values-and-then-report-back-the-result-of-that-comparison\/"},"modified":"2008-05-21T01:45:00","modified_gmt":"2008-05-21T01:45:00","slug":"hey-scripting-guy-how-can-i-compare-two-strings-values-and-then-report-back-the-result-of-that-comparison","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-compare-two-strings-values-and-then-report-back-the-result-of-that-comparison\/","title":{"rendered":"Hey, Scripting Guy! How Can I Compare Two Strings Values and Then Report Back the Result of That Comparison?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I need to write a command-line script that, given two arguments, can compare the two values and then \u2013 using standard errorlevel \u2013 report back whether string 1 is greater than or less than string 2, or if the two strings are identical. Can you help me with that?<br \/>&#8212; JP<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, JP. You know, in the past few days the sports world has been rocked by the revelation that Jason Giambi, first baseman for the New York Yankees, often wears a gold lam\u00e9, tiger-striped thong under his uniform in an attempt to help him break out of a slump. \u201cI only wear it when I\u2019m really desperate,\u201d said Giambi, who must be about due to buy a new thong considering his current batting average of .191. Giambi also noted that, over the years, he has loaned his thong out to teammates Derek Jeter, Bernie Williams, Johnny Damon, Robin Ventura, and Robinson Cano. &#8220;All of them wore it and got hits,&#8221; he said. &#8220;The thong works every time.&#8221;<\/p>\n<p>Of course, upon hearing this news most people immediately wondered, \u201cGee, does this mean that the <i>Scripting Guys<\/i> wear gold-lam\u00e9, tiger-striped thongs under their clothes every time <i>they<\/i> go into a slump?\u201d As it turns out, the answer to that question is this: no, the Scripting Guys do <i>not<\/i> wear gold-lam\u00e9, tiger-striped thongs under their clothes every time they go into a slump. <\/p>\n<p>But that\u2019s mainly because the Scripting Guys have never actually gone <i>into<\/i> a slump. If we ever do, well, who knows?<\/p>\n<table class=\"dataTable\" id=\"EKD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. We should clarify that, when it comes to wearing wear gold-lam\u00e9, tiger-striped thongs, we\u2019re talking solely about work and work-related activities. What Scripting Guy Dean Tsaltas may or may not do in the privacy of his own home is his own business.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Of course, upon hearing <i>this<\/i> news most people immediately wondered, \u201cThe Scripting Guys <i>never<\/i> go into a slump? How is that even possible?\u201d Well, in part that\u2019s possible because we never really do much; after all, if your normal day consists of doing nothing at all, well, it\u2019s hard to fall into a slump and do even <i>less<\/i> than nothing. (Although if anyone <i>could<\/i> do less than nothing it\u2019s the Scripting Guy who writes this column). However, it\u2019s also because we\u2019re able to come up with scripts that can compare two values and then report back whether string 1 is less than or greater than string 2, or whether the two values are identical.<\/p>\n<p>You know, scripts just like this one:<\/p>\n<pre class=\"codeSample\">strFirst = Wscript.Arguments(0)\nstrSecond = Wscript.Arguments(1)\n\nintComparison = StrComp(strFirst, strSecond)\n\nIf intComparison = -1 Then\n    Wscript.Echo \"String 1 is less than string 2.\"\n    Wscript.Quit (1)\nElseIf intComparison = 1 Then\n    Wscript.Echo \"String 1 is greater than string 2.\"\n    Wscript.Quit (2)\nElse\n    Wscript.Echo \"The two strings are identical.\"\n    Wscript.Quit (3)\nEnd If\n<\/pre>\n<p>Before we explain how the code works let\u2019s take a look at the sample commands that JP included in his email:<\/p>\n<pre class=\"codeSample\">CScript \/\/NoLogo File.vbs  7.1  8.3b5-HP \nCScript \/\/NoLogo File.vbs  8.3b5-HP  8.3b5-HP \nCScript \/\/NoLogo File.vbs  9.2  8.3b5-HP\n<\/pre>\n<p>As you can see, what JP wants to do is start a script (in this case, a script named File.vbs) from the command prompt, passing that script two arguments. In his first example, those two arguments are the following:<\/p>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>7.1 <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>8.3b5-HP<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>He would like the script to compare the two values and then set the DOS errorlevel according to the following criteria: If the first string is \u201cless than\u201d the second (that is, when sorted alphabetically would string 1 come first) then he wants to set the errorlevel to 1. If the first string is greater than the second string he\u2019d like to set the errorlevel to 2. <\/p>\n<p>And what if the two strings are identical? Well, in that case, he\u2019d like to set the errorlevel to 3. Can that be done? You bet it can.<\/p>\n<p>Or at least we <i>think<\/i> it can. Let\u2019s find out for sure.<\/p>\n<p>We start out by using the following two lines of code to grab the arguments that were supplied when the script was started:<\/p>\n<pre class=\"codeSample\">strFirst = Wscript.Arguments(0)\nstrSecond = Wscript.Arguments(1)\n<\/pre>\n<p>As you probably know, each time you start a VBScript script any command-line arguments supplied when starting the script are automatically stored in the <b>Wscript.Arguments<\/b> collection. Because this is a \u201czero-based\u201d collection that means that the first argument becomes argument 0, the second argument becomes argument 1, etc. In other words, the command <b>strFirst = Wscript.Arguments(0)<\/b> simply tells the script to take the first command-line argument (item 0 in the collection) and assign it to the variable strFirst. We then use the same process to assign the second command-line argument (item 1) to the variable strSecond.<\/p>\n<p>That brings us to this line of code:<\/p>\n<pre class=\"codeSample\">intComparison = StrComp(strFirst, strSecond)\n<\/pre>\n<p>What we\u2019re doing here is using VBScript\u2019s <b>StrComp<\/b> function to compare our two string values. By default, we\u2019re doing a \u201cbinary comparison,\u201d which is basically a case-sensitive comparison. In other words, the string value <b>ABC<\/b> is <i>not<\/i> the same as the string value <b>abc<\/b>. What if we wanted to do a case-<i>insensitive<\/i> comparison? No problem; all we have to do is add the optional parameter 1 to the end of our command:<\/p>\n<pre class=\"codeSample\">intComparison = StrComp(strFirst, strSecond, 1)\n<\/pre>\n<p>That\u2019s it.<\/p>\n<p>Well, OK: and <i>maybe<\/i> we\u2019d also put on a gold-lam\u00e9, tiger-striped thong. But that\u2019s optional when working with the StrComp function.<\/p>\n<p>StrComp is going to return one of three possible values:<\/p>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>&#8211;<b>1<\/b>, if the first string listed (strFirst) is less than the second string listed.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>1<\/b>, if the first string listed is greater than the second string listed.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>0<\/b>, if the two strings are identical.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>What does that mean? Well, let\u2019s go back to our first example:<\/p>\n<pre class=\"codeSample\">CScript \/\/NoLogo File.vbs 7.1 8.3b5-HP\n<\/pre>\n<p>In this case, the first string (7.1) is less than the second string (8.3b5-HP). Thus StrComp is going to return a -1. See how that works? Suppose the arguments were switched and we started the script with <i>this<\/i> command:<\/p>\n<pre class=\"codeSample\">CScript \/\/NoLogo File.vbs 8.3b5-HP 7.1\n<\/pre>\n<p>In this case StrComp would return a 1; that\u2019s because the first string (8.3b5-HP) is greater than the second string (7.1).<\/p>\n<p>That\u2019s all well and good, except for this: what are we supposed to <i>do<\/i> with the -1 that StrComp returns to us? As it turns out, we\u2019re going to execute this block of code:<\/p>\n<pre class=\"codeSample\">If intComparison = -1 Then\n    Wscript.Echo \"String 1 is less than string 2.\"\n    Wscript.Quit (1)\nElseIf intComparison = 1 Then\n    Wscript.Echo \"String 1 is greater than string 2.\"\n    Wscript.Quit (2)\nElse\n    Wscript.Echo \"The two strings are identical.\"\n    Wscript.Quit (3)\nEnd If\n<\/pre>\n<p>All we\u2019re doing here is using a set of If-Then statements to take action based on the value of intComparison (that is, based on the number returned by StrComp). Suppose intComparison <i>is<\/i> -1? That\u2019s fine; in that case we execute these two lines of code:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"String 1 is less than string 2.\"\nWscript.Quit (1)\n<\/pre>\n<p>In the first line we\u2019re simply echoing back the fact that string 1 is less than string 2. (We don\u2019t really need to do that, but when you\u2019re testing your script this makes it easy to determine whether or not the script is working correctly.) In addition to that, we use the <b>Quit<\/b> method to terminate the script. But notice that we don\u2019t just Quit; instead, we specify an errorlevel of 1 when quitting:<\/p>\n<pre class=\"codeSample\">Wscript.Quit (1)\n<\/pre>\n<p>So will that <i>really<\/i> set the DOS errorlevel to 1? You bet it will. And you can verify that by typing the following command at the command prompt:<\/p>\n<pre class=\"codeSample\">echo %errorlevel%\n<\/pre>\n<p>Unless you\u2019re in a terrible slump and <i>nothing<\/i> is going right for you, you should get back the following:<\/p>\n<pre class=\"codeSample\">1\n<\/pre>\n<p>Granted, that\u2019s not much. But it\u2019s all we really needed.<\/p>\n<p>We then run a separate check to see if intComparison is equal to 1; in that case, that means that string 1 is greater than string 2, which also means that we execute these two lines of code instead:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"String 1 is greater than string 2.\"\nWscript.Quit (2)\n<\/pre>\n<p>And what if intComparison isn\u2019t equal to <i>either<\/i> 1 or -1? Well, in that case we assume that intComparison is equal to 0, which means that the two strings are identical:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"The two strings are identical.\"\nWscript.Quit (3)\n<\/pre>\n<p>And that, as they say, is that.<\/p>\n<p>If you aren\u2019t a baseball fan you might have been a little \u2026 surprised \u2026 to hear that a major leaguer would wear a gold-lam\u00e9, tiger-striped thong under his uniform in an attempt to break out of the slump. Admittedly that <i>is<\/i> a little weird, but, then again, not <i>that<\/i> weird, at least not for baseball players. Needless to say, baseball players tend to be a little superstitious. For example, Kevin Rhomberg, a journeyman major leaguer back in the 80s, had a compulsive need to \u201ctouch back\u201d anyone or anything that touched him. One time, in a minor league game, Rhomberg was tagged out by a player who was aware of this superstition. Upon tagging Rhomberg this player turned and threw the ball out of the stadium. Rhomberg, in turn, spent several hours after the game trying to track the ball down.<\/p>\n<p>We can\u2019t remember for sure if Rhomberg ever found the ball or not, but if he didn\u2019t, well, no big deal. After all, any time he was unable to touch someone back he would simply send them a letter saying, \u201cThis counts as a touch.\u201d <\/p>\n<p>Even the Scripting Guy who writes this column is not immune to baseball superstitions. For example, back in his playing days he used to eat coleslaw every game day. Why would he eat coleslaw in every game day, especially in light of the fact that he doesn\u2019t really <i>like<\/i> coleslaw? Because when he was 12 the Scripting Mom made him eat some coleslaw before a game. \u201cColeslaw is good for you,\u201d she said. \u201cIt\u2019ll help you get some hits.\u201d That night the Scripting Guy who writes this column went 3-for-3; after that he began eating coleslaw before every game. This same Scripting Guy also had to walk around the umpire and catcher in order to get into the batter\u2019s box; he could not cross in front of them. Why not? Beats us. But if it was a choice between that or wearing a gold-lam\u00e9, tiger-striped thong, well \u2026.<\/p>\n<p>Incidentally, the Scripting Editor is once again playing in the Microsoft softball league, and the Scripting Guy who writes this column has noticed that she has a few baseball-related superstitions as well. For example, if she\u2019s playing second base and the ball is hit to her she absolutely <i>refuses<\/i> to catch the ball; instead she just lets it go by. Likewise, when she\u2019s at bat, she\u2019s <i>very<\/i> superstitious about hitting the ball hard; instead she just dribbles a ground ball to one of the infielders.<\/p>\n<p>Maybe she needs to eat more coleslaw.<\/p>\n<table class=\"dataTable\" id=\"EVAAC\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Ed<\/b><b>itor\u2019s Note:<\/b> The Scripting Editor has no superstitions that keep her from fielding ground balls and getting solid base hits. Sometimes the glove and the bat seem to be a little superstitious, though. Maybe some coleslaw in the glove\u2026? Okay, maybe not.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to write a command-line script that, given two arguments, can compare the two values and then \u2013 using standard errorlevel \u2013 report back whether string 1 is greater than or less than string 2, or if the two strings are identical. Can you help me with that?&#8212; JP Hey, JP. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,21,5],"class_list":["post-55523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to write a command-line script that, given two arguments, can compare the two values and then \u2013 using standard errorlevel \u2013 report back whether string 1 is greater than or less than string 2, or if the two strings are identical. Can you help me with that?&#8212; JP Hey, JP. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55523\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}