[HELP] Batch - Read File's Specific Data

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RedEyedRocker
Posts: 19
Joined: 25 Mar 2017 12:15
Location: Nepal
Contact:

[HELP] Batch - Read File's Specific Data

#1 Post by RedEyedRocker » 26 Mar 2017 11:34

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 26 Mar 2017 12:39

"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

RedEyedRocker
Posts: 19
Joined: 25 Mar 2017 12:15
Location: Nepal
Contact:

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

#3 Post by RedEyedRocker » 26 Mar 2017 13:04

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 26 Mar 2017 13:09

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

RedEyedRocker
Posts: 19
Joined: 25 Mar 2017 12:15
Location: Nepal
Contact:

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

#5 Post by RedEyedRocker » 26 Mar 2017 13:11

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.

Post Reply