Page 1 of 1

for /f and findstr help please

Posted: 13 Sep 2011 12:53
by Exouxas
Hi, me again ^^

this is the text in textfile.txt:

Code: Select all

@echo off
cls
rem version#2.8#

((((lots of code that will be used later for a certain use))))


Now, what i want it to do is:

Green: variable
Blue: directory / file
Red: Step number
Pink: Stuff i want to be pink
Purple: Command

#1: Check for a line in a file that contains "version#"
#2: Assign that line to a variable (lets call it LineVar1)
#3: Use 'for /f' to split LineVar1 (that is why i added the #'s)
#4: Then take the 2nd part ('2.8') and assign that to LineVar2.
#5: Split it into 2 (in this case part 1 would be '2' and part 2 would be '8'.
#6: Assign thoose two parts into two different variables('Verp1' and 'Verp2')
#7: Do the same process with a different file (UFile.txt)

I can most likely do this part (#8 and #9) myself, but you might want to know the rest :P

#8: Check if 'Verp1' in 'textfile.txt' is greater than 'Verp1' in 'Ufile.txt'
#9: If #8 is false then check if 'Verp2' in 'textfile.txt' is greater than 'Verp2' in 'Ufile.txt'


EDIT:

Nevermind about the splitting 2.8 into two parts, figured i could do: if 2.8 gtr 2.9 lol :P

Re: for /f and findstr help please

Posted: 13 Sep 2011 15:50
by Exouxas
If anyone know how to do any part of this script, just post it, you dont need to help me with the whole thing.

Re: for /f and findstr help please

Posted: 13 Sep 2011 17:12
by Bob D
use FIND to find the line(s) in question, push the output to a file, read the file into a variable, search the variable for the target.

Code: Select all

find /i "wanted string" fileA > tempfile.txt
set /p var1 = < tempfile.txt

the above may fall over if there is more than one line in the original file that contains the target string.
I have to think more about how to proceed from here.

Re: for /f and findstr help please

Posted: 14 Sep 2011 02:38
by Exouxas
or just use findstr and do the same process

Re: for /f and findstr help please

Posted: 14 Sep 2011 16:32
by dbenham
Exouxas wrote:Nevermind about the splitting 2.8 into two parts, figured i could do: if 2.8 gtr 2.9 lol :P
Actually that won't be reliable because "DOS" only understands integral numbers. It will do an alpha comparison instead of a numeric comparison because of the period. This will result in 2.8 being greater than 10.1 - not what you want :!:

You can either split the version into two parts like you originally planned, or you can make sure you 0 pad the parts to account for as many versions as you think are likely. For example, 02.08 will allow up to 99 versions and subversions. Zero padding will allow adding a third or fourth subversion as needed without requiring changes to code. For example, "02.08.02" is greater than "02.08". Another advantage of zero padding is you could add alpha suffixes to the parts if you wanted and the comparisons will still work.

For the remainder of this post I'll assume you are not zero padding.


Bob D wrote:use FIND to find the line(s) in question, push the output to a file, read the file into a variable, search the variable for the target.
...
...The (code) may fall over if there is more than one line in the original file that contains the target string.

No need for a temporary file or multiple commands. Steps 1 through 6 can be accomplished with one relatively simple FOR /F construct using FINDSTR or FIND. I tend to favor FINDSTR when either will work, though I don't have a compelling reason for this.

Code: Select all

set "verp1="
set "verp2="
for /f "tokens=2,3 delims=#." %%a in ('findstr /i /c:"version#" file') do set /a "verp1=%%a, verp2=%%b"

If version# appears in multiple lines, than the last one detected will "win"

If you want to keep the 1st line that contains version# instead, then you simply use a GOTO in the loop:

Code: Select all

set "verp1="
set "verp2="
for /f "tokens=2,3 delims=#." %%a in ('findstr /i /c:"version#" file') do (
  set /a "verp1=%%a, verp2=%%b"
  goto :endLoop
)
:endLoop

I clear verp1 and verp2 before the FOR /F just in case the version# line is not found.

The above code will fail if the version does not have the correct format (it must be purely numeric with one period between the parts)

It will also fail if # or . appears in the line prior to version#

There are ways to safeguard against this using regular expressions and FINDSTR.

With slightly more complex code it is also possible to support a line like

Code: Select all

rem author#Joe Smith# version#2.8#
It all depends on how far you want to take the concept.

Dave Benham