I am trying to calculate the length of an output from a set. It is very important to me I try to calculate this with out enable delayaedexpansion just raw batch.

@echo off
set aaa=asdf
  for %%a in ("%aaa%") do (
  set /a bbb=%%~za + 0 
  echo %bbb%

If I do not add the +0, it keeps returning missing operand. If I put the +0 on , it keeps returning ECHO is off. This is not the first time I have tried to do a numerical echo. I am confused as to why letters will echo just fine but numbers will not.

1 Answer 1


I think that %%~z only works for files, not for variables. So, write the contents of the variable to a file, and get the file size.

What about this:

@echo off
set aaa=asdf

:: Write variable to file
set tmpfile=%TEMP%\%RANDOM%
echo %aaa%> %tmpfile%

:: Determine filesize, subtract 2 bytes for newline
for /F "usebackq" %%a in ('%tmpfile%') do set /a size=%%~za - 2

del %tmpfile%

echo Size: %size%
  • I am new at this, I am not sure if this is the correct answer or just one of many. It works for what I need. Almost exactly what I was going for. I am trying to teach my self this from web site searches, I feel I am doing pretty well, I just do not get all the lingo. After 30 minutes of someone on here barking rudely and then deleting everything we discussed. I researched more and realized this is called stringlength, So Berend I am very appreciative you read what I wrote and did not just tell me, I see your script is broken, what are you going to do about it. Sep 14, 2016 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.