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.

Solved: Batch Login "Find / Findstr / F Tokens" Help

Discussion in 'Windows Vista' started by Psycoblast, Apr 7, 2010.

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

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    Sorry about all the post this week but I have another question

    Ok heres whats going on I'm making a login system on my friends USB and I'm using the find command to search a data file for the user name which i stored all the user names in.

    My Problem with the user names is that if I use this code:

    set /p "user=>"
    REM drivel was set earlier in code not shown here
    Find "%User%" "%drivel%:\Check\Data\Users.dat"
    IF %errorlevel%==0 (
    goto FindPass
    )
    :UserError
    cls
    echo Sorry %user% dosen't exist
    pause >nul
    exit
    :FindPass

    and So on...

    Now lets say in the Users.dat file theres only two users mentioned Psycoblast and Admin
    but if I type Psyco the errorlevel will come up zero how can I make it find whole word per line?

    Also 2 things sorry I didn't fix this but after Find there is a /I how do I make the text quit overwriting each other when I click back in this window I'm typing in?

    Thanks
    -Psycoblast
     
  2. Psycoblast

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    Oh yeah I'll get to the f token problem later.
     
  3. Psycoblast

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    Ok I finally got it after messing with it this whole time

    Replace Find in code above with Findstr /I /X

    Now it works now to next question the F Tokens

    Ok currently I'm using this code:

    attrib -h -s "%drivel%\%user%.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ren "%drivel%\%user%.{21EC2020-3AEA-1069-A2DD-08002B30309D}" "%user%'s Folder"

    ping localhost -n 2 >nul

    :skip1

    IF Exist "%drivel%\%user%'s Folder\~Sys~\" goto skip2

    attrib -h -s "%drivel%\%user%'s Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ren "%drivel%\%user%'s Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}" "~Sys~"

    ping localhost -n 2 >nul

    :skip2

    For /F "tokens=1*" %%A in ("%drivel%\%user%'s Folder\~Sys~\Logs\P1.dat") Do set CP1=%%A

    ren "%drivel%\%user%'s Folder\~Sys~" "~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    attrib +h +s "%drivel%\%user%'s Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"

    ping localhost -n 2 >nul

    ren "%drivel%\%user%'s Folder" "%User%.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    attrib +h +s "%drivel%\%user%.{21EC2020-3AEA-1069-A2DD-08002B30309D}"

    ping localhost -n 2 >nul

    cls
    echo %user% Please Enter Password
    echo.
    set /p "P1=>"
    IF "%P1%"=="%CP1%" goto CorrectPass
    IF NOT "%P1%"=="%CP1%" goto WrongPass
    exit

    So what happens is no matter what I type as a password it always goes to CorrectPass whats up with this did I do some bad scripting?

    Thanks
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Would help if you surrounded your CODE in the CODE TAGS.

    Would need to know what is in the CP1 file. Because you are using Tokens=1* and not using a DELIMS option, anything after the first delimiter it finds will go to %%B. So if this is a password file and someone puts a space in their password it will never match.

    Doesn't look like you are showing us all your code either. I don't see the CorrectPass or WrongPass function. Would help to know where they are in the script.

    I am trying to understand the point of this script. Looks like you are just trying to create some pseudo security. If you want security for a USB drive there are plenty of great third party utilities out there that offer REAL security for USB drives.
     
  5. Psycoblast

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    Ok I'm sorry if I've been annoying or somthing I'll try to go over everything I need help on and I'll put down my whole cod efor you.

    Well my first problem is the Find(str) command if I have all the users entered like this in User.dat:

    Psycoblast
    Admin
    Administrator

    And thats all there is in the whole folder the problem is the find (or findstr) when it searchs for the variable %user% and lets say I set it to Psyco it will accept it as a user because its part of the word how do I make it search the whole line? Oh yeah I do have /I switch after the findstr command.

    My second problem is f token commands searching for password for each user, every user made by the administrator (Not quite there yet.) gets there own folderso they all have there own password in P1.dat so will that problem from your last post still apply?

    Finally here is the full code so far:

    Code:
    @echo off
    :start
    For /F "Tokens=1* Delims=:" %%A in (%drive%\check\Admin.dat) Do set CurrentAdmin=%%A
    title Cole's Login ::: Made by PsycoZL
    set drivel=0
    IF Exist A:\check\check.dat set drivel=A:
    IF Exist B:\check\check.dat set drivel=B:
    IF Exist C:\check\check.dat set drivel=C:
    IF Exist D:\check\check.dat set drivel=D:
    IF Exist E:\check\check.dat set drivel=E:
    IF Exist F:\check\check.dat set drivel=F:
    IF Exist G:\check\check.dat set drivel=G:
    IF Exist H:\check\check.dat set drivel=H:
    IF Exist I:\check\check.dat set drivel=I:
    IF Exist J:\check\check.dat set drivel=J:
    IF Exist K:\check\check.dat set drivel=K:
    IF Exist L:\check\check.dat set drivel=L:
    IF Exist M:\check\check.dat set drivel=M:
    IF Exist N:\check\check.dat set drivel=N:
    IF Exist O:\check\check.dat set drivel=O:
    IF Exist P:\check\check.dat set drivel=P:
    IF Exist Q:\check\check.dat set drivel=Q:
    IF Exist R:\check\check.dat set drivel=R:
    IF Exist S:\check\check.dat set drivel=S:
    IF Exist T:\check\check.dat set drivel=T:
    IF Exist U:\check\check.dat set drivel=U:
    IF Exist V:\check\check.dat set drivel=V:
    IF Exist W:\check\check.dat set drivel=W:
    IF Exist X:\check\check.dat set drivel=X:
    IF Exist Y:\check\check.dat set drivel=Y:
    IF Exist Z:\check\check.dat set drivel=Z:
    IF %drivel%==0 goto USBError
    For /F "Tokens=1* Delims=:" %%A in (%drivel%\check\check.dat) Do set En-Dis-Able=%%A
    goto Login1
    :USBError
    cls
    echo Sorry USB Was Not Found
    echo.
    echo Please Fix Connection and Try Again
    echo.
    echo.
    echo Press Any Button To Exit
    pause >nul
    Exit
    IF %En-Dis-Able%==false goto AdminDisabled
    IF %En-Dis-Able%==False goto AdminDisabled
    Exit
    :Login1
    cls
    echo Please Enter Username
    echo.
    set /p "user=>"
    cls
    IF "%user%"==A goto UserError
    IF "%user%"==B goto UserError
    IF "%user%"==C goto UserError
    IF "%user%"==D goto UserError
    IF "%user%"==E goto UserError
    IF "%user%"==F goto UserError
    IF "%user%"==G goto UserError
    IF "%user%"==H goto UserError
    IF "%user%"==I goto UserError
    IF "%user%"==J goto UserError
    IF "%user%"==K goto UserError
    IF "%user%"==L goto UserError
    IF "%user%"==M goto UserError
    IF "%user%"==N goto UserError
    IF "%user%"==O goto UserError
    IF "%user%"==P goto UserError
    IF "%user%"==Q goto UserError
    IF "%user%"==R goto UserError
    IF "%user%"==S goto UserError
    IF "%user%"==T goto UserError
    IF "%user%"==U goto UserError
    IF "%user%"==V goto UserError
    IF "%user%"==W goto UserError
    IF "%user%"==X goto UserError
    IF "%user%"==Y goto UserError
    IF "%user%"==Z goto UserError
    IF "%user%"==%CurrentAdmin% goto skip3
    IF "%user%"==Admin goto skip3
    IF "%user%"==Administrator goto skip3
    IF "%user%"==Admin goto skip3
    IF "%user%"==admin goto skip3
    IF "%user%"==administrator goto skip3
     
    IF %En-Dis-Able%==false goto AdminDisabled
    IF %En-Dis-Able%==False goto AdminDisabled
     
    :skip3
    Findstr /I /X "%user%" "%drivel%\Check\Data\Users.dat" >nul 
    If %errorlevel%==0 (
    goto FindP1
    )
    goto UserError
     
    :FindP1
    cls
    echo Please Wait
    ping localhost -n 1 >nul
    cls
    echo Please Wait.
    ping localhost -n 1 >nul
    cls
    echo Please Wait..
    ping localhost -n 1 >nul
    cls
    echo Please Wait...
    ping localhost -n 1 >nul
    cls
    echo Please Wait
    ping localhost -n 1 >nul
    cls
    echo Please Wait.
    ping localhost -n 1 >nul
    cls
    echo Please Wait..
    ping localhost -n 1 >nul
    cls
    echo Please Wait...
    ping localhost -n 1 >nul
    cls
    echo Please Wait
    ping localhost -n 1 >nul
    cls
    echo Please Wait.
    ping localhost -n 1 >nul
    cls
    echo Please Wait..
    ping localhost -n 1 >nul
    cls
    echo Please Wait...
    ping localhost -n 1 >nul
    IF %user%==%CurrentAdmin% goto AdminP1
    IF %user%==Admin goto AdminP1
    IF %user%==Administrator goto AdminP1
    IF %user%==Admin goto AdminP1
    IF %user%==admin goto AdminP1
    IF %user%==administrator goto AdminP1
     
    IF Exist "%drivel%\%user%'s Folder\" goto skip1
    attrib -h -s "%drivel%\%user%.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ren "%drivel%\%user%.{21EC2020-3AEA-1069-A2DD-08002B30309D}" "%user%'s Folder"
    ping localhost -n 2 >nul
    :skip1
    IF Exist "%drivel%\%user%'s Folder\~Sys~\" goto skip2
    attrib -h -s "%drivel%\%user%'s Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ren "%drivel%\%user%'s Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}" "~Sys~"
    ping localhost -n 2 >nul
    :skip2
    For /F "tokens=1*" %%A in ("%drivel%\%user%'s Folder\~Sys~\Logs\P1.dat") Do set CP1=%%A
    pause 
    ren "%drivel%\%user%'s Folder\~Sys~" "~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    attrib +h +s "%drivel%\%user%'s Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ping localhost -n 2 >nul
    ren "%drivel%\%user%'s Folder" "%User%.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    attrib +h +s "%drivel%\%user%.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ping localhost -n 2 >nul
     
    cls
    echo %user% Please Enter Password
    echo.
    set /p "P1=>"
    IF "%P1%"=="%CP1%" goto CorrectPass
    IF NOT "%P1%"=="%CP1%" goto WrongPass
    exit
     
     
     
    :UserError
    cls
    echo Sorry %user% isn't a current user.
    echo.
    echo If you would like to have an account Please talk to Administrator of USB.
    echo.
    echo Current Administrator is %CurrentAdmin%
    echo.
    echo.
    echo Press Any Button to Exit
    pause >nul
    exit
    :AdminDisabled
    cls
    echo Sorry All Accounts have been disabled by Admin
    echo.
    echo To unlock please talk to Administrator
    echo.
    echo.
    echo Current Administrator is %CurrentAdmin%
    echo.
    echo.
    echo.
    echo Press Any Button To Exit
    pause >nul
    exit
    :CorrectPass
    echo Correct Password ::: Congrats
    pause >nul
    exit
    :WrongPass
    echo Correct Password ::: Congrats
    pause >nul
    exit
     
    :AdminP1
    IF Exist "%drivel%\Administrator Folder\" goto skip1
    attrib -h -s "%drivel%\Administrator.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ren "%drivel%\Administrator.{21EC2020-3AEA-1069-A2DD-08002B30309D}" "Administrator Folder"
    ping localhost -n 2 >nul
    :skip1
    IF Exist "%drivel%\Administrator Folder\~Sys~\" goto skip2
    attrib -h -s "%drivel%\Administrator Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ren "%drivel%\Administrator Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}" "~Sys~"
    ping localhost -n 2 >nul
    :skip2
    For /F "tokens=1,2* Delims=!" %%A in ("%drivel%\Administrator Folder\~Sys~\Logs\P1.dat") Do set CP1=%%A & set CP2=%%B
    echo %CP1%
    echo %CP2%
    pause >nul
    cls
    ren "%drivel%\Administrator Folder\~Sys~" "~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    attrib +h +s "%drivel%\Administrator Folder\~Sys~.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ping localhost -n 2 >nul
    ren "%drivel%\Administrator Folder" "Administrator.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    attrib +h +s "%drivel%\Administrator.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
    ping localhost -n 2 >nul
    cls
    echo Please Enter 1st Password
    echo.
    set /p "P1=>"
    IF "%P1%"=="%CP1%" goto AdminP2
    IF NOT "%P1%"=="%CP1%" goto WrongFirstAdminPass
    exit
     
    :AdminP2
    cls
    echo Please Enter 2nd Password
    echo.
    set /p "P2=>"
    IF "%P2%"=="%CP2%" goto AdminControl
    IF NOT "%P2%"=="%CP2%" goto WrongSecondAdminPass
    exit
    :AdminControl
    cls
    echo Not Finished
    pause >nul
    exit
    :WrongFirstAdminPass
    cls
    echo Sorry Your Password Is Wrong.
    echo.
    echo All information will be recorded for security use.
    echo.
    echo.
    echo If there are any problems please contact Administrator.
    echo.
    echo.
    echo Current Administrator is %CurrentAdmin%
    echo.
    echo.
    echo Press Any Button to Exit
    pause >nul
    exit
    :WrongSecondAdminPass
    cls
    IF WrongP2=1 set /a WrongP2=%WrongP2%+1 & goto skip4
    set WrongP2=1 
    :skip4
    echo Sorry Your Password Is Wrong.
    echo.
    echo All information will be recorded for security use.
    echo.
    echo.
    echo If there are any problems please contact Administrator.
    echo.
    echo.
    echo Current Administrator is %CurrentAdmin%
    echo.
    echo.
    echo Press Any Button to Try one More Time
    pause >nul
    IF %WrongP2%==2 exit
    goto AdminP2
    
    P.S. I know theres more then just the Control Panel special number, For ~Sys~ I'm acctually using the Recycle Bin one I just don't want to show it if your wondering.
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
     
  7. Psycoblast

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    In P1 file I have:

    password!penguin
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    So there full password is: password!penguin

    If the only thing in the CP1 file is their password a better way to get the password into a variable would be to use the set command.
    Code:
    set /p =<P1.dat
    Remember that if you are using delayed expansion anywhere using exclamations is going to wreak havoc and crash your batch file if their are exclamations in your data.
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    anything wrong with this?
    Code:
    :CorrectPass
    echo Correct Password ::: Congrats
    pause >nul
    exit
    :[color=red]WrongPass[/color]
    echo [color=red]Correct[/color] Password ::: Congrats
    pause >nul
    exit
     
  10. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Code:
    For /F "tokens=1*" %%A in ("%drivel%\%user%'s Folder\~Sys~\Logs\P1.dat") Do set CP1=%%A
    Why aren't you using the delims option?

    You are in your other for loop for the administrator. But even that one is coded wrong as well.
     
  11. Psycoblast

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    Hmm.. I really like the set idea it never even passed through my mind to do that.

    Yeah I noticed the Correct Pass error after I posted it thanks for the close eye.

    for the F tokens the Admin gets two passwords and I use this for Admin:

    For /F "tokens=1,2* Delims=!" %%A in ("%drivel%\Administrator Folder\~Sys~\Logs\P1.dat") Do set CP1=%%A & set CP2=%%B

    Normal Users get one password thats where this came in:

    For /F "tokens=1*" %%A in ("%drivel%\%user%'s Folder\~Sys~\Logs\P1.dat") Do set CP1=%%A

    Do I need to use the delims option for one password being set?

    And I think what I'll do for the admin two password thing is make a P1.dat and a P2.dat

    Thanks for all your patentice my friend and I both apperciate it. :D(y)
     
  12. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    This code shouldn't even work.
    Code:
    For /F "tokens=1,2* Delims=!" %%A in ("%drivel%\Administrator Folder\~Sys~\Logs\P1.dat") Do set CP1=%%A & set CP2=%%B
    You can leave both passwords on one line but I would pick a more standard delimiter then an exclamation. You are just asking for trouble by doing that. You aren't using delayed expansion so I guess it really doesn't matter but still isn't best practice. Since you have two passwords on one line you can change your tokens to 1*. Everything after the first delimiter will go to the 2nd variable which will be %%B. Since your path has spaces in it, you need to use the USEBACKQ option. I am also a big fan of readability so I like to format my For Loops this way.
    Code:
    For /F "usebackq tokens=1* Delims=!" %%A in ("%drivel%\Administrator Folder\~Sys~\Logs\P1.dat") Do (
    set CP1=%%A
    set CP2=%%B
    )
    If your normal users only have one password then just use the set statement. Parsing the file in a For Loop is extra overhead you do not need.
    Code:
    set /p CP1=<P1.dat
    You should read the help file for the IF statement and you will probably realize you have a lot of redundant code and also instances of input that you will never catch.

    Not sure why you are using all those IF statements for a single character user name.. Why don't you just compare to the user file. Plus the way you have it coded won't match anything and it doesn't check if someone entered in a number or special character. Later on you are trying to use the FINDSTR command to see if the user name matches in the user file. Why aren't you just using a for loop to parse through it and then compare the entered in user name to the ones in the user file. Seems much more straight forward.

    You do realize that these two lines of code will never be executed. Plus it is redundant code. You need to read the help file for the IF statement.
    Code:
    Exit
    IF %En-Dis-Able%==false goto AdminDisabled
    IF %En-Dis-Able%==False goto AdminDisabled
    You should also look at the help file for the ping cmd. Twelve separate ping statements are not needed to do what you are trying to accomplish.

    I can go on and on about what is wrong with all your code but I know TheOutCaste has already picked it to shreds and will be posting his response shortly. I think you will be extremely humbled by his knowledge of batch scripting. I wish I was as advanced in batch as him.
     
  13. Psycoblast

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    Thank you so much I've always had so much trouble with F tokens this new code of yours has opened so many more possibilities, Thanks so much.

    And to answer your question the reason I was using all the If commands for the single letters was because I was trying to find away to fix the Findstr command because when it searched the data file it could find the beggining of a word for example lets say the only user name in the users.dat file is Psycoblast if I use the find command and search for Psyco it finds even though thats not the full user name. I was using the find command because I could never get the F tokens to work thanks so much with your help I was able to fix everything with just that one f tokens code you gave me.


    Thanks so much, I'm really sorry for all the stress and confusion I must of caused I'm going to be able to finish up all the rest of this USB Lock\Login System.

    Thanks Again

    -Psycoblast
     
  14. Psycoblast

    Psycoblast Thread Starter

    Joined:
    Aug 1, 2009
    Messages:
    45
    Oh yeah before I mark it solved I'll wait for what you guys have to say, and I'll post the new code again.
     
  15. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    Quite a project. Here's a bunch of comments/suggestions for you to wade through.

    I'd suggest you use a consistent capitalization scheme. Capitalizing the first letter of commands and variables is easier for most people to read than having it all lower case. Using all caps for commands is often used too, just being consistent looks better.
    .
    Line 3 - the Drive variable isn't defined before this line is executed. It needs to be defined somehow or you'll get an error. I'm guessing this is used to be able to make any User the Admin
    The Tokens=1* construct. Data to the left of the first delimiter (:) will be assigned to %%A. Everything to the right will be assigned to %%B. You don't use %%B so no need to specify it.
    Same in Line 33.
    To check the drive letter, a For loop would be faster. The If statements take over 1 second the first time run, then .04 seconds after that on my system regardless of the drive letter. A For loop takes .02 seconds if the drive is drive Z.
    What if there are two USB drives plugged in? Do you want to use the first one found, or the last one? Or check to see if more than one is connected?

    If you use a goto to jump to a section to print an error message, best to put it at the end (or beginning) of the file with other similar sections. In this case, you use a goto if the drive letter isn't found, then have to use a goto to go around that section after reading the En-Dis-Able value. Or, you can change the If statement for the drive letter to skip the error message if the letter is found. This is mainly stuff to find and streamline after troubleshooting, but if you have a structure to follow from the start you can avoid the multiple gotos in the first place.

    lines 45, 46, and 47 will never be executed.

    lines 54-79 - If you are only going to accept user names that are in the file, there's no point in checking if the user entered a single letter for a user name. check the name first, if the name isn't in the file it's an error.

    Note that none of these If statements will ever be true; you have quotes on the left side but none on the right, so it's impossible for the user to enter anything that would match.

    Lines 80-89 can be streamlined. You can use /I to make the comparison case insensitive instead of using two statements. Plus if they enter ADMIN the check won't see it as a valid Admin username, but the check later on would find it as a valid user name.

    Lines 98-133 You spend 12 seconds printing Please Wait to the screen. There is nothing going on in the background so I don't see the point of making the user wait:confused:.

    Lines 141-157 and 206-214 - If you assign the GUID to a variable, it makes it easier to read, and much easier to change if needed, as you only have to edit one line. You should avoid using the same label in different locations. It works now, but might not in the future, and if you move sections around you can end up executing the wrong section as it searches from the current location to the end of the file, then wraps around looking for a lable until it finds a match.

    Line 151. With For /F if you need to quote a file name, you have to use the Usebackq option, or the file name will be seen as a literal string. The other option is to use the Type command. Also, there is no delimiter specified, so the default of space and tab will be used. If the data in the file contains a space or tab, it may not match what you want to match. Also, you are using only one token, so the 1* isn't needed. You can use "Usebackq Tokens=*" or "Usebackq Tokens=1 Delims=" in this case, they are equivalent.

    Line 216. set CP1=%%A & set CP2=%%B
    Notice the space after A and before &. This space will be added to the value, so you will never be able to match that with anything typed in. Trailing spaces at the end of a line are ignored, such as when using Set /P, but this is not at the end of the line. You can remove the space, or use parentheses to define the end of the statement:
    set CP1=%%A& set CP2=%%B
    (set CP1=%%A) & set CP2=%%B

    Or put the Set statements on separate lines as Squashman shows above. All three methods are equivalent.
    When testing, always helps to bracket the variables you echo so you can catch stuff like this:
    Echo.==%CP1%==
    Echo.==%CP2%==
    This will help you spot extraneous spaces that can sneak in.

    For loops often require using Delayed Expansion, so always a good idea to avoid using ! in text as it may be seen as the start of a variable name, and they (and the following text) may be removed from the text. You might add something 6 months from now that needs to use Delayed Expansion and then have to change that delimiter, as well as tracking down that that is the problem in the first place.

    Some of the Echoed text is repetitive, that can be replaced with a goto to a shared section to shorten the code and make it consistent.

    Line 266 you haven't defined the WrongP2 variable first, and left off the % symbols. And one of my favorite typos, only one = instead of two, I do that all the time.

    Looks like you have two formats for the P1 file.
    Administrator is <password1>!<password2>
    Users are just <password>
    Admin.dat appears to be <CurrentAdminName>:<something>
    User.dat is just <UserName>

    If you make the file formats the same, you can use the same routine to rename the files and read the password instead of having the same code duplicated. The only difference is one name is Administrator, and the other is %user%'s
    Admin and Administrator use the same folder, so the password has to be the same for both with the way things are written. Are these actually separate accounts with different passwords, or is Admin just a shortcut for Administrator?

    If they are the same, be easier to just set an admin flag then set the users variable to Administrator. Then you can use the same routine to rename the folders and read the passwords. Then use that flag to see if you need to check two passwords or one. You can assume there are two passwords and use a for loop to read two from the file even if there is only one. For the User P1.dat file the CP2 variable would be undefined. This is where the Admin flag would come in, the CP2 variable wouldn't be checked for a User, only for an Admin.
    You could setup the users.dat file like this:
    Psycoblast:User
    Admin:Admin
    Administrator:Admin
    Fred:User

    Then any account could be used as an admin, and you could use the CurrentAdmin name as the contact admin, while allowing multiple accounts to be Admin accounts. Of course anyone who can modify the users.dat file could make their user name an admin, so hopefully the USB drives are formatted NTFS so limited accounts can be prevented from changing the users.dat file.

    And anyone who can run the batch file can read the P1.dat files and see the passwords, so there's no real security here.

    I don't see the point of adding a GUID to the folder names. On Win 7, Vista or XP all it does is make a longer folder name, it's not treated special in any way. Using the Recycle Bin GUID changes the Icon, but you can do that with a Desktop.ini file and not have to mess with renaming the folder. The constant renameing and changing attributes (which isn't needed) will also increase the wear on a Flash drive.

    The following includes most of the above suggestions. I removed all the renaming and changing attributes for the User folder, but left it in for the Administrator folder.
    Take a look and see what you think:
    Code:
    @Echo Off
    :Start
    For /F "Usebackq Tokens=1 Delims=:" %%A In ("%drive%\check\Admin.dat") Do Set CurrentAdmin=%%A
    Title Cole's Login ::: Made by PsycoZL
    :: If this is run from the USB drive, this will get the drive letter
    ::Set drivel=%~d0
    :: If run from the hard drive use this to find the USB drive letter.
    :: This will find the first drive only if more than one connected.
    For %%I In (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) Do If Exist "%%I:\check\check.dat" Set drivel=%%I:&Goto Chk1
    Goto USBError
    :Chk1
    Set GUID={21EC2020-3AEA-1069-A2DD-08002B30309D}
    If Not Exist "%drivel%\check\check.dat" Goto USBError
    For /F "Usebackq Tokens=1 Delims=:" %%A In ("%drivel%\check\check.dat") Do Set En-Dis-Able=%%A
    :Login1
    Cls
    Echo.Please Enter Username
    Echo.
    Set /P user=^>
    >Nul Findstr /I /X "%user%" "%drivel%\Check\Data\Users.dat"
    If %errorlevel%==1 Goto UserError
    If /I "%user%"=="Admin" Goto AdminP1
    If /I "%user%"=="Administrator" Goto AdminP1
    If "%user%"=="%CurrentAdmin%" Goto AdminP1
    If /I "%En-Dis-Able%"=="false" Goto AdminDisabled
    
    For /F "Usebackq Tokens=*" %%A In ("%drivel%\%user%'s Folder\~Sys~\Logs\P1.dat") Do Set CP1=%%A
    
    Cls
    Echo.%user% Please Enter Password
    Echo.
    Set /P P1=^>
    If NOT "%P1%"=="%CP1%" Goto WrongPass
    Goto CorrectPass
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :AdminP1
    If Exist "%drivel%\Administrator Folder\" Goto Askip1
    Attrib -H -S "%drivel%\Administrator.%GUID%"
    Ren "%drivel%\Administrator.%GUID%" "Administrator Folder"
    Ping localhost -N 2 >Nul
    :Askip1
    If Exist "%drivel%\Administrator Folder\~Sys~\" Goto Askip2
    Attrib -H -S "%drivel%\Administrator Folder\~Sys~.%GUID%"
    Ren "%drivel%\Administrator Folder\~Sys~.%GUID%" "~Sys~"
    Ping localhost -N 2 >Nul
    :Askip2
    For /F "Usebackq Tokens=1,2 Delims=!" %%A In ("%drivel%\Administrator Folder\~Sys~\Logs\P1.dat") Do (Set CP1=%%A)&Set CP2=%%B
    Echo.==%CP1%==
    Echo.==%CP2%==
    >Nul Pause
    Cls
    Ren "%drivel%\Administrator Folder\~Sys~" "~Sys~.%GUID%"
    Attrib +h +S "%drivel%\Administrator Folder\~Sys~.%GUID%"
    Ping localhost -N 2 >Nul
    Ren "%drivel%\Administrator Folder" "Administrator.%GUID%"
    Attrib +h +S "%drivel%\Administrator.%GUID%"
    Ping localhost -N 2 >Nul
    Cls
    Echo.Please Enter 1st Password
    Echo.
    Set WrongP2=0
    Set /P P1=^>
    If NOT "%P1%"=="%CP1%" Goto WrongFirstAdminPass
    :AdminP2
    Cls
    Echo.Please Enter 2nd Password
    Echo.
    Set /P P2=^>
    If NOT "%P2%"=="%CP2%" Goto WrongSecondAdminPass
    :AdminControl
    Cls
    Echo.Not Finished
    Goto End
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ::                 Error Displays 
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :USBError
    Cls
    Echo.Sorry USB Was Not Found
    Echo.
    Echo.Please Fix Connection and Try Again
    Goto PressAny
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :UserError
    Cls
    Echo.Sorry %user% isn't a current user.
    Echo.
    Echo.If you would like to have an account Please talk to Administrator of USB.
    Call :CA
    Goto PressAny
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :AdminDisabled
    Cls
    Echo.Sorry All Accounts have been disabled by Admin
    Echo.
    Echo.To unlock please talk to Administrator
    Call :CA
    Goto PressAny
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :CorrectPass
    Echo.Correct Password ::: Congrats
    Goto End
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :WrongPass
    Echo.Wrong Password ::: Too Bad
    Goto End
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :WrongFirstAdminPass
    Call :WrongAdmin
    Goto PressAny
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :WrongSecondAdminPass
    Set /A WrongP2+=1
    Call :WrongAdmin
    If %WrongP2%==2 Goto End
    Echo.
    Echo.
    Echo.Press Any Button to Try one More Time
    Call :End
    Goto AdminP2
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :WrongAdmin
    Cls
    Echo.Sorry Your Password Is Wrong.
    Echo.
    Echo.All information will be recorded for security use.
    Echo.
    Echo.
    Echo.If there are any problems please contact Administrator.
    :CA
    Echo.
    Echo.
    Echo.Current Administrator is %CurrentAdmin%
    Goto :EOF
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :PressAny
    Echo.
    Echo.
    Echo.Press Any Button to Exit
    :End
    >Nul Pause
    Goto :EOF
     
  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!

Thread Status:
Not open for further replies.

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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice