| 1. |
Solve : Set part of a user's input as a variable? |
|
Answer» How do I set part of a user's input equal to a variable? but I'm not certain that a For loop is called for. What's the right way to do it?No, you don't need the FOR command at all as I have demonstrated. Either way works. Use what you feel comfortable with.Squashman - my apologies. I glanced at the first line of your command (which won't work for my application of the command) and disregarded it. Based on your follow-up I actually APPLIED the logic in a way that will work for me and it works (of course you knew that)! Learned something new from you today, thank you!Sometimes I just like to open the cmd prompt and run the commands one at a time to show the user the output. Sometimes the visual helps. But the base code is just: Code: [Select]SET /P UPN=Enter the users UPN: set upn=%upn:*@=%Of course I would do some error checking on the input so I would end up doing this. Code: [Select]:USERLABEL SET UPN= SET /P UPN=Enter the users UPN: IF DEFINED UPN ( set UPN=%UPN:*@=% ) ELSE ( GOTO USERLABEL )There are a few differences between these methods. I don't think that any of these scenarios will COME up in your example, by the way... If there is nothing before the @, the FOR method will return nothing, as it will read the @ as TABULATION, and the part of the string that you are looking for will be read as the first token, and ignored. The special expansion method would in this case select everything except the @. If the @ was followed by a second @, then the FOR method would eliminate the second @ as well, the expansion method wouldn't. If there was an @ somewhere else in the company name, then the FOR method would detect everything after that @ as the THIRD token, and exclude it, the Expansion method however would have already found the substring it was looking for, and would ignore the second @ Assuming that everybody using the script has a name, and that company names don't include the @ symbol, then either method will work FINE, however it seems that if you aren't going to add some more code to make it secure then Squashman's method is more secure. Also thanks Squashman, for that method. I'd never seen that used in place of for /f before.The asterisk only works as a wild card if it is the first character. It does not work in reverse and then you would need to use a FOR command to get the first token. |
|