Solved

Output of Windows Shell command to a variable

Posted on 2009-05-18
14
890 Views
Last Modified: 2012-05-07
I need to write a code that saves the below output of a MS DOS Command output to a variable

FC File1.txt File2.txt

Output
Comparing Files File1.txt and File2.txt
FC: no differences encountered
0
Comment
Question by:Nathan_nyc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
14 Comments
 

Expert Comment

by:smjunk
ID: 24417694
try
FC File1.txt File2.txt > output.txt
0
 
LVL 2

Expert Comment

by:Manoj_a
ID: 24417999
0
 
LVL 2

Expert Comment

by:Manoj_a
ID: 24418015
smjunk, that would redirect it to a file.
Nathan, you could use this example :

REM Test
FC Notes.txt Notes.txt > %temp%\x.txt & set /p test="" <%temp%\x.txt
echo %test%

O/P - Comparing files Notes.txt and NOTES.TXT
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 24418598
Only thing with this approach is that a variable can only contain one line and the set /p will just assign the first line of output.

What do you want to do with this output, we can use a for comand to act on each line in turn or check the errorlevel returned to see if there were differences or not?

e.g.
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('fc file1.txt file2.txt') do (set line=%%a
echo next line is !line!
)
echo Last line was !line!
0
 
LVL 38

Accepted Solution

by:
BillDL earned 500 total points
ID: 24424301
Nathan

Your question assumes that you will always receive the "FC: no differences encountered" result, and that is what you need to set as a variable.  What do you want it to do if FC returns a mismatch result that lists the differing lines one above the other.

The simplest way to get an instant result of a Yes files match, or No files are different is to use the %errorlevel% return.  The results of a FC command are already stored as a variable ready for you to use.

When a program runs and does something, the result of the last action is stored in a variable named %errorlevel%.  In general, an errorlevel of Zero means success, and a 1 means failure, but because there can be a number of reasons for "failure" there are sometimes additional errorlevels returned by the program.

The following batch file shows this in action and hides the normal FC screen output:

@echo off
fc File1.txt File2.txt > nul
echo.
echo The Return Code for the last command (FC) is: %errorlevel%
echo.
pause
exit

The redirect to > nul (sends output to nul device to hide it) and the echo. lines (they just echo a blank line to screen) are not treated as the "last command executed" and don't return a success or fail code, so in the above example the Return Code is for the FC command.

FC will return the Code 2 if one or other, or both, of the files named in the command is not found.  Actually, the Return Code of 2 just means that an error occurred when FC tried to execute, and not necessarily that files were not found to compare.

0  Command completed successfully and no mismatches were found
1  Command completed successfully and one or more mismatches were found
2  (or higher) FC command could not complete due to an error.

Theoretically there can be 256 Return Codes (0 to 255), but it depends what the programmer chose to make available.  255 is often used by programmers to say that a command was called with the wrong syntax.  In the case of the FC command, if you try and run it with the wrong syntax, eg:
fc File1.txt
it will usually return a code of -1 but it depends on what you got wrong in the syntax.

So, you can use the batch file to test the Return Code and do whatever you want depending on the result.  Here is a very long-winded batch file that demonstrates this.  You just specify the path to FILE1 and FILE2 in the two SET lines at the start.  If they are in the same folder as the batch file you don't need a path, just the file names.

Is this useful to you?

@echo off
 
set FILE1=C:\Unzip\File_Compare\source1\File1.txt
set FILE2=C:\Unzip\File_Compare\source2\File2.txt
 
fc "%FILE1%" "%FILE2%" > nul
 
if errorlevel 2 goto :ERR1
if errorlevel 1 goto :DIFF
if errorlevel 0 goto :SAME 
 
:ERR1
cls
echo.
echo 	ERROR!!
echo.
echo The FC command did not execute properly or completely.
echo.
echo Most likely cause is that one or more of the named
echo files do not exist where they are expected to be.
echo.
echo First file was : %FILE1%
echo Second file was: %FILE2%
echo.
echo Press any key to quit ...
pause > nul
goto :END
 
:DIFF
cls
echo.
echo %FILE1%
echo 	*** DOES NOT MATCH ***
echo %FILE2%
echo.
echo Press any key to quit ...
pause > nul
goto :END
 
:SAME
cls
echo.
echo %FILE1%
echo 	MATCHES
echo %FILE2%
echo.
echo Press any key to quit ...
pause > nul
goto :END
 
:END
exit

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
ID: 24427155
Nice work BillDL
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 28915038
Would be shame to lose BillDL's long explanation of errorlevels there.  Sugest Accept BillDL, perhaps with a little Assist for dragon-it as I did actually answer the original question too.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 28915040
Would be shame to lose BillDL's long explanation of errorlevels there.  Sugest Accept BillDL, perhaps with a little Assist for dragon-it as I did actually answer the original question too.
0
 
LVL 38

Expert Comment

by:BillDL
ID: 29074566
Thank you dragon-it and modus_operandi :-)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 29078311
Should paste that one into an article Bill...
0
 
LVL 38

Expert Comment

by:BillDL
ID: 29209093
Hmmm.  I'm not sure that I am into articles.  There have been some fine ones posted by t0t0, Qlemo, and probably yourself, but they contain quite specific and in many ways unique or surprising findings.  My comment is really pretty generic, albeit probably explained in a reasonably well structured way.  So I think I might pass.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 29504504
Bill,

Having my Page Editor hat on: That would indeed make part of a good article. If you enrich it with how to evaluate the errorlevel (with both if errorlevel and %errorlevel%), how to produce an error level (exit), how to make fail/success decisions (&& and ||), and maybe even more I can imagine at the moment, that would be great. We need articles based on all levels of knowledge - a more general and/or basic one is pretty much welcome!


Steve,

I appreciate your objection. That description is something I did not see at Cleanup, and it is worth keeping.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question