0

I am trying to parse a multi-line output and struggling with a few things. I'm trying to write a script that will execute the 'diff' command and identify files that have been modified or added to a core project. There script accepts 2 input parameters for the top level of the master repository and the top level of the modified project. The code is:

for /f "delims=" %%a in ('diff -qr "%path1%" "%path2%"') do (
    SET output=%%a
    echo a:%%a
    echo output:%output%
    ...
)

The first problem I'm having is that the output from this is:

a:Only in <filename>
output:

the SET output command does not seem to assign the variable the value. This is making processing the output a challenge. What I'd like to do is separate the diff results into different lists. The diff utility generates 3 possible output:

  1. Only in <path1 filename>
  2. Files <path1 filename> and <path2 filename> differ
  3. Only in <path2 filename>

I'm trying to identify which type of output the output string belongs to and then to strip out only the file name for the path1 files, but the code I've used before to test for contained strings doesn't work with the %%a variable; i.e.

SET newFile=Only^ in^ 
if NOT "x%%a:%newFile%=%% == "x%%a%%" do ...

Any help is appreciated. Thanks

New contributor
Barry Andreasen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

For the first problem, you will find it much easier to use batch functions. You could use delayed expansion but functions make your batch much easier to read and understand.

Note, goto :EOF is like saying "return" in any other language.

Instead of:

for /f "delims=" %%a in ('diff -qr "%path1%" "%path2%"') do (
    SET output=%%a
    echo a:%%a
    echo output:%output%
    ...
   )

Try:

for /f "delims=" %%a in ('diff -qr "%path1%" "%path2%"') do call :parse_results "%%a"
goto :EOF

:parse_results
Set output=%~1
echo a:%~1
echo output:%output%
goto :EOF

The second problem isn't so easy.. you have two problems here...

The first problem we solved above [WHICH IS] that you can't do string substitution on %%a, you need a whole variable for it (aka %output:old=new%)

[BAD INFO - not true] The second problem is that string substitution doesn't seem to work against variables (someone correct me if I am wrong).. there are ways around this depending on your use case.

[NEW INFO - should be true] it appears that you CAN to substitution using a variable now.. it didn't used to work.. so this should do it for you. :)

Your Answer

Barry Andreasen is a new contributor. Be nice, and check out our Code of Conduct.

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.