如何让我的批处理脚本为自己添加代码

时间:2016-06-08 13:29:36

标签: batch-file

我已经创建了一个只是基本登录屏幕的批处理脚本,但是,我想这样做,所以它在你注册后添加了一个代码(也就是它让你跳过注册)我想添加这个代码:< / p>

:alreadyregistered
goto login

然而,为了使其正常工作,我还需要它来记住您的用户名/密码。此脚本将此文本存储在文本文档中,但我不知道如何将您登录的用户名/密码与文档中的用户名/密码进行比较。 (像这样:if %username% == "what is in the notepad" the goto logged in if else goto error) 如果你能解决我的一些错误,那也很酷。我想这样做,如果在您的帐户成为“用户帐户”(获取系统错误5)时出错,则会将您带到accounterror部分。我尝试使用errorlevel但它不起作用。 (if ERRORLEVEL NEQ 0 goto accounterror)感谢您的帮助,因为我是初学者。

这是我的代码:

@echo off 
color fc
rem register
:register
title Register 
cls 
set /p username="Please type a username: "
echo username = %username% > logindetails.txt
powershell -Command $pword = read-host "Please Enter a Password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt
echo password = %password% >> logindetails.txt
powershell -Command $pword = read-host "Please Retype your Password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p confirmpassword=<.tmp.txt & del .tmp.txt
if %username% == %password% goto matcherror
if %confirmpassword% NEQ %password% goto passwordnotmatch
goto login1
rem if passwords don't match
:passwordnotmatch
title ERROR 
cls
(
   mshta javascript:alert^("Passwords do not match, try again"^);close^(^);
)
goto register
rem this is for credential matching
:matcherror
title ERROR
cls
(
   mshta javascript:alert^("Credientials cannot match, try again"^);close^(^);
)
goto register
rem username for login
:login1
title Login
cls
set /p usernamelogin="Please type your username: "

if %usernamelogin% == %username% goto login2
if %usernamelogin% NEQ %username% goto loginerror
rem this is incorrect username/password, will go back to username entering
:loginerror
title ERROR
cls
(
   mshta javascript:alert^("Incorrect username or password"^);close^(^);
)
goto login1
rem password for login
:login2
title Login
powershell -Command $pword = read-host "Please Enter your Password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p passwordlogin=<.tmp.txt & del .tmp.txt

if %passwordlogin% == %password% goto loggedin
if %passwordlogin% NEQ %password% goto loginerror
rem after login is complete
:loggedin
title Welcome %username%
cls

echo Hello %username%, what would you like to do?
echo 1) Make your account an actual user account
echo 2) Exit
set /p something=">"

if %something% == 1 goto work
if %something% == 2 exit
if not defined something (
(
   mshta javascript:alert^("Please enter a valid number"^);close^(^);
)
goto loggedin
)
rem make a user account using credentials from this file
:work
net user %username% %password% /add

(
   mshta javascript:alert^("Congratulations, your account %username% was created!"^);close^(^);
)
goto loggedin
rem error making account an actual user account
:accounterror
cls
(
   mshta javascript:alert^("Sorry, %username%, your account was not created due to an error"^);close^(^);
)
goto loggedin

1 个答案:

答案 0 :(得分:0)

  

(类似这样:如果%username%==&#34;记事本中有什么&#34;   goto登录如果否则转到错误

那将是:

set /p "EnteredName=Please enter Username: "
<file.txt set /p NameFromFile=
if %NameFromFile%==%EnteredName% (
  echo they are the same
) else ( 
  echo they are NOT the same
)

请不要将username用作变量名称,因为它是Windows使用的系统变量(例如datetime)。改变它可能会导致意想不到的结果。

  

我想添加此代码:

将代码附加到批处理文件很简单:只需附加它:

(echo :alreadyregistered
echo goto login)>>%dpnx0

%~dpnx0代表 D 铆钉, P ath, N ame和e X 紧张当前正在运行的批处理文件