【问题标题】:Write Multiple Variables in one txt document Batch在一个txt文档中批量写入多个变量
【发布时间】:2022-12-08 00:24:12
【问题描述】:

是否可以在一个.txt文件中批量写入多个Variables? 我想制作一个随机密码生成器,您首先必须说出密码必须有多少个字符,然后生成密码并将其放入 .txt 文件中

我的想法是,首先(在您说出密码应该有多长之后)生成一个随机数(开始时为 1、2 或 3(1 = a、2 = b、3 = c))。然后看选择了哪个数字然后搜索对应的字母写入txt文档,直到和你开头说的一样多的字符。

看起来像这样:

@echo off

:main
cls
set /p anz=How many characters?: 
goto rand
:rand
set /a letter=%random% %%3
goto test

:test
if %letter%==1 goto 1
if %letter%==2 goto 2
if %letter%==3 goto 3

:1
if %anz%==0 goto finish
set /p print=a
set /a anz-=1
goto printin

:2
if %anz%==0 goto finish
set /p print=b
set /a anz-=1
goto printin

:3
if %anz%==0 goto finish
set /p print=c
set /a anz-=1
goto printin

:printin
echo %print% > Your_Password.txt                   <--- Here does the letter get written in the .txt file
goto rand

:finish
echo finish
goto main

但它只写 .txt 文件中的最后一个字母

一开始我只用 a, b, c 做了,以后我想添加整个字母表

我是一批新手,首先收集我的第一次经验

【问题讨论】:

  • &gt; 覆盖文本文件的内容,&gt;&gt; 附加到文本文件的末尾。此外,echo 添加了一个换行符,因此每个字母都将在其自己的单独行上,除非你这样做 &lt;nul set /p "=%print%" &gt;Your_password.txt。此外,%random% %% 3 将返回 0 到 2 之间的数字,因此您需要相应地调整您的数字或将 +1 粘贴到该等式的末尾。
  • <nul set /p "=%print%" >Your_password.txt 是什么意思,我需要将它设置在该行后面还是用该行替换它?
  • &lt;nul set /p "=%print%" &gt;&gt;Your_password.txt而不是echo %print% &gt; Your_Password.txt
  • 现在在 Your_Password.txt 中写着:ECHO ist ausgeschaltet (OFF)。你知道谁来解决这个问题吗?

标签: batch-file


【解决方案1】:

enter code here@echo o enter code heresetlocal enabledelayedexpansion enter code here设置计数器=1 enter code herefor /f %%x in (D:COMP_T.txt) do ( 在此处输入代码`set "comp!Counter!=%%x" 设置/一个计数器+=1 )

【讨论】:

  • 现在我比以前更困惑了,对不起。我不明白。
  • @RedstoneGuy。忽略这个。 Lokesh 似乎完全误解了你的问题。
  • 好的,谢谢@Stephan
【解决方案2】:

最好先将所有需要的字符收集到一个变量中,然后只写入文件一次:

@echo off
setlocal enableextensions enabledelayedexpansion
REM valid chars for password:
set "chars=abcdefghijklmnopqrstuvwxyz"
REM number of chars:
set "CharCount=26"
REM clear pwd: 
set "pwd="
set /p "anz=How many characters?: "
REM %anz% times, add a random char:
for /l %%i in (1,1,%anz%) do call :addChar
(echo %pwd%)>"YourPassword.txt"
goto :eof

:addChar
rem get a random number [0..anz-1]
set /a rnd=%random% %% %CharCount%
REM add the rnd-th character from validChars to the pwd (zerobased count):
set "pwd=%pwd%!chars:~%rnd%,1!"
goto :eof

请参阅set /? 了解如何选择随机字符,并查看for /? 了解它是如何做到的 anz 次。
另请阅读有关 delayed expansion 的内容,了解为什么使用 !

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2017-11-29
    • 2020-10-06
    • 1970-01-01
    • 2018-08-02
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多