3

i am working on a batch script.

i want to store the count of row's in variable.

like

set var = mysql -uroot -proot -e"select count(*) from table";

i also tried to do it other way like

 set var= mysql -uroot -proot -e "select count(*) from table into outfile 'F:\count.txt'";

 for /f %%a in ("F:\count.txt") do ( 
set output = %%a
echo %output% 

pause 

)

In above code the variable "output" shows nothing(empty).

please help me out.

3 Answers 3

2

I can see at least two issues in your script:

  1. A string in double quotes inside IN( ) is treated as a literal, not as a file path/name, unless you specify the usebackq option, which enforces different semantics, whereby either double-quoted string or non-quoted one is treated as a file name.

  2. You are storing <space>%%a into the output<space> variable, not %%a into output.

After you've fixed those two, there will remain one (probably, just one) more issue. You are assigning a value to a variable and then evaluating the variable in the same bracketed block (which is your loop body) using immediate variable expansion (%var%). This cannot work as expected. The thing is, a bracketed block is parsed entirely as a single unit, i.e. all its commands are parsed before the first one executes. As you can guess, your %output% expression will in this case evaluate to nothing, because output is not yet assigned a value at the time of parsing. (And when it is assigned a value, it will change nothing, because the previous (empty) value will already have replaced the expression.)

You can solve this using delayed variable expansion, which, as can be guessed, uses a different timing for evaluation. First, you should enable delayed expansion by issuing the SETLOCAL EnableDelayedExpansion command, then use a slightly different syntax: !var! instead of %var%.

So, if we address all the issues mentioned above, the loop may look like this:

SETLOCAL EnableDelayedExpansion

FOR /F "usebackq" IN ("F:\count.txt") DO (
SET output=%%a
ECHO !output!
)
Sign up to request clarification or add additional context in comments.

Comments

2

You just define var with the content mysql -uroot ... but you don't execute it!
So there shouldn't be a F:\count.txt file.

Your set-syntax is wrong, remove the spaces ( from set output = %%a), else you create a variable output<space> instead of output

Your code could look like

mysql -uroot -proot -e "select count(*) from table into outfile 'F:\count.txt'";
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (F:\count.txt) do ( 
  set "output=%%a"
  echo !output%!
)

Comments

0

You are on the right track, but the FOR /F command you show is not proper syntax - it would give an error (I'm ignoring the IN() clause because I know it is intentionally incomplete).

The "tokens=" option is incomplete, it must be followed by at least one number or an asterisk - see the documentation for more info (type HELP FOR from the command line). In your case you don't need a tokens option - the "delims=" is all you need.

There must be at least one space between DO and the (.

If you make those fixes and complete your IN() clause, then it should work. If it doesn't then something may be wrong with your IN() clause. You should post the entire command if you want help diagnosing the problem.

5 Comments

What i am doing now is : set var= mysql -uroot -proot -e "select count(*) from table into outfile 'F:\count.txt'"; for /f %%a in ("F:\count.txt") do ( echo %%a pause )
But when i try to store %%a value in variable like set output=%%a echo %output% it shows me blank value.
my aim is to store count in one variable and use it in the further calculation. Like i am planning to run count query 2 times. and want to subtract both output in if condition.
@PratikGujarathi - I understand your goal, but I'm not sure I understand what code you have tried. Please edit your question and post the exact code, properly formatted, so that we can read it. Don't just post the line you think is failing, also post what leads up to it.
in first line i am capturing output in count.txt file and then i try read that file and store the value on it to variable. but i think i am somewhere wrong but did you got my logic??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.