trying to make an "OS" in batch - batch-file

#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
ping localhost -n 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date
cls
echo welcome to windows 71
echo before we bigen please type your name
set /P_name=here:
if %name%=admin goto admin
if not goto ms
rem ms=menu start
:ms
echo %time%
echo hello %name%
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
if not goto wc
rem wc=wrong command
:admin
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
So the problem is that it crashes after the :LOGIN part and I don't know what to do!
I'm trying to make an OS batch (something like MS-DOS), but it crashes after the "login" part.
I tried everything I could think of and it didn't work, also I want to make a save file so users can set a password for their "account".

As mentioned in above comments, you need to correctly use your variables, you can however use choice instead of set /p for your commands.
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
timeout /t 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date /t
timeout /t 2>nul
cls
echo welcome to windows 71
echo Before we begin please type your name
set /P "_name=here:"
if /i "_%name%"=="admin" goto admin
rem ms=menu start
:ms
echo %time%
echo hello %_name%
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:admin
echo hello %_name% to the admin panel
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:opt1
echo Help stuff goes here
goto :eof
:opt2
exit
:opt3
echo Calenders stuff goes here
Some things to note. You do not require to goto ms is the user is not admin as the statement for not being admin will not be met, we will automatically faal through into the ms label.
Notice where the problems were in your code. i.e if %name%=admin should be if "%_name%"=="admin" with double equal sign and the underscore in the name. It is also double quoted to ensure that we do a match without unwanted whitespace. Lastly /I option to catch ADMIN in any case.
See if /?, choice /? from command line for more help around these functions.

Okay this code is quite wrong.
I fixed it.
#echo off
:load
rem imitation of loading the os
color 70
title boot
echo please wait...
ping localhost -n 3 >nul
cls
rem in here user types name he wants to be his account name
:login
title login
cls
echo Welcome to Microsoft Windows 7!
echo Before we begin, please type your name.
set /p name=here:
if "%name%"=="admin" goto admin
if not "%name%"=="admin" goto ms
rem ms=menu start
:ms
echo %time%
echo Hello %name%
echo Type HELP for list to commands
set /p command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
goto ms
rem wc=wrong command
:admin
CLS
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
GOTO :ADMIN
Okay but you don't need to start systeminfo and all that.

Related

How do I JUST replace text in a file (Like all of the text)

I've been searching for a while now and Google's terrible algorithm gives me everything but the aching question I've been asking. I'm trying to create a sort of password lock script, and I'm trying to create a lock out function. Here's what I have so far:
#echo off
set tries=3
:type
set /p mytextfile=< zepassword.txt
if %mytextfile%==LOCKED goto locked
echo Please type in your key.
set /p okay=
if %okay%==yes goto good
if not %okay%==yes goto bad
:good
cls
echo Correct Password.
pause >nul
exit
:bad
cls
set /a tries=%tries%-1
echo This key is incorrect. %tries% tries remaining.
pause >nul
if %tries%==0 echo LOCKED >> zepassword.txt
goto type
:locked
cls
echo You are LOCKED OUT.
echo Press any key to unlock.
pause >nul
echo UNLOCKED >> zepassword.txt
pause >nul
SomethingDark answered this question for me.
echo text > [File name]

How do I create an update function in Batch?

I would like to create an update function that works just after the user is prompted to type into the "ChatBox" so that other users on my school network can type and all other users can see it without having to restart the program or typing space to reload the .txt file
here is the code I have written so far;
:enter
cls
type cblog.txt
echo.
set /p text=
echo %text% >> cblog.txt
goto enter
I like this topic, so I wrote a fully working prototype:
#echo off
setlocal
if "%~1" equ "" echo You must give your username as parameter & goto :EOF
set "user=%~1"
set "now=%time%"
echo %now%: User %user% entered the chat room>> msgQueue.txt
call :chatRoom 3< msgQueue.txt
goto :EOF
:chatRoom
rem Omit previous messages
:omitMsg
set /P "msg=" <&3
if "%msg:~0,12%" neq "%now%:" goto omitMsg
echo %msg%
echo/
echo Press S to send a message or eXit to end
echo/
:msgLoop
rem Check for new messages received
:showMsg
set "msg="
set /P "msg=" <&3
if not defined msg goto send
echo %msg%
goto :showMsg
rem Check to send a new message
:send
ver > NUL
choice /C SNX /N /T 3 /D N > NUL
if errorlevel 3 goto end
if errorlevel 2 goto msgLoop
rem Send a message
echo -----------------------------------
set /P "msg=Message: "
echo -----------------------------------
echo %user%: %msg% >> msgQueue.txt
goto msgLoop
:end
echo %time%: User %user% leaved the chat room>> msgQueue.txt
The response time may be adjusted in /T 3 parameter of choice command: shorter times makes the chat more responsive, but it consume more CPU time.
Below is an image that show a test with four users in the Chat Room:

Set /P not capturing input in IF statement

Everything in this batch script works fine, but when I enter the IF statement, for some reason set /p id= doesn't actually capture anything. In fact shortly after it will echo:
You chose session %id%.
but that will return a blank, as though nothing was entered for ID.
Any advice would be greatly appreciated.
#echo off
echo Please be sure CMD is being run as an administrator.
echo.
:loop
set /p targetpc="Which PC would you like to query for users (Hostname or IP)?: "
echo.
echo Querying %targetpc%...
echo.
quser /server:%targetpc%
echo.
set /p choice="Would you like to log a user off of %targetpc%? [Y/N]: "
echo.
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session %id%.
echo.
logoff %id% /server:%targetpc% /V
echo.
echo Done!
echo.
goto loop
)
IF /I "%choice%" EQU "N" (
goto loop
)
You are using the %id% value within the same block where it is set. This being the case, you need to use delayed expansion.
Add these lines to the top and bottom of your script, respectively:
SETLOCAL EnableDelayedExpansion
<Your script>
ENDLOCAL
Now use delayed expansion notation with your block (! around variables instead of %):
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session !id!.
echo.
logoff !id! /server:%targetpc% /V
echo.
REM Note, replacing with a period here.
echo Done.
echo.
goto loop
)
There are tons of other questions on this site regarding delayed expansion, so a quick search within the batch-file tag should yield lots of additional info if you need it.

Batch File Game Accounts

I want to make a batch adventure game (not text-based adventure) that gives you options and lets you choose to progress the story.
Problem is I don't know how to save or load progress in the game and I want to make (possibly) multiple accounts you can access to start where you left off.
I know it's possible, since I've seen it in a batch game (who's file I lost). So, anyone know to make it possible?
#echo off
:menu
cls
echo 1.Sign in
echo 2.Register
echo 3.Exit
echo.
set /p input=What would you like to do:
if %input%==1 goto log
if %input%==2 goto reg
if %input%==3 goto exit
goto menu
:reg
cls
set /p user=Enter your desired username:
set /p pass="Enter your desired password:
echo %pass% >> %user%.txt
:: This will prompt for a username and password and then
:: output the user variable as the name of a .txt file that contains the
:: password
goto menu2
:log
set /p user="Enter your username: "
set /p pass="Enter your password: "
set /p password=<%user%.txt
pause >nul
:: This will check if the password entered is equal to the password in
:: the .txt file
if %pass% equ %password% goto menu2
goto menu
:menu2
::Enter your script here
The system is much shorter when you get rid of the comments. :)
Later on in your game you could use the same system as used in the :reg label to create a batch file with player stats and maybe an inventory in it such as set /a armor=2 and set /a sword=1 then call on that file when you want to show the player their statistics/inventory. Just an idea... Anyway, hope this helps at least a little bit.
Im making a game and this is the login signup this is part of the code
#echo off
color f
:logsign
title whattodo?
cls
echo What will you do?
echo.
echo 1) Login
echo 2) Sign in
echo 3) Exit
echo.
set /p web=Type 1 or 2?
if "%web%"=="1" goto login
if "%web%"=="2" goto signup
if "%web%"=="3" exit
:notvalid
echo you're username is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:notvalid1
echo you're password is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:signup
title Sign Up
cls
echo What will be your username?
echo.
set /p username=Username:
echo %username% >%username%.bat
cls
echo What will be your password?
echo.
set /p password=Password:
echo %password%>>%username%.bat
cls
echo Go back to log in menu then log in.
pause
cls
goto logsign
:login
title login
cls
echo Let's start with name.What is it?
echo.
set /p username=Type username:
if not exist %username%.bat (goto notvalid)
pause
cls
echo Now what is your password?
echo.
set /p password=Type password:
if not exist %username%.bat (goto notvalid1)
pause
goto home
The sign up saves the user and pass in a .bat file
possibly you could save stats to a file named after the player profile and read off of that file to see what level they where on look around over here.How to read file contents into a variable in a batch file?

Batch opening wrong file type

So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona

Resources