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
Solved

Output of Windows Shell command to a variable

Posted on 2009-05-18
14
884 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Hyper-convergence systems have taken the IT world by storm and have quickly started to change our point of view of how the data center should and could be architected. In this article, I’ll explain the benefits of employing a hyper-converged system …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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