Help with batch!!

April 15, 2011 at 09:42:25
Specs: Windows 7
So I'm making a batch and I got stuck on a part (note: this is a login-register thing it's not really helpful but it's an experiment):

@echo off
:loginreg
cls
//a line of code should be here to test if a room exists if so it should goto login
goto register
:login
cls
echo Type "register" to register.
set /p username=Username:
if %username% == register goto register
set /p password=Password:
cls

if %user% == %pass% goto witch
goto incorrect
:incorrect
cls
echo Username or password is incorrect!
pause >nul
goto login
:witch
cls

:register
cls
set /p username=Username:
set /p password=Password:
set /p confrimpassword=Re-type password:
set /p name=Name:
cls
if %password% == %confrimpassword% goto continue
goto error
:continue
cls
//line of code here will test how many people are currently signed up and set it to total
set /a ID= %total% + 1
cls
//this will create a room just for that user.
echo :%ID% >> "self.bat"
echo cls >> "self.bat"
echo echo Welcome %name%! >> "self.bat"
echo pause >nul >> "self.bat"
echo exit >> "self.bat"
//the room will be alot longer but I didn't want to put all 37 lines that are inserted
cls
set usernamel=username
set passwordl=password
set userl=user
/*this part is the tricky part cause I want the lines to insert 13 lines from the top apposed to the bottom line*/
echo if %%%usernamel%%% == %username% set user=%ID% >> "self.bat"
echo if %%%passwordl%%% == %password% set pass=%ID% >> "self.bat"
//insert in the empty space in the witch room
echo if %%%userl%%% == %ID% goto %ID% >> "self.bat"
cls
echo Success!
echo Window will now close.
pause >nul
exit
:error
cls
echo %password% is not the same as %confrimpassword%.
echo Please try again.
pause >nul
goto register

This code is kind off long (i removed about 2/3 of it but left all the important stuff).

Thanks for taking the time to read this :)
Please help!

Please no super long codes that take like 20 lines to type... keep it simple...


See More: Help with batch!!

Report •


#1
April 15, 2011 at 18:31:39
that take like 20 lines to type

Do you really type it out? Copy and paste is your friend.

/*this part is the tricky part cause I want the lines to insert 13 lines from the top apposed to the bottom line*/

If you create two file you can use copy to insert the lines into a third:

> tempfile1 echo line1
>> tempfile1 echo line2
> tempfile2 echo line3
>> tempfile2 echo line4

copy /b tempfile1 + tempfile2 newfile

You should probably validate you input, what if someone enters something like this at the "password" prompt:

1==1 dir&echo typo's that contain "special" characters are more likely&pause&if 1


Report •

#2
April 16, 2011 at 08:48:17
What's the tempfile thing for? Well if they enter that password then they'll have to remember the whole thing... but if there's a way to make it 4-20 charectors then that'll be good to put in there.

Report •

#3
April 16, 2011 at 17:08:16
What's the tempfile thing for?

If you want to insert text at the top of a file you will need to rebuild it. That isn't just a batch problem. I just used the temp files as an example.

Well if they enter that password then they'll have to remember the whole thing...

That's not the point, the point is that something as simple as a typo can make the whole thing crash or even execute something unwanted. Simply type in the character > into one of the prompts, when you are using that variable the script will error out.


I thought the insert was the actual problem, now I'm not sure of what the problem even is, if I don't touch on it below can you please clarify what you need help with.


Set /p validation, the first thing is to check the variable isn't empty. Next is a check that the variable contains more than double quotes. Next strip out any double quotes.

At this point variable contains no double quotes, now it will test the input is at least 4 characters and then strip it to the first 20 chars.

:label
set /p variable=Input:
if not defined variable goto label
if "%variable:"=%"=="" goto :label
set "variable=%variable:"=%"

if "%variable:~3,1%"=="" goto label
set "variable=%variable:~0,20%"



Report •
Related Solutions


Ask Question