1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batch file. Creating username with exclusionary conditions

Discussion in 'Software Development' started by lammv, Aug 19, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. lammv

    lammv Thread Starter

    Joined:
    Aug 18, 2012
    Messages:
    10
    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
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  3. lammv

    lammv Thread Starter

    Joined:
    Aug 18, 2012
    Messages:
    10
    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:
    . 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.
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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
    
     
  5. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    In regards to your user names having the words user boy or girl in them you want to use the FINDSTR command for that.
     
  6. lammv

    lammv Thread Starter

    Joined:
    Aug 18, 2012
    Messages:
    10
    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
     
  7. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Code:
    echo %user% |findstr /i "user boy girl" >nul &&goto :MenuA
     
  8. lammv

    lammv Thread Starter

    Joined:
    Aug 18, 2012
    Messages:
    10
    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.
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  10. lammv

    lammv Thread Starter

    Joined:
    Aug 18, 2012
    Messages:
    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
     
  11. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    I do not know what a cicle is.
     
  12. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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 . . .
     
  13. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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
     
  14. lammv

    lammv Thread Starter

    Joined:
    Aug 18, 2012
    Messages:
    10
    hehe. ops. sorry. cycle
     
  15. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
  16. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1065658