0phoi5 Posted December 4, 2015 Share Posted December 4, 2015 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. Quote Link to comment Share on other sites More sharing options...
cooper Posted December 5, 2015 Share Posted December 5, 2015 From reading this pretty elaborate help text on the findstr command it should work like this: echo "%INPUT%" | findstr /m /r "[^a-zA-Z]" IF %ERRORLEVEL% EQU 1 echo "Match" Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted December 6, 2015 Author Share Posted December 6, 2015 Thanks, I'll check this out at work tomorrow. Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted December 8, 2015 Author Share Posted December 8, 2015 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! Quote Link to comment Share on other sites More sharing options...
cooper Posted December 8, 2015 Share Posted December 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
fugu Posted December 8, 2015 Share Posted December 8, 2015 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% ) Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted December 10, 2015 Author Share Posted December 10, 2015 Thanks both, I'll have a play and repost results. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.