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

Author Topic: .bat file that reads from a existing .txt file and creates a new .txt file  (Read 3393 times)

0 Members and 1 Guest are viewing this topic.

motoxeryz125

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    Hello everyone.

    I am trying to make a .bat file that reads the individual lines of a .txt file, and then, creates a new .txt file. I am doing this because i am trying to automate an extremely repetitive scheduling task. 

    For example, I have a file named project.txt  which has  several lines, and it looks like this:

    Firstidea
    Secondidea
    Thirdidea
    Fourthidea
    Fifthidea
    Sixthidea
    Seventhidea
    …..(and so on)

    Im trying to make a .bat file that will read the file above and create a new file, called projectschedule.txt, that will look like this:


    <Schedule0>
        <JobNum>4</JobNum>
        <JobParam>firstidea</JobParam>
    </Schedule0>
    <Schedule1>
        <JobNum>1</JobNum>
        <JobParam></JobParam>
    </Schedule1>
    <Schedule2>
        <JobNum>0</JobNum>
        <JobParam></JobParam>
    </Schedule2>
    <Schedule3>
        <JobNum>4</JobNum>
        <JobParam>secondidea</JobParam>
    </Schedule3>
    <Schedule4>
        <JobNum>1</JobNum>
        <JobParam></JobParam>
    </Schedule4>
    <Schedule5>
        <JobNum>0</JobNum>
        <JobParam></JobParam>
    </Schedule5>
    <Schedule6>
        <JobNum>4</JobNum>
        <JobParam>thirdidea</JobParam>
    </Schedule6>
    <Schedule7>
        <JobNum>1</JobNum>
        <JobParam></JobParam>
    </Schedule7>
    <Schedule8>
        <JobNum>0</JobNum>
        <JobParam></JobParam>
    </Schedule8>

    …….And so on.


    So, the <jobparam> section from <schedule1> will reference the first line of the project.txt file.
    The <jobparam> section of <schedule4> will reference the 2nd line of the project.txt file
    The <jobparam> section of <schedule7> will reference the 3rd line of the project.txt file
    The <jobparam> section of <schedule10> will reference the 4th line of the project.txt file
    The <jobparam> section of <schedule13>will reference the 5th line of the project.txt file

    And so on for each line in the project.txt file.


    The project.txt file that the .bat file will be referencing can have hundreds of lines. How can I make a for loop to reference each individual line of the project.txt file to make an output file called projectschedule.txt that looks similar to above?

    I am thinking that the loop will look something like this:

    @echo off
    setlocal enabledelayedexpansion
    cd /d "C:\Users\John\Desktop\schedule"
    set /a sched=0
       for /f "delims=" %%A in (project.txt) do (
          echo <Schedule!sched!> > projectschedule.txt
          echo <JobNum>4</JobNum> >> projectschedule.txt
          echo <JobParam>firstidea</JobParam> >> projectschedule.txt
          echo </Schedule!sched!> >> projectschedule.txt
          echo <Schedule!sched!+1> >> projectschedule.txt
          echo <JobNum>1</JobNum> >> projectschedule.txt
          echo <JobParam></JobParam> >> projectschedule.txt
          echo </Schedule!sched!+1> >> projectschedule.txt
          echo <Schedule!sched!+2> >> projectschedule.txt
          echo <JobNum>0</JobNum> >> projectschedule.txt
          echo <JobParam></JobParam> >> projectschedule.txt
          echo </Schedule!sched!+2> >> projectschedule.txt
    sched=!sched!+3
    )


    I am new to batch, and i had a lot of help developing a similar batch from a forum a member named Salmon Trout.

    The code above does not work. Part of the reason is because my output text needs to have < and > symbols.


    -----echo <JobParam>firstidea</JobParam> >> projectschedule.txt
    Also, the string above is the 8th line of the large code above. This string of code needs output the next consecutive line of the project.txt file each time it is looped through. Currently, the code above will only output"firstidea" every time it is looped through.



    Any ideas or help would be greatly appreciated.

    motoxeryz125

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Hello,

      Thank you for the input.

      The code you sent above works great!

      The line that helped out the most was

        echo ^<JobParam^>%%i^</JobParam^>  >>  projectschedule.txt
      inside of the do() loop, as i did not know how to reference each invidiviual line of the project.txt file as the loop was running.

      Thanks again!