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

Author Topic: Reading text file line by line and do some operations - Windows Batch File  (Read 98749 times)

0 Members and 1 Guest are viewing this topic.

batch_novice

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows XP

    What I am trying to do here is to parse every row in filename.txt file and then run 2nd FOR loop to output it into another text file.

    Filename.txt has multiple rows which looks something like this:

    [email protected]|Filename.csv
    [email protected]|Filenam1.csv
    [email protected]|Filename.csv
    and so on.

    I need help in building a script to read the lines one by one for example I want to read [email protected]|Filename.csv 1st and then do some operations and after the operations are done, I want to to goto the next line and do the same operations. The output of the operations will be stored in a file and will be overwritten every time the loop counter increments and goes to the next line.

    My output file should contain only 1 row at a time and not all the rows. What I am thinking is to create a counter and then use that counter in the for loop and then do the operations but I am not getting the desired output.

    I went through the threads where something similar was asked but could not solve my issue.

    batch_novice

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Windows XP
      One more important point there is a pipe ('|') in the text file.

      I have tried this, till now:
      set /a count=0
      for /F "tokens=*" %%a in (C:\filename.txt) do (
               
               Set /a count+=1
               Set output[!count!]=%%a
               echo %output[!count!]%
               Pause
               )
      I am not able to call or access this outside the loop, like I would like to use the 1st row which will be represented by counter=1 first in a different for loop do some operations and then  come back and increment the counter=2 pointing to the 2nd row and do the same operations.
      « Last Edit: June 13, 2017, 11:05:04 AM by batch_novice »

      batch_novice

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Windows XP
        Lets say we have a file called file.txt.

        Within that file I have the following rows: [email protected]|Filename.csv [email protected]|Filenam1.csv [email protected]|Filename.csv

        So what I want is to read/access each line one at a time. For instance, 1st we access [email protected]|Filename.csv then do some operation where I will be inputing [email protected]|Filename.csv into a temp file and use this file in a different for loop for some other operations.

        After this is done I want to goto [email protected]|Filenam1.csv and do the same operations and finally [email protected]|Filename.csv.

           
        Every time the increment happens, the temp file will be overwritten with the current row field and we will exit out of the loop when %1 = ''.

        I hope I have explained my problem clearly.

        Hackoo



          Hopeful
        • Thanked: 42
        • Experience: Expert
        • OS: Windows 10
        Hi  ;)
        We can do like this script without using a temp file :
        Code: [Select]
        @echo off
        set "File=C:\filename.txt"
        set /a count=0
        SETLOCAL enabledelayedexpansion
        for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
                 Set /a count+=1
                 Set "output[!count!]=%%a"     
        )

        For /L %%i in (1,1,%Count%) Do (
        Call :Action "!output[%%i]!"
        pause
        )

        Exit
        ::*******************************************************
        :Action
        echo We treat this line : %1
        exit /b
        ::*******************************************************

        jonasgozdecki



          Newbie

          Thanked: 1
          • Experience: Experienced
          • OS: Windows 7
          Re: Reading text file line by line and do some operations - Windows Batch File
          « Reply #4 on: September 03, 2019, 08:01:52 AM »
          I've subscribed to this forum just to thankyou "Hackoo" for your solution.
          Worked like a charm.

          Cheers!

          Hackoo



            Hopeful
          • Thanked: 42
          • Experience: Expert
          • OS: Windows 10
          Re: Reading text file line by line and do some operations - Windows Batch File
          « Reply #5 on: September 12, 2019, 07:11:44 AM »
          I've subscribed to this forum just to thankyou "Hackoo" for your solution.
          Worked like a charm.
          Cheers!
          @jonasgozdeck
          You are very welcome and anytime too Bro  ;) :D
          Have a nice day  ;)

          la3



            Greenhorn

            • Experience: Beginner
            • OS: Windows 10
            I have joined this form because this is as close to what I'm looking for as I can find... I want to take what you've created here and be able to select a line by choice and then use those variables only on that line... My objective is to take something like the following -

            Code: [Select]
            Variable1    Variable2     Variable3     Variable4
            Description1 ComputerName1 ComputerUser1 ComputerPassword1
            Description2 ComputerName2 ComputerUser2 ComputerPassword2
            Description3 ComputerName3 ComputerUser3 ComputerPassword3
            Description4 ComputerName4 ComputerUser4 ComputerPassword4
            Description5 ComputerName5 ComputerUser5 ComputerPassword5
            Description6 ComputerName6 ComputerUser6 ComputerPassword6

            And then use it for creating something that would look like this -

            Code: [Select]
            Variable1's listed from text file -

            Description1
            Description2
            Description3
            Description4
            Description5
            Description6

            Make a selection: ((User enters Variable1))

            cmdkey /generic:"<line1variable2>" /user:"<line1variable3>" /pass:"<line1variable4>" <-------from a specific line a user selects
            mstsc /v:"<line1variable2>" /f <-------from a specific line a user selects

            Any help would be greatly appreciated...



            Hi  ;)
            We can do like this script without using a temp file :
            Code: [Select]
            @echo off
            set "File=C:\filename.txt"
            set /a count=0
            SETLOCAL enabledelayedexpansion
            for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
                     Set /a count+=1
                     Set "output[!count!]=%%a"     
            )

            For /L %%i in (1,1,%Count%) Do (
            Call :Action "!output[%%i]!"
            pause
            )

            Exit
            ::*******************************************************
            :Action
            echo We treat this line : %1
            exit /b
            ::*******************************************************

            la3



              Greenhorn

              • Experience: Beginner
              • OS: Windows 10
              I had reposted this question as my own and Sidewinder answered!

              Here's his perfect reply -- thanks!

              Batch language is truly ugly. That said, the batch logic here is to create an in-memory array (of sorts), let the user make a selection and then create the parameters for the cmdkey and mstsc commands.

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              set x=0

              for /f "skip=1 tokens=1-4" %%i in (%~dp0\Choices.txt) do (
                call set /a x+=1
                call set item.%%x%%.1=%%i
                call set item.%%x%%.2=%%j
                call set item.%%x%%.3=%%k
                call set item.%%x%%.4=%%l
              )

              for /l %%i in (1,1,%x%) do (
                call echo %%i. !item.%%i.1!
              )

              echo. & set /p sel=Enter Selection:

              cmdkey /generic:"!item.%sel%.2!>" /user:"!item.%sel%.3!" /pass:"!item.%sel%.4!"
              mstsc /v:"!item.%sel%.2!" /f

              The batch file uses a file named Choices.txt for it's input. The name is negotiable, just be sure to change the batch file accordingly.

              The descriptions, computername, computeruser and computerpassword fields cannot contain any embedded spaces or special characters. This is not negotiable.

              Hope this gives you some ideas

              linhkythuat



                Newbie

                • Experience: Beginner
                • OS: Unknown
                Re: Reading text file line by line and do some operations - Windows Batch File
                « Reply #8 on: December 20, 2020, 10:46:49 PM »
                My data in txt file is paths. I used command
                Code: [Select]
                start %1% to open a file, but it don't work.
                This is my code:
                Code: [Select]
                @echo off
                set "File=D:\listpathfile.txt"
                set /a count=0
                SETLOCAL enabledelayedexpansion
                for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
                         Set /a count+=1
                         Set "output[!count!]=%%a"     
                )

                For /L %%i in (1,1,%Count%) Do (
                 Call :Action "!output[%%i]!"

                 pause
                )

                Exit
                ::*******************************************************
                :Action
                echo We treat this line : %1
                start %1%
                exit /b
                ::*******************************************************
                Any help I appreciate and welcome

                Hackoo



                  Hopeful
                • Thanked: 42
                • Experience: Expert
                • OS: Windows 10
                Re: Reading text file line by line and do some operations - Windows Batch File
                « Reply #9 on: December 29, 2020, 02:35:06 PM »
                @linhkythuat
                You should try something like that :
                Code: [Select]
                @echo off
                set "File=D:\listpathfile.txt"
                set /a count=0
                SETLOCAL enabledelayedexpansion
                for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
                Set /a count+=1
                Set "PathFile[!count!]=%%~a"
                )

                For /L %%i in (1,1,%Count%) Do (
                Call :Action "!PathFile[%%i]!"
                pause
                )
                Exit
                ::-----------------------------------
                :Action
                echo We treat this line : "%~1" to start "%~nx1"
                start "%~nx1" "%~1"
                exit /b
                ::-----------------------------------