Page 1 of 1

How to write command line output to text file?

Posted: 2011-02-01T12:46:48-07:00
by fatejd
Hi -

I'm somewhat new to command line scripting (Windows XP), and am now stumped about how to get the results of an IM command and write it to a text file. I'm comparing images and would like to get the output (number of pixels that are different) and write it to a text file. Or, better yet, get the result in the script and assign it to a variable for further manipulation.

Here is what I have for the IM part of my script:

Code: Select all

compare -metric AE %currTest%.png %fileName%.png %fileName%_DIFF.png>>results.txt
The variables are file names and directories and some input test numbers. This command writes nothing to the results.txt file though. The output is written to the screen:

Code: Select all

C:\Screenshots>"c:\Documents and Settings\xxxxxxxx\Desktop\test_script.bat" TEST1
1480
Any ideas?

Thanks

Re: How to write command line output to text file?

Posted: 2011-02-01T13:49:17-07:00
by el_supremo
The output from the compare command is sent to the error output file descriptor 2. So use this command:

Code: Select all

compare -metric AE %currTest%.png %fileName%.png %fileName%_DIFF.png 2>results.txt
Pete

Re: How to write command line output to text file?

Posted: 2011-02-01T15:18:55-07:00
by fatejd
Thanks for the reply.

Of course, shortly after I posted this I figured out a way to do this, with a variable as well:

Code: Select all

FOR /F %%A in ('compare -metric AE %currTest%.png %fileName%.png %fileName%_DIFF.png 2^>^&1') DO set pixelDiff=%%A

Re: How to write command line output to text file?

Posted: 2011-04-05T05:45:49-07:00
by lizac
Hi! Your code is what i needed... Do you know how to convert it in vbscript?

I would actually need the AE value to test if it's equal to zero or not. I'm having a hard time looking for resources where I need to pass the value of batch result to use in my vbscript. Once the value is tested, if it's equal to zero then the script will exit, otherwise it will send an email to me. Thank you in advance.

Re: How to write command line output to text file?

Posted: 2011-04-05T13:16:42-07:00
by whugemann
lizac wrote:Hi! Your code is what i needed... Do you know how to convert it in vbscript?
See http://www.imagemagick.org/Usage/windows/#vb_example how you cann read the result of an operation into a VBScript.

Wolfgang Hugemann

Re: How to write command line output to text file?

Posted: 2011-04-05T19:19:06-07:00
by anthony
Example do not detail the handling of standard error output in windows. If there even is such a concept in BAT scripts.

Re: How to write command line output to text file?

Posted: 2011-04-06T00:35:26-07:00
by whugemann
anthony wrote:Example do not detail the handling of standard error output in windows. If there even is such a concept in BAT scripts.
The example demonstrates how you read stdout of a DOS command into VBScript. You can then perform the testing of the value within VBScript and act upon the result. So I think that this would do what what is asked for over here. But I will check whether there is a possibility to read stderr back into VBScript.

Wolfgang

Re: How to write command line output to text file?

Posted: 2016-10-29T09:37:00-07:00
by pctechtv
Where do you find the output results.txt file after running command? The command is working. I get my jpg output, but I don't have any results file afterwards. My command looks like this

Code: Select all

magick -density 300 "C:\CTestAC1\Pointer.pdf"[0] -crop %[w]x%[fx:w*0.5625]+0+0 "C:\CTestAC1\Pointer.jpg" 2>results.txt
Thanks

Re: How to write command line output to text file?

Posted: 2016-10-29T09:41:20-07:00
by pctechtv
OK I see the example is only for the compare command. The title for the post attracted me because I need the command line output in a text file. How can this be accomplished? All I really need is a file-size and name report output in a text file. Thanks

Re: How to write command line output to text file?

Posted: 2016-10-29T09:48:47-07:00
by pctechtv
Thanks

Re: How to write command line output to text file?

Posted: 2016-10-29T10:01:36-07:00
by fmw42
Please do not add a post to a very old topic. Also always provide your IM version and platform. Please read viewtopic.php?f=1&t=9620

When you crop an image, it makes new images. You say you want a text file.

Code: Select all

 But what information should be in the text file?
Raster data from the crop new images would not normally be put into a text file.

Images can be written to text, but you would then replace the output jpg with a textfile.txt. But that will be a huge amount of text, one line of text information for every pixel.

Re: How to write command line output to text file?

Posted: 2016-10-29T10:08:15-07:00
by pctechtv

Code: Select all

version: ImageMagick-7.0.3-Q16
I just need the jpg file name and the file size in a text file. I don't need any conversion of the jpg output to text. I want this in same directory that my jpg is output to. I want it generated with the same command that creates the jpg output. That is why I thought the command above with the pipe output was what I am looking for. When my command is finished, I want to a jpg of my PDF, and text file reporting the name and file size (kb) of the jpg. Thanks

Re: How to write command line output to text file?

Posted: 2016-10-29T10:23:30-07:00
by fmw42
There is no output image, yet, to get its name, so this would only get the output size

Code: Select all

magick logo: -crop 500x300+100+100 -format "%w %h" -write info:tmp.txt logo_crop.jpg
So the text file will only have
500 300

If you want the output name, then you need a second command

Code: Select all

magick logo: -crop 500x300+100+100 logo_crop.jpg
magick logo_crop.jpg -format "%f %w %h" info: > tmp.txt
Here the textfile will have
logo_crop.jpg 500 300

Re: How to write command line output to text file?

Posted: 2016-10-29T10:32:23-07:00
by pctechtv
Yes... no file so I need second command after! I get it. thank you. The two commands will be fine, that is exactly what I am looking for. Thanks