1.

Solve : Setting variable to result of a command??

Answer»

Is it possible to set a varaible to the result of a command in DOS? I am trying to set a VARIABLE = the result of a FINDSTR command.

My EXAMPLE, in a configuration/property file I have a log name. In a Batch file I want to get that log name and put it in a variable. Then I want to copy the file of that name to another filename (with a DATESTAMP).Since I don't know the format of your data or how many records FINDSTR finds, try redirecting the output of the FINDSTR to a file and then using a FOR to read and parse that file and set the variable accordingly.

Hope this helps. That sounds like a fairly reasonable solution and will implement if noone has any ideas after this post.

I expect the FINDSTR to find either 1 or 0 result lines. In a UNIX shell I'd do something like this:

var1 = `grep "stringabcd" filename`

and varabc would contain all lines that grep returned. I am trying to implement similar result in DOS. I had thought about redirecting to a file, but wasn't SURE what to do with it from there. I assume what you're suggesting is something like this in UNIX shells:

grep "abcd" file1 > filename
for i in `cat filename`
do
var1 = i
done

Is that what you're saying? TIA!Something like that.

Code: [Select]
findstr /i "string" filename.ext > search.dat
for /f "tokens=1,delims=" %%a in (search.dat) do (
set var=%%a
)


The 'found' string(s) will end up in search.dat. You can then use the FOR statement to read search.dat and pull the info you need. If the data is embedded in the line that FINDSTR found you will have to set up the NUMBER of tokens and delimiters accordingly. Type for /? at a command prompt for details.

Hope this helps.



Discussion

No Comment Found