Creating a password protected batch file with a batch file

Status
Not open for further replies.

MattPwns

Beta member
Messages
1
Hey everyone, I'm new at programming at everything, but I'm trying to learn, so bear with me.

I created a batch file, for fun, to shut down a computer in 60 seconds, but creates a text file and a second password protected batch file to abort the shutdown.

This is the code I've used.

Code:
@ECHO OFF
MODE CON: COLS=70 LINES=6
TITLE Shutdown - By Matt
START C:\Windows\System32\shutdown.exe -s -t 60 -c "Shutting Down"
ECHO Warning! Your computer will shutdown in 60 seconds! > shutdown.txt
ECHO. >> shutdown.txt
ECHO To abort, run the "abort" batch file on your desktop. >> shutdown.txt
ECHO @echo off > abort.bat
ECHO MODE CON: COLS=20 LINES=5 >> abort.bat
ECHO title abort >> abort.bat
ECHO color 9B >> abort.bat
ECHO :Start >> abort.bat
ECHO cls >> abort.bat
ECHO set /p password="Password: " >> abort.bat
ECHO if "%password%"=="qwerty" goto :correct >> abort.bat
ECHO goto :Start >> abort.bat
ECHO :correct >> abort.bat
ECHO cls >> abort.bat
ECHO echo Password correct! >> abort.bat
ECHO C:\Windows\System32\shutdown.exe -a >> abort.bat
ECHO pause \nul >> abort.bat
ECHO exit >> abort.bat
START C:\Users\%USERNAME%\Desktop\shutdown.txt
COLOR 9B
ECHO.
ECHO --------------------------------------------------------------------
ECHO Run abort.bat and enter the password
ECHO Now press any key to delete the two files created by this batch file.
ECHO --------------------------------------------------------------------
ECHO.
PAUSE
DEL C:\Users\%USERNAME%\Desktop\shutdown.txt
DEL C:\Users\%USERNAME%\Desktop\abort.bat
EXIT

It shuts down properly, creates and opens the text file properly, but I run into problems with the second batch file.

This is the code it puts into the second batch file:

Code:
@echo off 
MODE CON: COLS=20 LINES=5 
title abort 
color 9B 
:Start 
cls 
set /p password="Password: " 
if ""=="qwerty" goto :correct 
goto :Start 
:correct 
cls 
echo Password correct! 
C:\Windows\System32\shutdown.exe -a 
pause \nul 
exit

The password portion of the file won't rune because it doesn't add
Code:
%password%
into it.

Does anybody know of a way to make it add that into the second batch file, or a second way of writing a password command that will work?

Thanks,
- Matt
 
Batch is definitely not a strong suit for me but it looks to me like the first batch file is resolving the %password% variable (which has no value) before writing it to the second file.
You'll need to escape the special character % so that the line looks like:
Code:
ECHO if "\%password\%"=="qwerty" goto :correct >> abort.bat
where '\' is your escape character. NOTE: I don't know that '\' is the correct escape character and I don't have a windows box to test on at the moment.
The other thing to try is single quotes so it looks like this:
Code:
ECHO if '%password%'=="qwerty" goto :correct >> abort.bat
 
this is so much easier in vbs.

save this code as shutdown.vbs and double click
Code:
'Access Windows Shell Functions
Set WshShell = WScript.CreateObject("WScript.Shell")

'Run shutdown command
wshshell.run("shutdown -s -t 60")

'Check to see if password entered is correct
do while password <> "qwerty"

  'Display password prompt to abort
  password = inputbox("Enter password to abort shutdown", "Abort?")

  'Check to see if password is correct
  if password = "qwerty" then

    'Run command to abort shutdown
    wshshell.run("shutdown -a")

  else

    'Display error message to user
    msgbox "The password you have entered is incorrect" 

  end if

'Check password again, continue if correct
loop

'Exit Windows Shell
set wshshell = nothing

i use the following site as a reference guide for vbs
DevGuru Home
 
Status
Not open for further replies.
Back
Top Bottom