开发者

how to replace a string on the second line in a text file using a batch file?

开发者 https://www.devze.com 2023-01-27 18:33 出处:网络
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 ev

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

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.

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)

I did so using this:

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
:: ########################################################################

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: (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!

Thanks in advance!

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]


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.

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!
)

If input.txt has these contents:

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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消