DOS batch command to read some info from text file

后端 未结 5 937
醉话见心
醉话见心 2021-01-19 03:16

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

5条回答
  •  一向
    一向 (楼主)
    2021-01-19 03:52

    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

提交回复
热议问题