好吧,所以我一直在研究一个相当复杂的批处理文件,它基本上要求用户创建一个包含6行(或更多行)文本的文本文件.然后它接受该文件并将每个字母的每个实例更改为其相应的数字(例如,如果文本文件的第一行表示“可编程”,则程序将字母“a”的所有实例更改为1和“b”进入2,依此类推,直到它全部为数字.然后它将显示为“16 18 15 7 18 1 13 1 2 12 5”)并且我会通过使用以下脚本来做到这一点:
set /p var=< text.txt set var=%var:a=1% echo.%var% >> text.txt
但是,此脚本的唯一问题是它只会将第一行的字母a更改为1,而不是文档其余部分中的字母“a”.
我还尝试在程序开头拆分文档,以便当用户键入第一行的文本时,它将其发送到单独的隐藏文件(text1.txt),而不是仅发送用户的所有文本行类型,分成一个文本文件(text.txt)
我是这样做的:
set /p line1=type the first line: echo %line1% >> text1.txt attrib +h text1.txt set /p line2=type the second line: echo %line2% >> text2.txt attrib +h text2.txt :: ######################################################################## :: Im not going to repeat this 6 times but hopefully you get my point :: ########################################################################
现在将行拆分为单独文件的问题在于,将每个字母的所有实例更改为各自的数字会非常高效且非常耗时,并且对于每一行(或在此情况下,文件)执行此操作用户输入…如果我将所有这些放在上下文中会有所帮助,这里是我的程序到目前为止的脚本,对于“:LOADING”下标我目前正在使用我之前解释过的方法,我将这些行拆分成文件.
代码:(另存为“stringparsing.bat”)
@echo off title BETA cls echo. echo. echo. echo Setting Variables... echo Loading Language Database... :: ################################################################################### :: CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE :: ################################################################################### :: An Idea i was thinking of trying was to put a subscript here that calls another :: batch file that contains a list of the letters set as variables to their :: respective numbers (example: SET a=1,SET b=2,SET c=3) :: ----------------------------------------------------------------------------------- PING 1.1.1.1 -n 1 -w 3000 >NUL goto MAIN :MAIN set foo=0 cls echo. echo. echo. echo. echo =================================== echo ################################# echo ####### Main Menu: ####### echo ################################# echo =================================== echo. echo. echo 1.) Create New Language File... echo. echo 2.) Load Existing Lanuage File... echo. echo 3.) Settings... echo --------------------------------------------------------- SET /p CHOICE= Select a Function: IF %CHOICE%== 1 GOTO CREATE IF %CHOICE%== 2 GOTO LOAD IF %CHOICE%== 3 GOTO SETTINGS GOTO MAIN :CREATE cls title Step 1 echo. echo. echo. echo echo ============================================================================ echo. set /p name= please type a name for your new language file: echo. echo ================================================================================= cls echo. echo. echo. echo ============================================================== echo ############################################################## echo #============================================================# echo # # echo # - After you hit enter you will be redirected # echo # to a Live Typer. so anything you type into # echo # it will be sent to %name%.txt. # echo # # echo # # echo # - Next,select load language! # echo # # echo #============================================================# echo ############################################################## echo ============================================================== pause goto typer1 :typer1 cls set /p line1= : echo %line1% >> %name%1.txt attrib +h %name%1.txt cls set /p line2= : echo %line2% >> %name%2.txt attrib +h %name%2.txt cls set /p line3= : echo %line3% >> %name%3.txt attrib +h %name%3.txt cls set /p line4= : echo %line4% >> %name%4.txt attrib +h %name%4.txt cls set /p line5= : echo %line5% >> %name%5.txt attrib +h %name%5.txt cls set /p line6= : echo %line6% >> %name%6.txt attrib +h %name%6.txt cls echo. echo. echo. echo ========================================================== echo. ( IF EXIST %name%1.txt echo - FIRST LINE CONFIRMED. IF EXIST %name%2.txt echo - SECOND LINE CONFIRMED. IF EXIST %name%3.txt echo - THIRD LINE CONFIRMED. IF EXIST %name%4.txt echo - FOURTH LINE CONFIRMED. IF EXIST %name%5.txt echo - FIFTH LINE CONFIRMED. IF EXIST %name%6.txt echo - SIXTH LINE CONFIRMED. echo %name% > Language_File.txt attrib +h Language_File.txt set /a foo+ =1 ) echo. echo ========================================================== goto LOAD :LOAD set /a foo+ =1 IF %foo%== 2 goto loadexternal goto LOAD23 :loadexternal echo. echo language file is loading now! pause > nul cls set /p name=<Language_File.txt echo. echo. echo Language_File Loaded! pause >nul goto LOAD23 :LOAD23 cls echo. echo. echo. echo. echo. echo Encoding Your Language File... Please Wait... echo. echo. echo. PING 1.1.1.1 -n 1 -w 3000 >NUL :A1 set /p var=< %name%1.txt set var=%var:a=1% echo.%var% echo %var% > %name%1.txt echo. echo. echo. echo "A" done. goto B1 :B1 set /p var=< %name%1.txt set var=%var:b=2% echo.%var% echo %var% > %name%1.txt echo. echo. echo. echo "B" done. goto C1 :C1 set /p var=< %name%1.txt set var=%var:c=3% echo.%var% echo %var% > %name%1.txt echo. echo. echo. echo "C" done. goto D1 :D1 set /p var=< %name%1.txt set var=%var:d=4% echo %var% > %name%1.txt echo "D" done. goto E1 :E1 set /p var=< %name%1.txt set var=%var:e=5% echo %var% > %name%1.txt echo "E" done. goto F1 :F1 set /p var=< %name%1.txt set var=%var:f=6% echo %var% > %name%1.txt echo "F" done. pause cls type %name%.txt pause >nul goto MAIN :END cls title SHUTTING DOWN... echo. echo. echo. echo Terminating service stream... echo. echo. echo. echo. echo Done! Thank you for using this program! ping 1.1.1.1 w -n 1 -w 6000 > NUL Exit***
如果你有一个解决方案,我会很高兴听到它,因为我一直在寻找解决这个问题的高低,但一无所获.此外,如果任何人注意到我的脚本中的任何其他错误或错误,请随时发表评论!
提前致谢!
PS.如果我在本主题中发布的最后一个脚本没有正确显示或者全部混淆,那么只需从此链接下载脚本:
[http://home.danieljewison.operaunite.com/f/content/Documents/stringparsing.bat][1]
解决方法
让我向您介绍我的朋友
for loop.将用户输入的所有行保存到单个文件中.我将此文件称为input.txt.使用带有/ f开关的for循环和delims =选项循环遍历文件中的每一行,并将这些行存储在变量%% i中.如果没有delims =,它只会读到第一个空白字符.
对于它读取的每一行,进行文本替换.批处理编程的“问题”是在for循环中设置变量时,必须在文件顶部添加set setlocal enabledelayedexpansion,然后使用!而不是%来访问变量内容.
@echo off setlocal enabledelayedexpansion for /f "delims=" %%i in (input.txt) do ( echo translating "%%i"... ^<insert fake delay here^> set var=%%i set var=!var:a=1 ! set var=!var:b=2 ! set var=!var:c=3 ! set var=!var:d=4 ! set var=!var:e=5 ! set var=!var:f=6 ! set var=!var:g=7 ! set var=!var:h=8 ! set var=!var:i=9 ! set var=!var:j=10 ! set var=!var:k=11 ! set var=!var:l=12 ! set var=!var:m=13 ! set var=!var:n=14 ! set var=!var:o=15 ! set var=!var:p=16 ! set var=!var:q=17 ! set var=!var:r=18 ! set var=!var:s=19 ! set var=!var:t=20 ! set var=!var:u=21 ! set var=!var:v=22 ! set var=!var:w=23 ! set var=!var:x=24 ! set var=!var:y=25 ! set var=!var:z=26 ! echo !var! )
如果input.txt包含以下内容:
programable this is line 2 third line
然后输出看起来像这样:
C:\batch>encode.cmd translating "programable"... <insert fake delay here> 16 18 15 7 18 1 13 1 2 12 5 translating "this is line 2"... <insert fake delay here> 20 8 9 19 9 19 12 9 14 5 2 translating "third line"... <insert fake delay here> 20 8 9 18 4 12 9 14 5
如你所见,我遗漏了假延迟.我很喜欢我的节目. 原文链接:https://www.f2er.com/html/228325.html