【发布时间】:2018-09-12 17:01:05
【问题描述】:
我需要使用 .txt 作为 powershell 命令的输入,并且需要将其输出到新行中的同一文件。
我应该使用什么样的命令?
我只尝试过基本的东西,例如
C:\"bunch O'shit"\input.txt *2
"C:\"bunch O'shit"\input.txt" *2
这两种方法都没有奏效。
麻烦各位。
【问题讨论】:
标签: powershell
我需要使用 .txt 作为 powershell 命令的输入,并且需要将其输出到新行中的同一文件。
我应该使用什么样的命令?
我只尝试过基本的东西,例如
C:\"bunch O'shit"\input.txt *2
"C:\"bunch O'shit"\input.txt" *2
这两种方法都没有奏效。
麻烦各位。
【问题讨论】:
标签: powershell
您需要先阅读文件的内容,然后才能开始处理它。要使用 PowerShell 读取文件,您可以使用 Get-Contentcmdlet。
获得文件内容后,您需要将其转换为可用于计算的类型,即您需要进行类型转换。
假设变量$num 是一个字符串,你可以这样做:
[int]$num
这将从字符串中创建一个整数。
您可以使用Get-Membercmdlet 检查变量的类型。
将所有这些放在一起:
# For the sake of it, let's create a file containing a single number
"2" | Out-File -FilePath .\number.txt
# Let's read the file we just created and save the result in a variable
$num = Get-Content .\number.txt
# Do the type cast and multiply the result with another number
[int]$num * 2
您可以随时将结果通过管道传输到Get-Member 并检查它是什么类型。
【讨论】: