set /p var=< text.txtset 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.txtattrib +h text1.txtset /p line2=type the second line:echo %line2% >> text2.txtattrib +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 offsetlocal enabledelayedexpansionfor /f "delims=" %%i in (input.txt) do (echo translating "%%i"... ^<insert fake delay here^>set var=%%iset 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包含以下内容:
programablethis is line 2third line
然后输出看起来像这样:
C:\batch>encode.cmdtranslating "programable"... <insert fake delay here>16 18 15 7 18 1 13 1 2 12 5translating "this is line 2"... <insert fake delay here>20 8 9 19 9 19 12 9 14 5 2translating "third line"... <insert fake delay here>20 8 9 18 4 12 9 14 5
如你所见,我遗漏了假延迟.我很喜欢我的节目. 总结
以上是内存溢出为你收集整理的如何使用批处理文件替换文本文件中第二行的字符串?全部内容,希望文章能够帮你解决如何使用批处理文件替换文本文件中第二行的字符串?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)