Subexpression失败

时间:2017-10-23 18:57:02

标签: powershell

我正在尝试将一些数据输出到某些文件,其中文件名是变量的第一个字符。我认为接近结尾的子表达式存在问题。

$var = 123,456,789
echo abc > "$($env:userprofile)\dir\$($var[0].substring(0,1)).txt"

我根据我读到的内容对其进行了格式化,但它一直在抛出错误。

1 个答案:

答案 0 :(得分:0)

如果目标目录不存在,则使用重定向运算符>将失败。

$var = 123,456,789 #this is an array of ints
$Path = $env:UserProfile + "\dir\$($var[0].ToString().Substring(0,1)).txt"

If (-not (Test-Path -Path $Path)) {New-Item -Path $Path -ItemType 'File' -Force}

'abc' > $Path