Batch file searching multiline command output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
theirishmole
Posts: 1
Joined: 06 Mar 2012 11:24

Batch file searching multiline command output

#1 Post by theirishmole » 06 Mar 2012 11:43

I am calling a microsoft code coverage instrumentation tool, on each exe found in the folder from which the bat file is launched, which takes command line arguments.
i want to be able to search through the command output for several different error conditions.

I have tried outputting the standard error to a tmp file and reading it back in. this does not appear to work reliably however.
i notice that even if the tmp file is updated i do not always read back in what has been written to it.
I sometimes see i am reading what was previously in the file, even though i am deleting the file each time, maybe there is some buffering i don't know about?

The other issue with the file reading is that the command i am using only seems to be able to read in one line, when i want to read all the contents, as there may be several errors.

There has to be a better way of doing what i want to do :?

Here is the code:

Code: Select all

@echo off
set VSINSTRPATH=%~1
set INSTRUMENTATIONDIR=%~2
echo using vsinstr path: %VSINSTRPATH%
echo storing instrumented files: %INSTRUMENTATIONDIR%
del logFile.txt
set TOSIGNDIR=tosign
mkdir %TOSIGNDIR%

echo.
echo Instrmenting exe's
For %%X in (*.exe) do (
   echo Instrumenting %%X >> logFile.txt
   "%VSINSTRPATH%\vsinstr.exe" -coverage %%X /OUTPUTPATH:%INSTRUMENTATIONDIR% 2> tmpFile
   set /p vsinstrOutput= < tmpFile
   del tmpFile
   
   echo Start -- %vsinstrOutput% -- end
   echo %vsinstrOutput% >> logFile.txt

   echo %vsinstrOutput% | find "VSP2013"
   if NOT errorlevel 1 echo VSP2013 found, Program will need to ruin as 32bit application
   echo %vsinstrOutput% | find "VSP1011"
   if NOT errorlevel 1 echo VSP1011 found, Need profiler flag to be enabled, error logged, no other action to take
   echo %vsinstrOutput% | find "VSP2001"
   if NOT errorlevel 1 (
      echo VSP2001 found, exe/dll will need to be signed, Move instrumented files to tosign folder
      move /y ".\"%INSTRUMENTATIONDIR%"\"%%X ".\"%TOSIGNDIR%   
   )
   echo %vsinstrOutput% | find "VSP1030"
   if NOT errorlevel 1 echo VSP1030 found Invalid, mismatched, or no PDB file was found, log error no other action to take
   echo %vsinstrOutput% | find "VSP1005"
   if NOT errorlevel 1 echo VSP1005 found Command line error: instrumentedfiles is invalid - OUTPUTPATH must refer to a valid, local directory
)


The above file is being called like this: fileoptest.bat "C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools" instrumentedfiles

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file searching multiline command output

#2 Post by Squashman » 06 Mar 2012 12:07

You need to use Delayed Expansion with your variables inside the FOR LOOP.

SET /P will only read in the first line when using a file as input. If you need to read the entire file then you need to use a FOR loop to read it.

Not understanding why you are outputting the errors to a temp file and then reading it back into a variable and then outputting that error to another Log File. :?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file searching multiline command output

#3 Post by foxidrive » 06 Mar 2012 12:26

Is the error message always one line?

Here's a functionally similar way of coding it.

Code: Select all

@echo off
set VSINSTRPATH=%~1
set INSTRUMENTATIONDIR=%~2
echo using vsinstr path: %VSINSTRPATH%
echo storing instrumented files: %INSTRUMENTATIONDIR%
del logFile.txt
set TOSIGNDIR=tosign
mkdir %TOSIGNDIR%

echo.
echo Instrumenting exe's
For %%X in (*.exe) do (
   echo Instrumenting %%X >> logFile.txt
   "%VSINSTRPATH%\vsinstr.exe" -coverage %%X /OUTPUTPATH:%INSTRUMENTATIONDIR% 2> tmpFile
   echo Start --
   type tempfile
   echo End   --
   type tmpfile >> logFile.txt

   find "VSP2013" < tmpFile >nul && (
   echo VSP2013 found, Program will need to run as 32bit application
   )
   find "VSP1011" < tmpFile >nul && (
   echo VSP1011 found, Need profiler flag to be enabled, error logged, no other action to take
   )
   find "VSP2001" < tmpFile >nul && (
      echo VSP2001 found, exe/dll will need to be signed, Move instrumented files to tosign folder
      move /y ".\"%INSTRUMENTATIONDIR%"\"%%X ".\"%TOSIGNDIR%   
   )
   find "VSP1030" < tmpFile >nul && (
   echo VSP1030 found Invalid, mismatched, or no PDB file was found, log error no other action to take
   )
   find "VSP1005" < tmpFile >nul && (
   echo VSP1005 found Command line error: instrumentedfiles is invalid - OUTPUTPATH must refer to a valid, local directory
   )   
del tmpfile 2>nul
)   

Post Reply