首页 > 解决方案 > 如何制作一个批处理程序,将我输入的内容输出到 .txt 文件中

问题描述

所以基本上我想批量制作一个程序,当我开始时它要求我登录并询问我的电子邮件我想要的是让程序接受我输入的内容就像这个例子:
程序要求电子邮件我输入 email1234@gmail .com 程序将其作为 Login.txt 存储在临时目录中,
因此当用户尝试再次登录时,程序会查找 Login.txt 并立即登录用户,而无需重新开始。这是代码:

@echo off
:choice
set /P c=Do you have a Account?[Y/N]?
if /I "%c%" EQU "Y" goto :login_succesfully
if /I "%c%" EQU "N" goto :Cannot_Login
goto :choice


:login_succesfully

cls
Color 2
echo "Ok Login To Verify Your Identity To Use This"
cls
:choice
rem Saved in C:\Temp
set /P c=Email?

if /I "%c%" EQU "" goto :Pass
goto :Pass
pause
exit

:Pass

cls
set /P c=Password?
if /I "%c%" EQU "" goto :Download
goto :Download

:Download
cls
echo Thank You Now Starting Example!
TIMEOUT /T 3
Start https://cdn.discordapp.com/attachments/845027712192217159/848817832170749963/Example1234.txt
pause
exit

:Cannot_Login

cls
color C
echo "Please Create a Account and ask a Admin to Manually Verify Who You Are"
pause
exit

标签: windowsbatch-filecmd

解决方案


对于不需要内容安全保护的任意登录系统,ntfs 系统提供了替代数据流提供的临时文件的替代方案。

@Echo off & CD /D "%~dp0"
Goto :Main

=====:# Save function
:Save
:# Define variables for retention with a # prefix
:# and output to the !User!_Save.cmd stream by calling this function
 (For /f "Delims=" %%G in ('Set #')Do Echo(Set "%%G") >"%~f0:%#User%_Save.cmd"
Exit /B 0

=====:# User input function
:Input <Variable> <Prompt> <Descriptor>
 Setlocal EnableDelayedExpansion
 Set "%~1="
 Set /P "%~1=%~2: "
 If Not Defined %~1 (
  Echo(%~3 required
  Endlocal
  Goto :Input
 )
 For /f "Delims=" %%v in ("!%~1!")Do ( Endlocal & Set "%~1=%%v" )
Exit /B 0

:Main
 Setlocal EnableDelayedExpansion
 Set "#Status=New"
 Cls
:Login
 Echo(Existing users:
 For /f "Skip=6 Tokens=2 Delims=:" %%G in ('Dir /R "%~f0"^|Findstr /vc:"_Save.cmd"')Do Echo(%%G
 Echo(
 Call :input #User "Enter your username" "Username"
 If /I "!#User!" == "E" Goto :Eof

:Verify
 Cls
 (More < "%~f0:!#User!") > nul 2>&1 && (
  Call :input #Pass "Enter your password" "Password"
  For /f "Delims= " %%G in ('More ^< "%~f0:!#User!"')Do Set "Validate=%%~G"
  REM case sensitive. Spaces not permitted.
  If not "!Validate!"=="!#Pass!" (
   Echo(Password invalid. Try again Y/N?
   For /F "Delims=" %%G in ('Choice /N /C:YN')Do if "%%G" == "N" Goto :Login
   Goto :Verify
  )
 ) || (
  Echo(Create user: "!#User!" Y/N?
  For /F "Delims=" %%G in ('Choice /N /C:YN')Do if "%%G" == "N" Goto :Login
  Call :input #Pass "Enter your password" Password
  Call :input PassC "Confirm your password" Password
  If not "!#Pass: =!" == "!PassC!" (
   Echo(Passwords do not match. Spaces not Permitted.
   Pause
   Goto :Verify
  )
  (Echo(!#Pass: =!) 1>"%~f0:!#User: =!" 2>Nul || (
   Echo(Invalid Characters in Password.
   Goto :Verify
  )
  Call :Save
 )

:ReturnAction
 Echo(
 Echo([C]ontinue [R]emove User [N]ew Password ?
 For /F "Delims=" %%G in ('Choice /N /C:CRN')Do (
  If "%%G" == "R" (
   Powershell -c "remove-item -path '%~nx0' -Stream '!#User!'"
   (Powershell -c "remove-item -path '%~nx0' -Stream '!#User!_Save.cmd'") 2> nul
   Goto :Login
  )
  If "%%G" == "N" (
   For /F "UsebackQ Delims=" %%G in ("%~f0:!#User!_Save.cmd")Do %%G
   Call :input #Pass "Enter your new password" Password
   Call :input PassC "Confirm your new password" Password
   If not "!#Pass: =!" == "!PassC!" (
    Echo(Passwords do not match. Space not permitted.
    Pause
    Goto :ReturnAction
   )
   (Echo(!#Pass: =!) 1>"%~f0:%#User: =%" 2>Nul || (
    Echo(Username contains invalid characters.
    Timeout /t 3 /Nobreak
    Goto :Verify
   )
   Call :Save
  )
 )

:# exit delayed expansion environment
 Endlocal & Set "#User=%#User%"

:# Load user information from the stream.
 For /F "UsebackQ Delims=" %%G in ("%~f0:%#User%_Save.cmd")Do %%G
 Echo(Login Successful.

:# The below line is not required. It simply shows the loaded values.
 Set #

:# Your script below
 Set "#Status=Returned"
 Call :Save

流使用以下语法:

  • “文件路径:流名”

每个用户都有两个与之关联的流,%~f0即批处理文件的路径:

  • "%~f0:!#User!"; 密码保存到哪个
  • "%~f0:!#User!_Save.cmd"; 用于存储所有#带前缀的用户变量以供以后加载
    • 加载是通过使用For /f循环读取和重新分配存储在流中的每个变量来实现的。

为了显示现有用户,for 循环使用开关和搜索词迭代Dir命令的输出,通过管道通过命令过滤(使用开关和匹配词从输出中删除 _Save.cmd 流)。/R"%~f0"findstr/V_Save.cmd

条件执行||&&用于根据流是否成功读取来控制脚本流。

当用户表示希望删除登录用户时,Powershell 用于从批处理文件中删除 Streams。(批处理没有从文件中删除流的本机方法)


推荐阅读