[英]how to replace a string on the second line in a text file using a batch file?
好的,我一直在研究一个相当复杂的批处理文件,该文件基本上要求用户创建一个包含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”更改为1。
我还尝试在程序的开头拆分文档,以便在用户键入第一行的文本时,它将文档发送到单独的隐藏文件(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循环 。 将用户输入的所有行保存到一个文件中。 我将这个文件称为input.txt
。 使用带/f
开关和delims=
选项的for
循环遍历文件中的每一行,并将这些行存储在变量%%i
。 如果没有delims=
,它将一直读到第一个空格字符。
对于它读取的每一行,请进行文本替换。 批处理编程的“陷阱”是在for
循环中设置变量时,必须在文件顶部添加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
如您所见,我省去了假的延迟。 我喜欢我的程序很快。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.