Page 1 of 1
Batch treat special characters as input errors
Posted: 29 Mar 2021 13:25
by btchneb
I want all non-numeric characters to be considered as input error. Alphabetic characters are well considered but not certain special characters like colons (and others ones).
Any idea how to achieve this ?
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set /a UserProfileNo=0
FOR /F "delims=" %%I IN ('dir /a:d /b "%SystemDrive%\Users\"') do (
set "UserProfileName=%%~I"
set /a UserProfileNo+=1
set "UserProfileName[!UserProfileNo!]=!UserProfileName!"
)
FOR /L %%k IN (1, 1, %UserProfileNo%) DO echo %%~k !UserProfileName[%%~k]!
set /p choice=Enter a Profile Number:
FOR %%f IN (%choice%) DO if "!UserProfileName[%%~f]!" == "" (
echo Error in the input
goto end
)
echo NO Error in the input
:end
pause
exit
Thank you
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 06:58
by Squashman
What is wrong with the help you got on
StackOverFlow?
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 09:50
by btchneb
Maybe there are other ways to achieve the same result. I'm open to all proposals.
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 11:53
by Compo
btchneb wrote: ↑30 Mar 2021 09:50
Maybe there are other ways to achieve the same result. I'm open to all proposals.
There are many ways of performing a task, and two different ones have already been submitted on SO. The least you could have done was to test both of our codes,
(we are both members of this community too), and provide feedback as to whether they achieve what was intended, i.e. only the numbers for the individual entries should pass through to the next stage of the script. It would also have been respectful to inform both myself and T3RRY/T3RR0R that whilst you were grateful for our responses, you were holding out for yet another method, possibly explaining to us why our methods did not meet with your specific needs, and giving us an opportunity, along with the rest of the community, to offer more suitable alternatives.
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 13:02
by btchneb
The answer provided by T3RR0R doesn't meet some of my expectations (please see my comment).
Your answer doesn't suit me either : I don't intend to use WMIC because I actually get the user profiles directly from the Active Directory using another tool. Plus, you have a style of code that only experienced users can understand (sorry if my answer doesn't suit you either).
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 13:58
by Compo
btchneb wrote: ↑30 Mar 2021 13:02
Your answer doesn't suit me either : I don't intend to use WMIC because I actually get the user profiles directly from the Active Directory using another tool. Plus, you have a style of code that only experienced users can understand (sorry if my answer doesn't suit you either).
The method I used to retrieve the user names and profile paths is irrelevant to your question. The code for performing the task you require, is in the :Pick labelled section which occurs after the echoing of numbers with names list to choose from, and the defining of the variables with respect to each. So all you need to study is the code in that labelled section. If you already have code you've used for retrieving the list and defining the variables, and you're unable to adapt either of our answers to that, feel free to post your code here, or in the originating post, and see if we can assist you in adapting it accordingly. Help is a two way process, and thus far, your part of the process has been lacking in this area.
Your question does not mention, or show, any method of retrieving profiles other than a basic parsing of a hard coded path. had it done so, it may not have been necessary for me to provide more robust code for retrieving standard local users. There's no reason why you could not have supplied code which is more representative of your task, and in doing so, the chances of responses being more suited would have significantly increased.. Regardless of that, there is still no reason why you could not still copy my code as is, and test in on any supported Windows OS, and see what happens when you include those poison characters. You could then have provided focused feedback, complete with the code you were unable to adapt ours to work with, and give us a chance to assist you.
My code, as was specifically mentioned in the linked SO post, was not posted to be modified, or adjusted, it was specifically for testing the method. You haven't asked for support with it, despite me specifically mentioning it, which suggests that despite my efforts, you've not tried the code at all, (a straight forward copy, paste and save followed by a doubleclick).
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 15:20
by Squashman
I will give a slightly different option that uses the CHOICE command. By utilizing the choice command you don't have to validate the input because the CHOICE command defines the allowable input characters. Since the CHOICE command only allows for single character input I have changed your input to require letters instead of numbers. I used the case sensitive option with the choice command so that you could in theory expand this to much more than 26 upper case alpha character choices. You could also have 26 lower case letter choices for a total of 52. Using the choice command also helps negate code injection. Batch files using SET /P input are susceptible to that.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set /a "UserProfileNo=0"
set "choices="
FOR /F "delims=" %%I IN ('dir /a:d /b "%SystemDrive%\Users\"') do (
set /a UserProfileNo+=1
call :ResolveChar !UserProfileNo!
set UserProfileName!retval!=%%I
set choices=!choices!!retval!
echo [!retval!] %%I
)
choice /c !choices! /cs /m "Enter a Profile Letter: " /n
call :ResolveChar %errorlevel%
set "Profile=!UserProfileName%retval%!"
echo Profile: %Profile%
pause
goto :eof
:ResolveChar
SETLOCAL
set "keys=_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_startchar=%1"
CALL SET "char=%%keys:~%_startchar%,1%%"
ENDLOCAL & SET "retval=%char%"
goto :eof
And running the code.
Code: Select all
C:\Users\Squashman\Desktop>so.bat
[A] All Users
[B] Beavis
[C] Butthead
[D] Devil
[E] Default User
[F] Marvin
[G] Squashman
[H] Public
Enter a Profile Letter: G
Profile: Squashman
Press any key to continue . . .
Obviously you may want to take a different approach to iterate the real profile names as using the Folder Names in C:\Users is not accurate. You should probably use WMIC for that.
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 17:00
by ShadowThief
I would be remiss if I didn't mention that you can also use xcopy to get user input
Code: Select all
@echo off
setlocal EnableDelayedExpansion
:::::
:: Advanced user input in batch [using xcopy]
:: https://gist.github.com/Grub4K/2d3f5875c488164b44454cbf37deae80
:::::
:: Returns by echoing result.
:: Fetching using for /f from main
:::::
set "prompt="
:: the input prompt
set "symbol="
:: the symbol that displays instead
set "length="
:: the max length of the input
set "allowed="
:: allowed characters ("a b c A B C")
set "invalid="
:: invalid characters ("b c d B C D")
:: allowed characters will override invalid
:::::
:: parse flags dict
:flags
if not "%~1" == "" (
set "%~1"
shift
goto flags
)
:: Fetch needed characters
for /f %%A in ('copy /Z "%comspec%" nul') do set "CR=%%A"
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "BS=%%A"
:: start of program
if defined symbol set "symbol=%symbol:~0,1%"
set "input=."
<NUL set /p "=.!BS! !BS!!prompt!" >CON
:input
set "key="
for /f "delims=" %%A in ('xcopy /w "%comspec%" "%comspec%" 2^>nul') do if not defined key set "key=%%A"
set "key=!key:~-1!"
if !key! equ !BS! (
if NOT "!input!" == "." (
set "input=!input:~0,-1!"
<NUL set /p "=!BS! !BS!" >CON
)
) else if !key! equ !CR! (
echo+ >CON
echo+!input:~1!
exit /B 0
) else (
if defined length (
if "!input:~-%length%!" == "!input!" (call)
) else (
(call)
)
) || (
if defined allowed (
for %%A IN (!allowed!) DO if %%A equ !key! (
set "input=!input!!key!"
if defined symbol (
<NUL set /p "=!symbol!" >CON
) else (
<NUL set /p "=.!BS!!key!" >CON
)
)
) else (
for %%A IN (!invalid!) DO if %%A equ !key! goto input
set "input=!input!!key!"
if defined symbol (
<NUL set /p "=!symbol!" >CON
) else (
<NUL set /p "=.!BS! !BS!!key!" >CON
)
)
)
goto input
Re: Batch treat special characters as input errors
Posted: 30 Mar 2021 19:34
by penpen
btchneb wrote: ↑29 Mar 2021 13:25
I would like all non-numeric characters to be considered as input error. Alphabetic characters are well considered but not certain special characters like colons and others ones such as !, ", %, ^, &, ), =, |, <, and >
I don't understand what you want:
In the first sentence you wrote that alphabetic characters are considered as input error (because they are non-numeric characters).
In the second sentence they are Ok.
To me that seems to be at least ambiguous.
Which characters exactly do you define as input error?
Do you want to filter out the characters, or do you want to discard the complete line?
Do you want to echo a success or an error message?
Is the information you want to list is volatile?
Depending on what you need, the resulting algorithm might be extremely easy or inherently complicated.
If you need to filter out complete lines, you might consider redirect your listed data into a text file and
search it with findstr within a for/f-loop twice (one run to process valid lines and one to output an error message for each invalid line).
penpen