Windows 10 CMD.EXE - How to fix command output into variable



I tried to send command output to variable.
I'm using is in Windows 10.1903 x64
I used syntax:

SET /P variable= | command

I used batch by context menu to get word, characters and lines count in text file using old WC tool (it's great).
First I used only copying output into clipboard by:

wc %1 | clip

which output format is:

extension description.txt: Words: 173 Lines: 31 Chars: 1195

I want also to show message with the same content by MSG command in syntax:

msg title "text"

So, finally batch code is this:

wc %1 | clip
SET /P count= | wc %1
msg Word count "%count%"

But new problem - file is not found - even copy to clipboard works not and here's output:

Warning! File: " E:\Documents\__Registry\Word Count\message.bat" not found.

Word Count (freeware)
TawbaWare software, August 1999

Usage: WC [-w] [-c] [-l] [-a] [filename]

-w Print word count
-c Print character count
-l Print line count
-a Print grammatical analysis

If no options are entered, program displays all items,
excluding grammatical analysis.

It looks easy to see that problem is in path (I included quotes to see it).
Q is - how to remove first space from file path? Yes, it is possible to
read %1 into variable from 2nd char, but first I checked if is %1
argument copied into variable
So, I tried this:

SET /P file= | %1
wc %file% | clip
SET /P count= | wc %file%
msg Word count "%count%"

But no output, nothing. So, before cutting path I need to put file path into variable

Really if anyone know where do I mistake, please correct me.
Thank you all.


Windows Forum Team
Staff member
In Powershell it would look like this, plus cmd is kind of on it's way out.

$FilePath = $args[0]

$Filename = Split-Path -Path $FilePath -Leaf
$Content = Get-Content -Path "$FilePath" | Out-String
$MeasuredData = Measure-Object -InputObject $Content -Line -Word -Character

$FormattedOutput = "Extension $($FileName): Words: $($MeasuredData.Words) Lines: $($MeasuredData.Lines) Chars: $($MeasuredData.Characters)"

Out-File -FilePath "Somefile.txt" -InputObject $FormattedOutput


Windows Forum Team
Staff member
Something like this


Part of Registry mod I know, but message appears not - even in script it has to be shown.
It produces text file with count only - I changed only its location.

But if we can solve message showing, is possible to modify this code (I'm not friendly with PowerShell):
Out-File -FilePath "E:\Documents\__Registry\Word Count\count.txt" -InputObject $FormattedOutput
First part creates file and 2nd defines output string which is created in code lines above this - it looks like file creation content.
I removed after first tests whole line - reason: need not to dump to file.

Main problem is that message shows not.