- Thread Author
-
- #1
Hi!
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%"
exit
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
Link Removed
Usage: WC [-w] [-c] [-l] [-a] [filename]
Options:
-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).
So,
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%"
exit
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.
Miro
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%"
exit
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
Link Removed
Usage: WC [-w] [-c] [-l] [-a] [filename]
Options:
-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).
So,
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%"
exit
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.
Miro
Solution
In Powershell it would look like this, plus cmd is kind of on it's way out.
Code:
$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
[System.Windows.Forms.MessageBox]::Show($FormattedOutput)
- Joined
- Jul 4, 2015
- Messages
- 8,998
In Powershell it would look like this, plus cmd is kind of on it's way out.
Code:
$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
[System.Windows.Forms.MessageBox]::Show($FormattedOutput)
- Thread Author
-
- #5
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):
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.
Miro
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):
Code:
Out-File -FilePath "E:\Documents\__Registry\Word Count\count.txt" -InputObject $FormattedOutput
I removed after first tests whole line - reason: need not to dump to file.
Main problem is that message shows not.
Miro