1.

Solve : Need Help extracting specific data from a txt file and using it is cmd argument?

Answer»

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


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
This data is in a txt file that is created by another program. Basically the upgrade itself will create this text file. 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.

(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-11thank you.  This worked perfectly.


Discussion

No Comment Found