Jump to content

[Batch/CMD] - Does variable contain numbers, spaces, one word?


0phoi5

Recommended Posts

Hi all,

I have a batch file which starts with the user inputting either;

  • The hostname for a PC
  • The full name of a user
  • The surname of a user

I would like the script to work out what was input and GOTO the next relevant section.

Basically;

Does the variable contain any numbers? If yes, GOTO Hostname

If not;

Does the variable contain any spaces? If yes, GOTO FullName

If not;

Does the variable contain only 1 word made up of only letters? If yes, GOTO Surname

If not;

Error message.

I have tried various instances of...

echo %INPUT%|findstr /r "[^a-zA-Z]" > nul

... and similar, but I can't seem to get it work correctly.

Thanks in advance.

Link to comment
Share on other sites

So the current code I have is...

echo %StartInput% | findstr /m /r "[0-9]"
IF ERRORLEVEL 1 ECHO This includes numbers
echo %StartInput% | findstr /m /r "[^a-zA-Z]"
IF ERRORLEVEL 1 ECHO This doesn't include numbers
Pause

... but the output, when I enter '1234' as the variable %StartInput% is...

1234
1234
Press any key to continue . . .

It appears it's ignoring the findstrs all together and is simply echoing the varibale on-screen twice.

Using your example doesn't work either, unfortunately, as it does the same...

echo "%StartInput%" | findstr /m /r "[0-9]"
IF %ERRORLEVEL% EQU 1 echo "Numbers"
echo "%StartInput%" | findstr /m /r "[^a-zA-Z]"
IF %ERRORLEVEL% EQU 1 echo "Letters Only"
Pause

... and the output, when I enter '1234' as the variable %StartInput% is...

"1234"
"1234"
Press any key to continue . . .

I've also tried;

echo "%StartInput%" | findstr /m /r "[^0-9]"
echo "%StartInput%"|findstr /m /r "[0-9]"
echo "%StartInput%"|findstr "[0-9]"

All give the same output, namely repeating the variable twice.

Hmmm!

Link to comment
Share on other sites

Did you try to echo ERRORLEVEL itself? It might be that it doesn't get set due to the pipe command, so try to echo the value into a file, then pass the filename as a parameter to findstr.

Link to comment
Share on other sites

A while back I found a batch script for OS version detection, it's doing something similar, maybe you can adapt it:

SET OSVersion=Unknown

VER | FINDSTR /L "5.0" > NUL

IF %ERRORLEVEL% EQU 0 SET OSVersion=2000

VER | FINDSTR /L "5.1." > NUL

IF %ERRORLEVEL% EQU 0 SET OSVersion=XP

VER | FINDSTR /L "5.2." > NUL

IF %ERRORLEVEL% EQU 0 SET OSVersion=2003

VER | FINDSTR /L "6.0." > NUL

IF %ERRORLEVEL% EQU 0 SET OSVersion=Vista

VER | FINDSTR /L "6.1." > NUL

IF %ERRORLEVEL% EQU 0 SET OSVersion=7

VER | FINDSTR /L "6.2." > NUL

IF %ERRORLEVEL% EQU 0 SET OSVersion=8

IF %OSVersion%==Unknown (

ECHO Unable to determine your version of Windows.

) ELSE (

ECHO You appear to be using Windows %OSVersion%

)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...