Read vaiable from txt file

June 9, 2009 at 10:35:01
Specs: Windows XP
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.

See More: Read vaiable from txt file

Report •

June 9, 2009 at 12:38:55
@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.

Report •

June 9, 2009 at 17:22:34
if you have Python on Windows:
for line in open("file.txt"):
    print ID

save the above as and on command line:

Report •

Related Solutions

Ask Question