How to Log Input With Batch Files





Introduction: How to Log Input With Batch Files

Are you creating a very dangerous batch file, and you want to know when people use the specified commands? This is the instructable for you!

Are you making a login-based AI that listens to your commands, and you want to know when people login? This is the instructable for you!

What you need:
your batch file put into a folder
nothing else in the folder
time to type a few lines of code.
You may have a shortcut somewhere else on your PC.

Step 1: How Do Add Logins.

This isn't originally my code (I did edit some of it), but here is the code to add logins:
If you can see,  when you sign up, it creates a file named %newname%.bat, and another named %newname%.logs.txt. %newname% is the username you give when you sign up. When it creates the .bat file, in it stores the username and the password. I created the logs.txt file, and in it it logs whenever you someone does a command in which it is programmed to record. Say, when you login, it will put into the logs: Logged in %time% %date%. These items will be placed into the same folder as your batch file, along with another folder named cmdacoBin. You can ignore this folder.

@echo off
echo [1] Log In
echo [2] Sign Up
echo [3] Exit
set /p op=
if %op%==1 goto 1
if %op%==2 goto 2
if %op%==3 goto 3
goto error
echo Sign Up
echo ======================================
set /p newname="Enter new username:"
if "%newname%"=="%newname%" goto inputname
cd "%userprofile%\documents"
if exist "cmdacoBin" goto skip
if not exist "cmdacoBin" goto noskip
md "cmdacoBin"
goto skip
cd "%userprofile%\documents\cmdacoBin"
if exist "%newname%.bat" goto namexist
if not exist "%newname%.bat" goto skip2
echo set realusername=%newname%> "%newname%.bat"
echo Signed Up: %time% %date%> "%newname%.logs.txt"
goto next
set /p pswd=Enter new Password:
if "%pswd%"=="%pswd%" goto inputpass
cd "%userprofile%\documents\cmdacoBin"
echo set password=%pswd%>> "%newname%.bat"
goto next1
echo The entered username already exists.
echo Press any key to return. . .
pause >nul
goto 2
echo ==================
echo Clevernot Accounts
echo ==================
echo Your account has been successfully created!
ping localhost-1>nul
goto Start
color 07
echo =========================
echo Clevernot Accounts Log In
echo =========================
Set /p logname=Username:
if "%logname%"=="%logname%" goto 2.1
set /p logpass="Password:"
if "%logpass%"=="%logpass%" goto login
cd "%userprofile%\documents\cmdacoBin"
if exist "%logname%.bat" goto call
if not exist "%logname%.bat" goto errorlog
call "%logname%.bat"
if "%password%"=="%logpass%" goto logdone
goto errorlog
color 0c
echo Username or Password incorrect.
echo Access denied.
pause >nul
goto home
goto LoggedIn

Step 2: How to Make the Batch File Record Commands.

It only records commands when you tell it to.
Say you wanted to record when a player Logs out, you can type (into your batch code)

echo Logged out %time% %date%>> "%logname%.logs.txt"

This would type into your file named (in example) HoweYouDrewin.logs.txt
In it it would say (in example) Logged out 4:09PM 02/13/2013

Make sure you put exactly as it says on top, or it will get screwed up.
if you put:

echo Logged out %time% %date%> "%logname%.logs.txt"

This code only has one (>) in it, and if you were to put only one in, it would wipe the whole text file, and start from scratch.

>=wipe, rebegin
>>=continue from where it left off

If you're confused, this is what it would look like in context:

@echo off
set /p logout= Would you like to log out?

if %logout% == yes goto Logout
if %logout% == no goto Start
goto Start
echo Logged out %time% %date%>> "%logname%.logs.txt"

Step 3: How to Add a Wrong Password Identifier.

if you want to give someone 3 chances to get a right password, the code would look like thi
(this is the code after the batch file determines it's the wrong password, if your actually looking for the identifier, find it on the first step)

You can see that after 3 failed attempts, the account will be deleted.
If the person manages to log in after (in example) 1 failed attempt, all of the files for the failed attempts (%logname%.wrongpass.txt)
will be deleted.

@echo off
echo Password Incorrect.
if exist "%logname%.wrongpass.txt" goto w3
if not exist "%logname%.wrongpass.txt" goto w2
echo First password failure occurred %time% %date%> "%logname%.wrongpass.txt"
ping localhost>nul
goto LogIn
if exist "%logname%.wrongpass2.txt" goto w5
if not exist "%logname%.wrongpass2.txt" goto w4
echo Second Password failure occurred %time% %date%> "%logname%.wrongpass2.txt"
ping localhost>nul
goto LogIn
if exist "%logname%.wrongpass3.txt" goto w7
if not exist "%logname%.wrongpass3.txt" goto w6
echo Third Password Failure occurred %time% %date%> "%logname%.wrongpass3.txt"
ping localhost>nul
goto LogIn
echo Your password has failed 3 times in a row. Your account will now be deleted.
del %logname%.bat
del %logname%.logs.txt
del %logname%.wrongpass.txt
del %logname%.wrongpass2.txt
del %logname%.wrongpass3.txt
echo Account deleted due to password failure %time% %date%>> "%logname%.del.txt"
ping localhost>nul
echo You have successfully logged In! All of your failed attempts (if any) will now be deleted.
del %logname%.wrongpass.txt
del %logname%.wrongpass2.txt
del %logname%.wrongpass3.txt
goto Whatever

Step 4: Conclusion

This is my first instructable, and it was made on a Mac so I don't really have the pictures of the batch coding or anything. If you have any questions or suggestions for better coding, please comment below. After all, I did only come up with the code in about an hour during class, so I didn't really work out the bugs for some of it yet.

Thanks for reading!



    • Epilog Challenge 9

      Epilog Challenge 9
    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Science of Cooking

      Science of Cooking

    We have a be nice policy.
    Please be positive and constructive.




    For more useful batch files visit

    What do you highlight? I just want the ablity of it making a file with username and password everytime I login or someone else logs in