Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Batch file. Creating username with exclusionary conditions


(!)

lammv's Avatar
lammv lammv is offline
Member with 10 posts.
THREAD STARTER
 
Join Date: Aug 2012
18-Aug-2012, 11:21 PM #1
Batch file. Creating username with exclusionary conditions
I would like to create a batch file with the next parameters:
1. A user is been asked to create his username.
2. If the user tries to enter less then 5 characters or more then 15 characters it must come to :A again.
3. If the user tries to use words: (user, girl, boy) it must come to :B

But I solved the issue not completely.
1. If the user tries to press ENTER, it comes to :A
2. If the user's name starts as (user, girl, boy) it comes to :B
But if user's name starts from _user, badboy, nicegirl it doesn't work

So I want to exclude usernames which contain next words (user, girl, boy) in any places of the username. Aslo if a user tries to use SPACES instead of username OR spaces in username (my name) it must come to :A
I know that operator IF is not allowed to use masks. What is the solution?
Thank you.

Code:
@echo off
:begin
CLS
:A
echo.
echo Enter your username.(5-15 characters, no spaces)

set /p n=Username:

IF "%n%" == "" goto :A
REM HERE I NEED TO PUT ALL CONDITIONS
REM I COULD SOLVE THE PROBLEM IN THIS WAY ONLY. BUT IT IS NOT ENOUGHT
SET _prefix=%n:~0,4% 
IF /i %_prefix% == user goto :B
IF /i %_prefix% == girl goto :B

SET _prefix=%n:~0,3% 
IF /i %_prefix% == boy goto :B

attrib -h -r userlist.txt
echo [Settings] > userlist.txt
echo username=%n% >> userlist.txt
attrib +h +r userlist.txt
GOTO C

:B
attrib -h -r userlist.txt
echo [Settings] > userlist.txt
echo username=Default_name >> userlist.txt
attrib +h +r userlist.txt
GOTO C

:C
:END

Last edited by lammv; 18-Aug-2012 at 11:34 PM..
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
19-Aug-2012, 11:16 AM #2
So you are already trying to use String parsing to see if the username is USER or GIRL. Do the same to check to see if the name is less than 5 positions or greater than 15. In other words, if positions 4 and above is blank then go back to A. If position 15 and above is not blank then go back to A.

And for the spaces in the user names just remove them for them. Use string substitution to remove all blanks from the username before you test it for its length.
lammv's Avatar
lammv lammv is offline
Member with 10 posts.
THREAD STARTER
 
Join Date: Aug 2012
19-Aug-2012, 01:21 PM #3
Thank you.
I understand all algorithms. My problem is sintax and lack of knowlegde of operators.
I understand you. If I see the space in a username I should substitue it for underline for example. But how it is in codes? I 've started to learn batching some hours before the first message.

also in my question were these lines:
Quote:
But if user's name starts from _user, badboy, nicegirl it doesn't work
. it means I must check if my variables x, y, z (x=user; y=girl; z=boy) are suitable for a username. I should start the cheking from first character to 12 for x, y and from 1 to 13 for z., to avoid the username, for example: VERYveryOLDgirl .
so, some codes, please.
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
19-Aug-2012, 03:44 PM #4
I wouldn't allow them to use spaces at all. Just remove the spaces then check for the length.
I am also not a big fan of using a single character for a variable name. Gets confusing when using FOR loops. Make your variables descriptive if you can.
Code:
@echo off
:begin
CLS
:MenuA
set user=
echo.
echo Enter your username.(5-15 characters, no spaces)

set /p user=Username:
:: removing spaces from username
set user=%user: =%
:: checking if user name is defined
IF NOT DEFINED user goto :MenuA
:: check if user name is less than 5 positions long
IF "%user:~4%"=="" goto :MenuA
:: checking if user name is greater than 15 positions long
IF NOT "%user:~15%"=="" goto :MenuA
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
19-Aug-2012, 03:57 PM #5
In regards to your user names having the words user boy or girl in them you want to use the FINDSTR command for that.
lammv's Avatar
lammv lammv is offline
Member with 10 posts.
THREAD STARTER
 
Join Date: Aug 2012
20-Aug-2012, 08:10 AM #6
Quote:
In regards to your user names having the words user boy or girl in them you want to use the FINDSTR command for that.
I've learned it, thanks, but probably of wrong syntax it doesn't want to work.
So, I want to check outcome ini file about the words: girl, boy, user. If there is combination of the words, it rewrites the ini file with new name: DEFAULT_NAME for example.


Code:
FINDSTR /i "user girl boy" userlist.ini  goto :B
..........
:B
attrib -h -r userlist.ini

echo [Settings] > userlist.ini
echo username=DEFAULT_NAME >> userlist.ini

attrib +h +r userlist.ini
:END
Or this line
Code:
FINDSTR /i girl   userlist.ini       goto :B
what is the key /L ?

notice that name: niceGIRL is not good too.



what i must put after userlist.ini ? what condition. in the case findstr found these words: girl, boy, user?
Code:
FINDSTR /i "girl boy user"  userlist.ini
I've tried to put ERRORLEVEL, doesn't work
Code:
FINDSTR /i "girl boy user"  userlist.ini 
IF ERRORLEVEL  0  GOTO : B

Last edited by lammv; 20-Aug-2012 at 08:43 AM..
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
20-Aug-2012, 11:09 AM #7
Code:
echo %user% |findstr /i "user boy girl" >nul &&goto :MenuA
lammv's Avatar
lammv lammv is offline
Member with 10 posts.
THREAD STARTER
 
Join Date: Aug 2012
20-Aug-2012, 12:36 PM #8
You know? It works. Thank you. It seems I should study it more deeply.
How can I exclude the following nicks with more then 3 alike characters in a row: stormUUUUU or +++++++++++ for example. What should I add to your last perfect line?
Code:
echo %user% |findstr /i "user boy girl" >nul &&goto :MenuA
In the case you are able to do it I will eat my hat.
I even agree to get it in two lines.
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
20-Aug-2012, 01:52 PM #9
That would be kind of tough I think. Findstr has regular expression capability but unless you know what character you want to find that is repeating it would be very difficult.

There is a 3rd party command line utility called TR(translate). It has the capability to squeeze repeats.
So if you have a string of characters like SSStormUUUUU it would output StormU.

I don't know of any way to squeeze repeats in pure batch. But I will look around

EDIT: TR won't work either. You have to know the character you want to squeeze as well.

Last edited by Squashman; 20-Aug-2012 at 02:24 PM..
lammv's Avatar
lammv lammv is offline
Member with 10 posts.
THREAD STARTER
 
Join Date: Aug 2012
20-Aug-2012, 02:49 PM #10
It is clear if we dont have enought operators to do it, we should think around an intricate algorith from existing operators.
What you say about a cicle that check if two last characters are equal the two previous. if so then it renames the last character or just deletes it.

OR
%n:~0,3% this counter must be putted in a cicle %n:~1,4% , %n:~2,5%
or better
%first_variable:~0,3% %second_variable:~1,4% %third_variable:2,5% ...... %eleventh_variable:8,11%

%n:~x,y%
x=x+1 ; y=y+1
and after each cicle it should be compared with the previous

delirium

Last edited by lammv; 20-Aug-2012 at 03:24 PM..
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
20-Aug-2012, 06:29 PM #11
I do not know what a cicle is.
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
20-Aug-2012, 08:16 PM #12
Not sure if this will make any sense to you. This is just a small example of what you are trying to do. I am trying to make it more flexible. This also may die if it has special characters in it.

Code:
@echo off
setlocal enabledelayedexpansion

set var=SssstoOoormUUUu
echo input string is: %var%
FOR /L %%G in (0,1,11) do call :parse %%G
echo final string is: %var%
Pause
goto :eof

:parse
set N1=%~1
set /a N2=%~1+1
set four=!var:~%N1%,4!
set three=!var:~%N1%,3!
IF /I "!var:~%N1%,3!"=="!var:~%N2%,3!" set var=!var:%four%=%three%!
Output
Code:
input string is: SssstoOoormUUUu
final string is: SsstoOormUUU
Press any key to continue . . .
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
20-Aug-2012, 10:53 PM #13
A bit more flexible.

Code:
@echo off

call :parse 2 SssstoOoormUUUUu
Pause
goto :eof

:parse
setlocal enabledelayedexpansion
set var=%~2
set N1=%~1
set /a N2=%~1+1
set p1=0

:iLoop
set /a p2=p1+1
set replace=!var:~%p1%,%N2%!
set change=!var:~%p2%,%N1%!
IF /I "!var:~%p1%,%~1!"=="!var:~%p2%,%~1!" set var=!var:%replace%=%change%!
IF /I NOt "!var:~%p1%,%~1!"=="!var:~%p2%,%~1!" set /a p1+=1
IF NOT "!var:~%p2%!"=="" GOTO :iLoop
echo %var%
goto :EOF
Parse 2 outputs: sstoormUu
Parse 1 outputs: stOrmu
lammv's Avatar
lammv lammv is offline
Member with 10 posts.
THREAD STARTER
 
Join Date: Aug 2012
20-Aug-2012, 11:26 PM #14
Quote:
I do not know what a cicle is.
hehe. ops. sorry. cycle
Squashman's Avatar
Trusted Advisor with 19,656 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
21-Aug-2012, 07:20 AM #15
And if you really want to blow your mind, I posted your question on another forum I belong to. Check out Dave Benham's post. His script will handle special characters.
http://www.dostips.com/forum/viewtopic.php?f=3&t=3671
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑