Solved Why I am getting error while running bat file

September 7, 2016 at 09:30:07
Specs: Windows 7
The code is as follows:

@Echo off
Set _File=SQLQuery.txt
Set /a _Lines=0
For /f %j in ('Type %_File%^|Find "" /v /c') Do Set /a _Lines=%j
Echo %_File% has %_Lines% lines.


When I run as a bat file it gives me error as 'the syntax of the command is incorrect' .

And when I run all the above commands it runs successfully.
Why this is so?


See More: Why I am getting error while running bat file

Report •


✔ Best Answer
September 9, 2016 at 09:35:14
Yep, blame the delayed expansion.
@SETLOCAL EnableDelayedExpansion
@SET /P Q=Q ?
@SET /A Q=^^!Q
@ECHO !Q!

How To Ask Questions The Smart Way



#1
September 7, 2016 at 10:10:45
Use %%J, not %J. %J is for when you run it directly in the command prompt, while %%J is for use in batch files.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Report •

#2
September 7, 2016 at 19:17:28
You will also need to manage the parsing of the output, if you just want the number:
for /f "tokens=2 delims=:" %%j in ('find /c /v "" sqlquery.txt') do set /a lines=%%j+0
(adding zero removes the leading space from the number).

Report •

#3
September 7, 2016 at 23:18:24
Hi nbrane,

UH... This gives me 25:

for /f "tokens=* delims= " %%a in ('find /v /c "" ^< myfile') do (
echo.%%a
) >> NEWFILE

tokens=2 gives nothing

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
September 7, 2016 at 23:27:45
nbrane, Help

This bit of intel from set/?

! ~ - - unary operators
* / % - arithmetic operators

We'll just ignore for the moment I coulda sworn - was an arithmetic operator LOL

What are unary operators?

TIA

[I'll probably get a dope-slap from R2 or IVO]

=====================

M2 Get custom script or take private lessons


Report •

#5
September 8, 2016 at 11:57:06
It's both!
0 - 5 = Arithmetic
  - 5 = Unary

Unary just means the operator works on a single number.
! = Logical NOT (!0 = 1, !<anything else> = 0)
~ = Bit flip / Ones' complement (Add 1, then add/remove negative sign. ~5 = -6)
- = Number negation (Covered above)

[Dope slapped]

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#6
September 8, 2016 at 18:13:36
Ha! I need a slap for messing this one up. I used the pipe in my snip, which omits the "verbose" response from FIND and only emits the digit(s), so no "tokens" is needed. Without the pipe, the tokens works to extract the digit(s) from the verbose output, like: -------- myfile.txt: 12
So OP had it right, in their version using 'type' which I tried to do an end-run around by using only the FIND. Consider me well-slapped! Rainbawz had it fixed regardless.

Report •

#7
September 9, 2016 at 08:53:55
HELP

set/a z=!z is kicking my butt

I guess the ! is getting marinated because of DELAYedeXpansioN.
TIA

@echo off > NEWFILE & setLocal enableDELAYedeXpansioN
set/p Q=Q ?
echo.!Q!
set/a Q=!Q
echo.!Q!

=====================

M2 Get custom script or take private lessons


Report •

#8
September 9, 2016 at 09:35:14
✔ Best Answer
Yep, blame the delayed expansion.
@SETLOCAL EnableDelayedExpansion
@SET /P Q=Q ?
@SET /A Q=^^!Q
@ECHO !Q!

How To Ask Questions The Smart Way


Report •

#9
September 9, 2016 at 23:07:27
SHEESH

Thanks, R2

I tried ^!Q
!!Q

=====================

M2 Get custom script or take private lessons


Report •

Ask Question