Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Need help writing a batch that extacts specfic characters from a txt to a txt  (Read 4736 times)

0 Members and 1 Guest are viewing this topic.

fastrack12003

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 10
    First off I do not really know anything about making batch files and my end goal may not even be possible using a batch file.
    long story short i would like a way to be able to make a batch file that I can just change a lines of text that will perform extract operations into another txt file (print.txt). I'll do my best to explain:
    My end goal is to be able to just change 3 lines of text and have it make all the necessary changes and then print the  print.txt file. Example of the 3 modifiers : 3001, 20170623, Y or N

    my export txt document reads something like this (List.txt)
    123456  A- 3000 ch
    234567  A- 3001 ch
    234568  A- 3001 cL
    234569  A- 3001 gp
    345678  A- 3002 ch
    345679  A- 3002 cL

    i want it to end something like this

                      12-3001         (12) here needs to be determined by another txt file as it needs to look at a master list for the two
    ____________________          digit number that corresponds to 3001. 3000 - 3002 could all be 12, but 3003 - 3005
    | 20170623     234567   |         could be 13
    |                                  |                                                                       Master List
    |                                  |                                                                to look something like this:    will be called "Master.txt"
    |                                  |                                                                         12 - 3000
    |__________________ |                                                                          12 - 3001
    |                                  |                                                                          12 - 3002   The master list will have 103 different
    |                                  |                                                                          13 - 3003     lines of text in the same format
    |       (Blank)                 |                                                                          13 - 3004
    |                                   |                                                                         13 - 3005
    |                                   |
    |__ _________________|
    |                                   |
    |                                   |
    |         (Blank)               |
    |                                   |
    |                                   |
    |___________________|

    when I enter the the modifiable 3000, 3001, etc i need it to for example say search list.txt for "3001 ch" in postions 12-15
    and when it finds "3001 ch" to copy the characters in the same row in positions 1-6 and insert them in to the  print.txt file on say row 5 positions 6-12 (paste positions will never need to change). The row that has "3001 ch" could be found on could be anywhere between row 1 and 500.
                                                                           
    I also need the Modifiable R=Y or N to say if a Y is present then do this : 
    search list.txt for 3001 cL  and for it to copy the characters from positions 1-6 and paste them into row 7 positions 16-21
    and at the same time is R=Y is present to also copy my modifier of "20170623"  into row 7 positions 3-10
    If N is set Leave blank.
    also need the same to happen if G=Y to search for "3001 gp"     
                                                                                                                   
                      12-3001                                                                                 
    ____________________         
    | 20170623     234567   |         
    |                                  |
    | text #1 never changes |
    |                                  |
    |__________________ |
    | 20170623     234568   |
    |                                  |
    | text #2 never changes | all of this to only be inserted if Y is set.  except the |'s and_  they need to
    | but is only present      |  always be present and in the  exact position.
    | if  R=Y is set               |
    |__________________ |
    | 20170623      234569  |                                 
    |                                  |
    | text #3 never changes |
    | but is only present      |
    | if G=Y                        |
    |___________________|

    This all seems possible to me, this will have to be scaled up with numerous other situations such as E=Y T=Y etc and the finsihed print.txt will have lots more none changing characters.

    Can someone help me figure out where to start? and i mean from scratch i dont understand any coding at all.   

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    This reminds me of help that foxidrive gave me on an ability to change values within text files with batch that generates a QBasic program and then calls that QBasic program to do stuff that batch alone wasnt able to do. Here is the problem I had that is archived here that worked perfect for changing values within text file based on rules within the QBasic of what to change within the text file. https://www.computerhope.com/forum/index.php?topic=151160.0;wap

    This code here is for my application of changes made by use of QBasic within batch... you could do a similar program to make all changes you need this way as well, but would need to write code that does it. So this is just a working example that works for my needs, but your needs are different.

    This solution for my needs replaces two values of N with Y and looks for keywords in the text file and targets them to change them so $LASTSORT N and LS: N are the keywords that it looks for and where found it replaces them with $LASTSORT Y and LS: Y .... My use of this is to force configuration changes to about 50 text files with .mtx and .xts file extensions by use of a Batch file that runs QBasic and targets all TXT files at the target location.

    Code: [Select]
    @echo off
    set "file=edit.bas"
     >"%file%" echo  OPEN "temp.tmp" FOR INPUT AS #1
    >>"%file%" echo  OPEN "temp.txt" FOR OUTPUT AS #2
    >>"%file%" echo   DO WHILE NOT EOF(1)
    >>"%file%" echo    LINE INPUT #1, a$: y=1
    >>"%file%" echo    if not INSTR(ucase$(a$), "LS: N"       ) = 0 THEN ? #2, "LS: Y"       : y=0
    >>"%file%" echo    if not INSTR(ucase$(a$), "$LASTSORT N" ) = 0 THEN ? #2, "$LASTSORT Y" : y=0
    >>"%file%" echo    if y=1 then ? #2, a$
    >>"%file%" echo   LOOP
    >>"%file%" echo CLOSE
    >>"%file%" echo SYSTEM

    for %%a in (*.mtx *.xts) do (
    del temp.txt 2>nul
    ren "%%a" temp.tmp
    qbasic /run %file%
    ren temp.txt "%%a"
    )

    del temp.txt 2>nul
    del %file%
    echo done
    pause


    My thoughts on this is that you will need to use QBasic or another language to achieve what you want to do, but by which that can be called within the batch file if it needs to be a batched process. I didnt think I had a solution for what i needed because the system that runs this is on old NT4 OS and I am not permitted to bringing in any 3rd party media/software etc to work. I was limited to whatever i could type into a text editor, so i had to manually create this on the system that it was to run on since there is a #1 rule that no software comes and goes on media, and rule doesnt apply to manually coding up locally which is virus/malware free. So I manually coded it up at the system that was to run this. You likely dont have restrictions and so your not stuck with only Qbasic etc and it could be achieved in a number of programming language and higher level scripting ways.

    fastrack12003

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Windows 10
      DaveLembke Thanks for the reply.
      I really don't understand how to use any of this information to help me withe my needs, is there a way that you could explain to me what I could do to run this so I can see exactly what this can do?
      could I somehow modify your code say like:

      replace input "temp.tmp" with "input.txt"
      and replace output "temp.txt" with "output.txt"

      If I were to create input.txt and output.txt files and make those above changes to the code, what would i need to have typed into input.txt for this to make a change to output.txt?

      also these lines of code you say are QBasic I don't know what that is or how to really use the code. Do I just copy the code into a text file and save it as a .bat file for it to work or is there more that needs done to make it function?'

      without being able to see theses lines of code operate i dont think i will be able to understand how i can eventually manipulate it into something that will help me.

      Sorry but i am completely new to all of this.

      DaveLembke



        Sage
      • Thanked: 662
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Hello   :) ... I didnt quite follow your examples of what you need to achieve, but i saw changes that are similar to what I use, so based on that I shared mine. But you shared your a beginner with all of this so I will try to avoid being confusing.

      Could you share a better description of what your trying to achieve sharing actual file names and sharing the inner contents of those files if able to share that. It doesnt need to be the entire file and can just be snippets from them, but looking at your initial example in first post on this, I looked at what you have and dont see how the different letters = Y or N come into play in your files. Its hard to follow, and maybe a new example making sure that nothing is left out and nothing too vague will make me and others here better able to assist you.  :)

      fastrack12003

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Windows 10
        I am trying to create a form that can be filled out multiple ways by using a batch file.
        sometimes I need it either filled out with just the top section filled in or just the top 2 sections filled in or all 3 sections filled in.

        It is a form that I have to fill out 52 times a week. Currently I have the entire form typed up in a Microsoft word document and I just copy and paste all the changes onto it manually. Each of the 3 sections correspond to a job that is to be performed. each form applies specifically to a location, and at each location the jobs change from week to week. but the 3 jobs will always go into the same section if that job is to be performed each week. Each location gets a different job number assigned to each job and these change weekly thats why I need to be able to have it search for a txt file for 3000 (Building Number) and be able to identify each of my 3 variables of P,R,G. so I can tell it which jobs to look for in the job number text file for it can export the job number into the correct spot on the final form.

        each building number (3000) has a specific identifier (2 digit number) that goes with it that must be on the form to look like this (12-3000) but the identifier is not listed on the job number list. So i will need to type up a master list some how that the batch file can go to to look up the 2 digit identifier so that it can input the full (12-3000) in the form.
                                                    (Building)     (Date)      (Job 1)    (Job 2)    (Job 3)
        My goal is to just type these:     3000       20170629     P=Y        R=Y       G=N     into the batch file

        example of finished filled out for will look similar to this:


                        --------- (Building Number with Identifier) ----------
        -----------------------------------------------------------------------------------
        | (Job Number) |  (Date) |                                              |      (Date)       |
        |----------------- -----------------------------------------------------------------
        |                                                         |                                                |
        |     (JOB 1 Description)                        |              Job 1 C/W                   |       Second block only filled in if Job 1 selected
        |                                                         |                                                |       (P=Y)
        |                                                         |           employee number __       |
        |-----------------------------------------------------------------------------------
        | (Job Number) |  (Date)  |                                             |      (Date)       |
        |----------------------------------------------------------------------------------- 
        |                                                         |                                                |
        |     (JOB 2 Description)                        |                Job 2 C/W                 |     Second block only filled in if Job 3 selected
        |                                                         |                                                |     (R=Y)
        |                                                         |            employee number __      |
        |-----------------------------------------------------------------------------------
        |                                                         |                                                |
        |                                                         |                                                |
        |                                                         |                                                |      All Blocks remain empty because (G=N)
        |                                                         |                                                |
        |                                                         |                                                |         
        |-----------------------------------------------------------------------------------

        Buliding Master.txt:

        12-3000
        12-3001
        12-3002
        13-3003
        13-3004
        13-3005
         continues on with a total number of 103 Building numbers with identifiers.


        Job Number List.txt:
        (Job Number) (Building) (Job Description)
        123456  A-      3000              CH                     CH - is Job 1    (P=)                   (A-  doesn't mean anything)
        234567  A- 3001 CH                                       CL - is Job 2     (R=)
        234568  A- 3001 CL                                        GP - is Job 3    (G=)
        234569  A- 3001 GP
        345678  A- 3002 CH
        345679  A- 3002 CL


         Don't Really want to make this more complicated, but it would be nice if I could add each building location to the master list and sort them into groups by that such as:

        12-3000 N                 N=North
        12-3001 S                  S=South
        12-3002 E                  E=East
        13-3003 W                 W=West
        13-3004 E
        13-3005 S

        Depending on the day of the week it determines what sections of the buildings to be worked
        Monday - North (13 Locations)
        Tuesday - South (13 Locations)
        Wednesday -East (13 Locations)
        Thursday - West (13 Locations)

        I would be nice if the batch could create all 13 forms for that day by just being run once and then sent to the printer automatically.

        I spend about 3 hours a week figuring out what buildings to be worked and creating and printing out each form.
        I bet a batch file could do all of this in a matter of minutes.



                                     
        « Last Edit: June 29, 2017, 06:02:24 PM by fastrack12003 »