0

Below is a simplified snippet of my code:

@echo off    
setlocal enabledelayedexpansion
    for /f %%f in ('%pomFiles%') do (
        findstr "var" %%f > nul
        if errorlevel 0 if not errorlevel 1 (
            cd "%%~dpf"
            for /f "usebackq" %%i in ('%%~dpftree.out') do ( set size=%%~zi && echo !size!)
            if !size! gtr 0 (
                   //do  stuff
            )
        )
    )

The !size! variable does not seem to be being set. If I do echo !size!, it prints !size!. How can I make sure !size! evaluates?

3
  • possible duplicate of Batch file printing out commands with @echo off Commented Feb 4, 2013 at 15:09
  • better to edit your original question as you home in on the problem, rather than post a whole new question. Commented Feb 4, 2013 at 15:09
  • @Vicky, I have fixed the issue of the commands printing due to your suggestion which I will mark as an answer if you post it. However I felt this was a completely different issue... Commented Feb 4, 2013 at 15:12

1 Answer 1

1

It's because you are using for /f, which is to read the contents of a file, hence the problem is you are trying to get the size of the variable %%i, which is set as the first line in the file.

You just need a regular for loop

for %%i in ("%%~dpftree.out") do ( set size=%%~zi && echo !size!)
if !size! gtr 0 (
    //do  stuff
)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the suggestion Bali C, but even with this code change it still seems to print out !size! rather than the value. If I write echo %%~zi the size does print...
Hmm, you have enabled delayed expansion haven't you? I know it's in your example, but it sounds like you don't. I just tested it and it works fine for me?
Yes, it is the first line before the first for-loop as shown in my example. That should enable delayed expansion for the inner-loop as well right? Given your proposed new for statement, is it sufficient to put the if !size! gtr 0 in the body of the for loop and change !size! for %%~zi? This loop will only every run once, right?
Yeah, as long as it's there it should work. In theory yes that should work fine, try that without assigning it to size first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.