0

In working on a script that computes, and outputs to a file, the total cyclomatic complexities of a project under test, I am trying to filter out files whose names (including path) contains "test". (Such files only exist for testing purposes, and are therefore exempt from computation.)

So far, I have the following code:

rem write temporary file, to append command line output to
SETLOCAL 
SET tmpfile=tmp_ComplexityAnalysis.txt
echo. 2>%tmpfile%
rem perform cyclomatic complexity analysis on all the files, iff those files are not test files and have some functions in them
echo %tmpfile%
for /R apiserver_sdk %%G in (*.go) DO (
    rem filter out "test" files
    gocyclo %%G >> %tmpfile%
)

I don't know how to exclude "test" or where.

UPDATE: Changing the body of the for loop to :

DIR /A %%G| findstr test
IF %ERRORLEVEL% NEQ 0 (
    rem filter out "test" files
    gocyclo %%G >> %tmpfile%
)

doesn't work because, somehow, %ERRORLEVEL% is always zero.

2
  • I can confirm your result, that %ErrorLevel% is always zero. I get the same result with find. The grep.exe port of the Unix utility gives the correct return status, but I'm looking for a work-round using standard commands. I need to go out now: if no-one has come with an answer by the time I get back, I'll look into it further. By the way, why use DIR /A %%G|..., rather than echo %%G|...?
    – AFH
    Sep 19, 2018 at 18:39
  • 1
    Yup. That would be me! My day job is software development Sep 19, 2018 at 21:26

2 Answers 2

2

As the questioner has found, %ErrorLevel% is not being set. I don't know whether delayed expansion and !ErrorLevel! would work, but what I found was that findstr is setting its return value, so either of the following scripts works:-

for /R apiserver_sdk %%G in (*.go) DO (
    rem filter out "test" files
    echo %%G | findstr /i test
    if errorlevel 1 ( 
        gocyclo %%G >> %tmpfile%
    )
)

or:-

for /R apiserver_sdk %%G in (*.go) DO (
    rem filter out "test" files
    echo %%G | findstr /i test || (
        gocyclo %%G >> %tmpfile%
    )
)

If the gocyclo command is the only one needed, the command group surrounding it could be removed.

1
  • That was simple! Sep 20, 2018 at 13:11
0
DIR /B | FOR /F %x IN ('FIND "test"') DO (
    ECHO Execute something using "%x"
)

Backwards, to exclude files which contains "test" substring in a name - add /V key to the FIND command.

screenshot

this needs to happen for all the .go files in the directory that don't have "test" in their pathnames

DIR /B *.go | FOR /F %x IN ('FIND /V "test"') DO(
    ECHO Execute something using "%x"
)
4
  • It didn't work. I get this at runtime: C:\Users\mwarren\Desktop>echo tmp_ComplexityAnalysis.txt tmp_ComplexityAnalysis.txt IN was unexpected at this time. C:\Users\mwarren\Desktop> DIR /B | FOR /F x IN ('FIND /V "test"') DO ( Prepending % to the variables doesn't make it work, either. Also, where do you use %%G in your solution? Sep 19, 2018 at 19:31
  • It didn't work. Method works - screenshot added. Apply it in your code. where do you use %%G in your solution? I use %x - replace if need.
    – Akina
    Sep 19, 2018 at 19:49
  • I simply inserted your for loop into mine. Keep in mind that this needs to happen for all the .go files in the directory that don't have "test" in their pathnames Sep 19, 2018 at 20:27
  • This is a clumsy way of doing for %f in (*test*) do ( ... ), which, as the questioner says, is the opposite of what he wants.
    – AFH
    Sep 19, 2018 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .