I am trying to read some info from a text file by using windows command line, and save it to a variable just like \"set info =1234\"
Below is the content of the txt
Knowing how to use CMD scripting deftly is great, but using PowerShell is even better. Here's one way to do it:
$revision = (( gc .\svninfo.txt | ? { $_.StartsWith( 'Revision: ' ) } ) -split ' ')[1]
What's going on?
$revision
is a variable
gc
is Get-Content
, aka type
. Each line in the file becomes a string in a sequence (pipeline).
?
is Where-Object
. It filters the objects in a pipeline based on a condition.
{}
delimits a script block
$_
is the current object in the pipeline.
-split
invokes String.Split()
in .NET, giving you an array of objects
[1]
indexes in to the array