# Batch file. Creating username with exclusionary conditions



## lammv (Aug 19, 2012)

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.


```
@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
```


----------



## Squashman (Apr 4, 2003)

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 (Aug 19, 2012)

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:


> 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 (Apr 4, 2003)

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.

```
@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 (Apr 4, 2003)

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 (Aug 19, 2012)

> 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.


```
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

```
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?

```
FINDSTR /i "girl boy user"  userlist.ini
```
I've tried to put ERRORLEVEL, doesn't work

```
FINDSTR /i "girl boy user"  userlist.ini 
IF ERRORLEVEL  0  GOTO : B
```


----------



## Squashman (Apr 4, 2003)

```
echo %user% |findstr /i "user boy girl" >nul &&goto :MenuA
```


----------



## lammv (Aug 19, 2012)

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? 

```
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 (Apr 4, 2003)

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.


----------



## lammv (Aug 19, 2012)

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


----------



## Squashman (Apr 4, 2003)

I do not know what a cicle is.


----------



## Squashman (Apr 4, 2003)

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.


```
@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

```
input string is: SssstoOoormUUUu
final string is: SsstoOormUUU
Press any key to continue . . .
```


----------



## Squashman (Apr 4, 2003)

A bit more flexible.


```
@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 (Aug 19, 2012)

> I do not know what a cicle is.


hehe. ops. sorry. cycle


----------



## Squashman (Apr 4, 2003)

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


----------



## lammv (Aug 19, 2012)

Really thank you. 


> And if you really want to blow your mind, I posted your question on another forum I belong to


After i"ve got my mind I search ways to blow it...
For sure I will visit the forum.


----------



## lammv (Aug 19, 2012)

Hello there.
I 'm trying to solve one issue.

1. I have a file names.ini. The end of the file is:

```
FOV=15
[Network] 
Networkline=default
```
Here I need to change FOV and two last lines leave the same. So I use the FINDSTR you advice me.
The easiest way for me is finding lines which contain words " network fov", delete them and then add to the file following lines


```
echo FOV=%m% >> names.ini
echo [Network] >> names.ini
echo Networkline=default >> names.ini
```
so. FIND and DELETE.

I've found how to FIND


```
sc query | findstr /i "network fov" names.ini > ?????
```
 how to delete the founded lines?

2. And for sure.
what is the command here just replacing FOV with its new value


```
sc query | findstr  "FOV" names.ini > ?????
```
how to replace the founded line by the new FOV's value?
3. Also I have a line in the names.ini file
NAME=changed_username

I can find it by means of the line too. 
But how can I read it and use in my next operations ""


```
sc query | findstr "FOV" names.ini > ????
```


----------

