如何更换上使用批处理文件中的文本文件的第二行的字符串? [英] how to replace a string on the second line in a text file using a batch file?

查看:283
本文介绍了如何更换上使用批处理文件中的文本文件的第二行的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我一直努力在该basicly要求用户创建一个包含6个(或以上)行文本的文本文件相当复杂的批处理文件。然后,它需要的文件,并在文本文件的第一行说,可编程,那么该程序会改变字母的所有实例一变成1和b每一个字母的每个实例更改为各自的编号(如进入2,依此类推,直到它被所有号码然后它会读16 18 15 7 18 1 13 1 2 12 5),我会通过使用下面的脚本做的:

Ok so ive been working on a fairly complex batch file that basicly asks the user to create a text file that contains 6 (or more) lines of text. Then it takes that file and changes every instance of every letter to its respective number (for example if the first line of the text file said "programable" then the program would change all instances of the letter "a" into 1 and "b" into 2 and so on until it was all numbers. It would then read "16 18 15 7 18 1 13 1 2 12 5") and i would do that by using the following script:

set /p var=< text.txt
set var=%var:a=1%
echo.%var% >> text.txt

不过这个脚本的唯一问题是,它只会改变字母a在第一行为1,而不是字母A,在文档的其余部分。

However the only problem with this script is that it will only change the letter a on the first line to 1, not the letter "a" in the rest of the document.

我还试图在程序的开始分裂文件,以便当用户键入的文本将其发送到一个单独的隐藏文件(text1.txt)的第一行,而不是仅仅将所有文本行的用户类型,到一个文本文件(的text.txt)

I also tried splitting the document at the beginning of the program so that when the user types the text for the first line it sends it to a separate hidden file (text1.txt) instead of just sending all of the lines of text that the user types, into one text file (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标我目前使用我刚才解释的方法,其中i分裂行到文件中。

Now the problem with splitting the lines into separate files is that it would be in-efficient and extremely time consuming to change all instances of every letter into their respective numbers, and do it for each line (or in this situation, file) that the user inputs... If it would help if i put all this in context, here is the script for my program so far, for the ":LOADING" subscript i am currently using the method i explained earlier where i split the lines into files.

code:(另存为stringparsing.bat)

Code: (Save As "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***

如果你有一个解决方案,我会很高兴听到这个消息,因为我一直在寻找高和低解决这个问题,但都一无所获。此外,如果任何一个告示在我的脚本任何其他错误或错误,那么请随时发表评论!

If you have a solution i would be glad to hear it because i have been searching high and low for a solution to this problem but have found nothing. Also, if any one notices any other mistakes or errors in my script then please feel free to comment!

在此先感谢!

PS。如果最后的剧本我发表在这个主题剪掉出来的权利,或者全都搞混了,然后就从这个链接下载脚本:

PS. If the last script i posted in this topic didnt come out right or it was all mixed up then just download the script from this link:

[http://home.danieljewison.operaunite.com/f/content/Documents/stringparsing.bat] [1]

[http://home.danieljewison.operaunite.com/f/content/Documents/stringparsing.bat][1]

推荐答案

让我把你介绍给我的朋友,的 for循环。保存用户输入到一个文件中的所有行。我会打电话给这个文件 input.txt的。使用循环与 /˚F开关和 delims = 选项遍历文件中的每一行,行存储在变量 %%我。如果没有 delims = ,它会只读,直到第一个空格字符。

Let me introduce you to my friend, the for loop. Save all the lines the user entered into a single file. I'll call this file input.txt. Use a for loop with the /f switch and the delims= option to loop through every line in the file, and store the lines in the variable %%i. Without delims=, it'd read only until the first whitespace character.

有关每次读取行,做你的文本替换。当您设置了循环中的变量疑难杂症批处理编程,你必须添加行 SETLOCAL enabledelayedexpansion 你的文件的顶部,并使用,而不是来访问变量的内容。

For each line it reads, do your text substitution. The "gotcha" with batch programming is when you set variables inside a for loop, you have to add the line setlocal enabledelayedexpansion at the top of your file, and use ! instead of % to access the variable contents.

@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

那么输出应该是这样的:

Then the output would look like this:

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

正如你所看到的,我离开了假延迟。我喜欢我的计划快。 :)

As you can see, I left out the fake delay. I like my programs fast. :)

这篇关于如何更换上使用批处理文件中的文本文件的第二行的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆