Solved

Output of Windows Shell command to a variable

Posted on 2009-05-18
14
880 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Usually shares are where we want them for our users and we tend to take them for granted. There are times, however, when those shares may disappear causing difficulty for your users. One of the first things to try is searching for files that shou…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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