Solved

Output of Windows Shell command to a variable

Posted on 2009-05-18
14
870 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 68

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dell E4300 9 72
Parse a Text File Based on Record Number in File 8 32
Server suggestion 2 25
RAID, not sure what Type ?? 14 67
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
this article is a guided solution for most of the common server issues in server hardware tasks we are facing in our routine job works. the topics in the following article covered are, 1) dell hardware raidlevel (Perc) 2) adding HDD 3) how t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now