Page 1 of 1

[HELP] Batch - Read File's Specific Data

Posted: 26 Mar 2017 11:34
by RedEyedRocker
Hello guys!
I just wanted to know if there's any possible way to read a file's specific data like say if I wanted to get only the float value from a file like "Version.txt" which contains the following data:-

Code: Select all

Version: 4.2


So as you can see, there is a string "Version: " and a float value "4.2" so I just want to extract the float value so is it possible to do so? I want this function so that I can set version for my programs and when I update it, it checks for older version and if it finds it, it will first uninstall the previous version and install the new one. So, for this feature, I need to read a specific data from a file. I know how to do this in QBasic but I can't seem to do it in Batch.

Does anyone know how to do this?

Re: [HELP] Batch - Read File's Specific Data

Posted: 26 Mar 2017 12:39
by aGerman
"Version:" and "4.2" are separated by a space (which is one of the default delimiters of a FOR /F loop). You need the 2nd token (substring).

Code: Select all

for /f "usebackq tokens=2" %%i in ("Version.txt") do set "version=%%i"


Steffen

Re: [HELP] Batch - Read File's Specific Data

Posted: 26 Mar 2017 13:04
by RedEyedRocker
aGerman wrote:"Version:" and "4.2" are separated by a space (which is one of the default delimiters of a FOR /F loop). You need the 2nd token (substring).

Code: Select all

for /f "usebackq tokens=2" %%i in ("Version.txt") do set "version=%%i"


Steffen


Great! Thanks for the help! Can you give me the reference to these?

Re: [HELP] Batch - Read File's Specific Data

Posted: 26 Mar 2017 13:09
by aGerman
You will get the command reference if you execute command /? (e.g. for /?) in a CMD window.
We also have a Command Index here at DosTips
http://www.dostips.com/DosCommandIndex.php

Steffen

Re: [HELP] Batch - Read File's Specific Data

Posted: 26 Mar 2017 13:11
by RedEyedRocker
aGerman wrote:You will get the command reference if you execute command /? (e.g. for /?) in a CMD window.
We also have a Command Index here at DosTips
http://www.dostips.com/DosCommandIndex.php

Steffen


Yeah, I wanted the reference on a website rather than on the console.