Computing.Net > Forums > Programming > Read vaiable from txt file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Read vaiable from txt file

Reply to Message Icon

Name: burtcampbell
Date: June 9, 2009 at 10:35:01 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

I have a txt file that is formated like this:
BackupSet Name= CsComputer, ID= 1149451491216
BackupSet Name= MacBook, ID= 1225425991161
BackupSet Name= Office Desktop, ID= 1234397390047
BackupSet Name= File Demo, ID= 1244129637412
BackupSet Name= SQL Demo, ID= 1244556292461

I need to be able to read this txt file from a batch program, find a specific string like "File Demo", then return the last 13 characters of that line, which happen to be the BackupSet ID. Or if its better to to grab everything after the "=" and trim the space.

Thanks so much.



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: June 9, 2009 at 12:38:55 Pacific
Reply:

@echo off
for /F "tokens=2 delims==" %%j in ('type "MyFile.txt" ^| find "%Name%"') do set ID=%%j
set ID=%ID:~1%

The above script, assuming the variable Name contains the required string, returns the number in ID. That requires just ONE line holds the target string, otherwise the code must be rearranged.

0

Response Number 2
Name: ghostdog
Date: June 9, 2009 at 17:22:34 Pacific
Reply:

if you have Python on Windows:

for line in open("file.txt"):
    ID=line.split()[-1]
    print ID

save the above as test.py and on command line:
C:\test>python test.py
1149451491216
1225425991161
1234397390047
1244129637412
1244556292461


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Read vaiable from txt file

Read lines from .txt file in DOS? www.computing.net/answers/programming/read-lines-from-txt-file-in-dos/15219.html

Reading data from a file by using batch file www.computing.net/answers/programming/reading-data-from-a-file-by-using-batch-file/19995.html

Extracting data out from txt file www.computing.net/answers/programming/extracting-data-out-from-txt-file-/20329.html