2

I want to test the results of both the cmd commands comp and FC running in PowerShell on a series of files. I have succeeded in getting FC to run quietly in a script and yield correct results. In a cmd batch file, comp can run quietly by prefixing it with echo N |, but that is not working in PowerShell.

Here is a test script:

$LastExitCode = 99
$sItem_1 = "C:\temp\file 1.txt"
$sItem_2 = "C:\temp\file 2.txt"
write-output "Do FC:"
cmd.exe /c "FC /b ""$sItem_1"" ""$sItem_2"" > null"
write-output $LastExitCode
$LastExitCode = 99
write-output "Do comp:"
cmd.exe /c "echo N | comp ""$sItem_1"" ""$sItem_2"" > null"
write-output $LastExitCode

and here is the console output of that script:

PS> test 2.ps1
Do FC:
0
Do comp:
cmd.exe : Compare more files (Y/N) ? 
At test 2.ps1:9 char:1
+ cmd.exe /c "echo N | comp ""$sItem_1"" ""$sItem_2"" > null"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Compare more files (Y/N) ? :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS> 

In the second line of output, the zero exit code means that FC found the files to be identical. But when PowerShell attempted to run comp, it choked on the "Compare more" prompt.

How can I get this to run?

NewSites
  • 1,402
  • 2
  • 11
  • 26
  • Just to be clear, neither `comp` or `fc` are "`cmd` commands"! They are both independent commandline executable files. – Compo Aug 09 '23 at 21:46

1 Answers1

1
  • Use comp.exe's /M option to suppress the prompt for further comparisons.

    • Run comp /? to see all options.
    • Curiously, as of this writing the online docs do not mention /M.
  • There is no need to call comp.exe or fc.exe via cmd /c from PowerShell. Given that they're external programs (executables), you can invoke them directly.

    • Use >$null to silence stdout output, 2>$null to silence stderr; *>$null silences both streams.
    • In direct invocation of external programs, PowerShell never requires you to enclose variable values in "..."

Therefore:

$sItem_1 = "C:\temp\file 1.txt"
$sItem_2 = "C:\temp\file 2.txt"

"Do FC:"
fc /b$ sItem_1 $sItem_2 >$null
$LastExitCode

$LastExitCode = 99
"Do comp:"
comp /M $sItem_1 $sItem_2 >$null
$LastExitCode
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Is that `/m` switch new? A pretty obvious solution, I should have known about that! Thank you for it. – NewSites Aug 09 '23 at 19:59
  • Glad to hear it helped, @NewSites. My _guess_ is that the switch has been around for a while; `comp /?` shows it. – mklement0 Aug 09 '23 at 20:00
  • 1
    I just checked the official documentation, and it does not include the `/m` switch, even though it's dated in Feb. of this year: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/comp – NewSites Aug 10 '23 at 06:13
  • @NewSites, that's unfortunate; I've seen similar omissions in the past (and I don't think they are necessarily indicative of an option having been recently introduced). At the bottom of the page there is an option to create a GitHub issue to report a problem with the page, which I encourage you to do. – mklement0 Aug 10 '23 at 11:27