Batch file printing out commands with @echo off

Alex picture Alex · Feb 4, 2013 · Viewed 7.6k times · Source

I am searching for certain XML files and then performing a command on them (mvn) which writes an output to a tree.out file. If this file is empty then I know there was no output (from mvn), so I don't print it.

However, after the first part of the loop execution it prints out from set size and then simply prints out the commands of the next loop iterations.

setlocal enabledelayedexpansion
set pomFiles=dir /s/b pom.xml
@echo off

for /f %%f in ('%pomFiles%') do (
    findstr "<packaging>pom</packaging>" %%f > nul
    if errorlevel 0 if not errorlevel 1 (
        cd "%%~dpf"
        mvn -q dependency:tree -Dincludes^=%dependency% -DoutputFile^="%%~dpftree.out"
        for /f %%i in ("%%~dpftree.out") do set size=%%~zi
        if !size! gtr 0 (
            type "%%~dpftree.out"
        )
        del "%%~dpftree.out"        
    )
)

I presume there is an error in my code which is causing the statements to print, but I cannot see what the issue is myself.

Answer

user2033427 picture user2033427 · Feb 4, 2013

The syntax of for /f for files is

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]

but in your code the file-set is ("%%~dpftree.out") which is for strings.

To use double quotes, I think you have to use the usebackq option:

for /f "usebackq" %%i in ('%%~dpftree.out') do set size=%%~zi

Hope this helps