1

i am writing a script that sets a new computer name. the syntax is:

script.bat /NAME:<place new name here>

the issue is, im not sure how to get my script to verify that the user inputted only letters numbers and hyphens. so, basically, LENOVO-190 is ok, but LENOVO:~19|0) is not. Also, id like it to change all lowercase letters to capitals, just for aestetic affect (i think it looks nicer).

i only want my script to accept A-Z, 0-9, and -. and if an invalid character is inputted (lets say #), it will say something like "hey! # is not allowed!"

thanks everyone, cheers.

ditheredtransparency
  • 475
  • 3
  • 10
  • 18
  • StackOverflow does not write scripts to order. Your task is to create a script, ours is to provide assistance with undesired issues resulting from its running. – Compo Nov 21 '17 at 19:08
  • Please read the first sentence carefully. – ditheredtransparency Nov 21 '17 at 19:18
  • @ditheredtransparency, Compo is basically requesting to see your existing code and what line of code are you having problems with. – Squashman Nov 21 '17 at 20:11
  • 1
    @ditheredtransparency, I believe this is the [answer](https://stackoverflow.com/a/47166947/1417694) you are looking for. – Squashman Nov 21 '17 at 20:21

1 Answers1

4

Modern versions of Windows have findstr, which is sort-of like grep in that it can look for matches of a regular expression. You can use it to check whether the input name matches your expectations. For example:

C> echo abcde| findstr /r /i ^^[a-z][a-z0-9-]*$ && echo Good name! || echo Only letters, digits and hyphens, please!
abcde
Good name!

C> echo ab#de| findstr /r /i ^^[a-z][a-z0-9-]*$ && echo Good name! || echo Only letters, digits and hyphens, please!
Only letters, digits and hyphens, please!

As to how to replace lower-case letters with upper-case letters in pure batchese, there are ways.

AlexP
  • 4,370
  • 15
  • 15
  • You are using the `/I` switch so you should not need to list the alphabet range twice within each class. – Squashman Nov 21 '17 at 20:54
  • Cool, big fan of `grep`. Glad to be able to use something similar under Windows. Thanks guys! @Squashman, I also upvoted that post of yours that you linked me to, as it also worked. :-) – ditheredtransparency Nov 22 '17 at 06:15
  • 1
    @ditheredtransparency, I was actually trying to link you to rojo's answer in that question which is similar to the one Alex posted here as well. Rojo's answer finds any characters that are not alphanumeric or a hyphen and tells you it is invalide. Alex's is the opposite but the conditional logic is reversed so they both work the same. Either findstr method should be rock solid. – Squashman Nov 22 '17 at 16:07