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

Author Topic: Need Help extracting specific data from a txt file and using it is cmd argument  (Read 15191 times)

0 Members and 1 Guest are viewing this topic.

GeoreTran

    Topic Starter


    Starter

    • Experience: Familiar
    • OS: Windows 10
    All,

    I have virtually no idea how to write batch files and I have been searching the internet for help with this. 

    I need to extract data from a txt file and use that data in a command line argument.  Below is an example of the data I need to extract.  The bold data is what I need.

    Number - 12345
    Version - 1.02.03.4567
    Name - Software name
    Status - Upgrade completed successfully
    Message - success
    Timestamp - 2019-12-11 17:12:45.188

    I then need to use the extracted data in a command like this.

    C:\util\someprogram.exe  -n 12345 -v 1.02.03.4567 -p Patch1 -d 2019-12-03 -m success -s “software upgraded successfully….”

    I have this so far but it includes the dash before the data I need except for the last line because I am having to use the 3rd token.

    FOR /F "tokens=3* delims= " %%i in (message.txt) do @echo %%i %%j

    Any help would be appreciated


    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10

    Where are you getting this information from? ( Registry? or some prior executed program that assigns a number etc)

    Quote
    Number - 12345
    Version - 1.02.03.4567
    Name - Software name
    Status - Upgrade completed successfully
    Message - success
    Timestamp - 2019-12-11 17:12:45.188

    GeoreTran

      Topic Starter


      Starter

      • Experience: Familiar
      • OS: Windows 10
      This data is in a txt file that is created by another program. Basically the upgrade itself will create this text file.

      Salmon Trout

      • Guest
      Use tokens 1* with the dash as the delimiter, for each line, and use the * token for your fields, all except the last (hint: it starts "Timestamp") where you do another for /f on the second token, using 1* with the space as the delimiter, and take the first token.


      Salmon Trout

      • Guest
      (1) input.txt
      Number - 12345
      Version - 1.02.03.4567
      Name - Software name
      Status - Upgrade completed successfully
      Message - success
      Timestamp - 2019-12-11 17:12:45.188


      (2) batch script
      @echo off
      set filename=input.txt
      for /f "tokens=1-2* delims= " %%A in (' type "%filename%" ^| find "Number"    ') do set number=%%C
      for /f "tokens=1-2* delims= " %%A in (' type "%filename%" ^| find "Version"   ') do set version=%%C
      for /f "tokens=1-2* delims= " %%A in (' type "%filename%" ^| find "Name"      ') do set name=%%C
      for /f "tokens=1-2* delims= " %%A in (' type "%filename%" ^| find "Status"    ') do set status=%%C
      for /f "tokens=1-2* delims= " %%A in (' type "%filename%" ^| find "Message"   ') do set message=%%C
      for /f "tokens=1-3 delims= "  %%A in (' type "%filename%" ^| find "Timestamp" ') do set mydate=%%C
      echo number:  %number%
      echo version: %version%
      echo name:    %name%
      echo status:  %status%
      echo message: %message%
      echo mydate:  %mydate% 


      (3) script output
      number:  12345
      version: 1.02.03.4567
      name:    Software name
      status:  Upgrade completed successfully
      message: success
      mydate:  2019-12-11

      GeoreTran

        Topic Starter


        Starter

        • Experience: Familiar
        • OS: Windows 10
        thank you.  This worked perfectly.