質問

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.

役に立ちましたか?

解決 2

The problem was that I was running the command

mvn -q dependency:tree -Dincludes^=%dependency% -DoutputFile^="%%~dpftree.out"

without using the key word call before it.

Using

call mvn -q dependency:tree -Dincludes^=%dependency% -DoutputFile^="%%~dpftree.out"

stopped the commands being printed.

他のヒント

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

I know this an old thread but I've spent hours looking for an answer which didn't worked for me and this being one of the most popular answers to this question, I'll post it here.

I've noticed that the sintax when ( or ) is being used, is really strict. So using an editor like Sublime, or Notepad++, I've found one space char just next to one ), in a 500 lines batch, that threw off the @echo off statement.

In other words( * being used to visually represent a space at the end of line):

IF %randomvar% EQU 1 (*
    goto DO 
) ELSE (
    goto DOELSE
)**

Either * or **, will mess @echo off and will output every line of code to console.

Just make sure that you don't have any unnecesary chars at the end of lines.

Cheers

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top