2

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]

daniel11
  • 2,027
  • 10
  • 38
  • 46
  • Have you considered using a modern programming language (Python...Ruby...anything really)? There aren't really any good reasons I can think of to use batch files at this point (...well, for the last 10 years really). – Gerrat Nov 30 '10 at 04:48
  • 2
    well, i would if i knew anything about any of those languages , ive tried making programs in c++ and ive got the source codes for all of them but i dont know how to compile them, because all of the compilers out there are really confusing and ask me for so many parameters and you have to download soo much to get them to work... but with batch you only need notepad :P plus im much better at shell/batch programming than i am at any other language (although i would love to expand my knowledge of shell because im pretty sure shell includes other parts besides batch files. – daniel11 Dec 04 '10 at 01:09

1 Answers1

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

indiv
  • 17,306
  • 6
  • 61
  • 82
  • Also take a look at http://stackoverflow.com/questions/3805036/read-only-x-lines-from-a-txt-file-with-a-windows-batch-file – Jaco Briers Nov 30 '10 at 05:36
  • Or this one, if we're linking related questions: http://stackoverflow.com/questions/130116/windows-batch-commands-to-read-first-line-from-text-file – indiv Nov 30 '10 at 05:48
  • thank you soo muchh this is exactly what i was looking for! btw for the fake delay im using a common command called PING 1.1.1.1 -n 1 -w 3000 >NUL do you know of a more efficient method of pausing a script for a specified number of seconds/milli seconds? thanks! – daniel11 Nov 30 '10 at 22:35
  • @daniel11: Your ping trick is as good as you can get without custom programs as far as I know. – indiv Nov 30 '10 at 23:30
  • ok well another thing i wanted to do was make this script work somehow: @echo off cls color A :MAIN cls echo. echo. echo ================================= echo. set /p var= what is your name? :: DOES NOT SHOW UP (STARTING HERE) echo. echo ================================= :: DOES NOT SHOW UP (ENDING HERE) set /p answer= so your name is %var%? IF %answer%== yes echo thats an amazing name! IF %answer%== no goto MAIN the part thats surrounded by the remarks doesnt want to show up for some reason... any ideas? – daniel11 Dec 01 '10 at 00:22
  • so sorry about the script getting mixed up... ive tried editing it several times but it wont save the changes i make to it – daniel11 Dec 01 '10 at 00:26
  • but it should be fairly obvious where to insert "enters" – daniel11 Dec 01 '10 at 00:27
  • @daniel11: Go ahead and post that as a new question to keep things clean and easier to manage. – indiv Dec 01 '10 at 00:45
  • yea ok thats a good idea, and also just thought id let you know that i found two much better alternatives to the "ping" command, you could use choice /T 5 /C y /N /D y > nul wich basicly waits 5 seconds for the user the type y , if the user doesnt type y then it will wait the 5 seconds (you can change it to whatever number) and then continues. also , heres the simpler version which i am using from now on is "TIMEOUT /T 5 /NOBREAK" (see next comment for the rest of description) – daniel11 Dec 01 '10 at 03:17
  • (continued ) ...which pauses the script for 5 seconds before continuing . and the "/nobreaks" switch just means that it stops the user from hitting a key to continue (without this switch the command would b like the pause command) hope this helps! – daniel11 Dec 01 '10 at 03:18