

InterviewSolution
Saved Bookmarks
1. |
Solve : For Loops and Strings in DOS Batch Files? |
Answer» <html><body><p>I am in need of a batch <a href="https://interviewquestions.tuteehub.com/tag/file-11330" style="font-weight:bold;" target="_blank" title="Click to know more about FILE">FILE</a> to do the following:<br/><br/>for %%cust in (abcde fghij klmno pqrst uvwxy) do command %%cust,XX,YY,%%substringofcust<br/><br/>In order words, the 'command' should be:<br/><br/>command abcde,XX,YY,abc<br/>command fghij,XX,YY,fgh<br/>command klmno,XX,YY,klm<br/><br/>etc..........<br/><br/>I can't figure out how to <a href="https://interviewquestions.tuteehub.com/tag/pull-593530" style="font-weight:bold;" target="_blank" title="Click to know more about PULL">PULL</a> only a portion of the customerI think your description of the the task requirements is just a tad too opaque. <br/>Set up your list in a file. Ex:<br/><br/><><br/>abcde<br/>fghij<br/>klmno<br/>pqrst<br/>uvwxy<br/><br/>Then use the following batch:<br/><br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/><br/>for /f "delims=" %%A in (List.txt) do (<br/> set sub=%%A<br/> set sub=!sub:~0,3!<br/> command %%A,XX,YY,!sub!<br/>)<br/><br/>The way to grab the substring is in the second line. What it is saying is <strong>SET</strong> the <strong>SUB</strong> <a href="https://interviewquestions.tuteehub.com/tag/variable-772077" style="font-weight:bold;" target="_blank" title="Click to know more about VARIABLE">VARIABLE</a> to the <strong>SUB</strong> variable offset by <strong>0</strong> characters for <strong>3</strong> characters. Essentially, take the first 3 characters of the sub variable (which was previously set to be the customer string) and make them the new sub variable. This only works if you know that you are always <a href="https://interviewquestions.tuteehub.com/tag/going-1008806" style="font-weight:bold;" target="_blank" title="Click to know more about GOING">GOING</a> to use the characters at specific positions within the string. If what you are looking for varies in it's location, it will take some different coding to get it. Please let us know if this is the case.</p></body></html> | |