Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

Output of Windows Shell command to a variable

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
Nathan_nyc
Asked:
Nathan_nyc
  • 4
  • 3
  • 2
  • +3
1 Solution
 
smjunkCommented:
try
FC File1.txt File2.txt > output.txt
0
 
Manoj_aCommented:
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Steve KnightIT ConsultancyCommented:
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
 
BillDLCommented:
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
 
t0t0Commented:
Nice work BillDL
0
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
BillDLCommented:
Thank you dragon-it and modus_operandi :-)
0
 
Steve KnightIT ConsultancyCommented:
Should paste that one into an article Bill...
0
 
BillDLCommented:
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
 
QlemoC++ DeveloperCommented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now