![代码](https://linux22.com/image/1568042/%E4%BB%A3%E7%A0%81.png)
我正在尝试连接不同 HashTable 中两个键提供的两个值。
代码
$caminho = @{
Aplicacao = "C:\Users\marconi.barroso\Desktop\APP";
jdk = "C:\Program Files\java\jdk1.8.0_172\bin"
}
$arquivo = @{
log = "log.txt";
apk = "lojasrede.apk"
}
if(Test-Path $caminho.Aplicacao\$arquivo.log) {
Remove-Item $caminho.Aplicacao\$arquivo.log
}
错误:
Test-Path:无法将参数与参数“Path”关联,因为它为空。行中:1 个字符:11
答案1
+ "\" +
只需在两个数组变量之间添加并将其括在括号内即可($caminho.Aplicacao + "\" + $arquivo.log)
。+
[加号] 将连接\
用 [双引号] 括起来的 [反斜杠] 字符串""
。因此,只需将 ( + "\" +
) 放在两个变量之间,您的逻辑几乎保持不变并按预期工作。
电源外壳
$caminho = @{
Aplicacao = "C:\Users\marconi.barroso\Desktop\APP";
jdk = "C:\Program Files\java\jdk1.8.0_172\bin"
}
$arquivo = @{
log = "log.txt";
apk = "lojasrede.apk"
}
if(Test-Path ($caminho.Aplicacao + "\" + $arquivo.log)) {
Remove-Item ($caminho.Aplicacao + "\" + $arquivo.log)
}
更多资源
答案2
有几种方法,
if(Test-Path (Join-Path $caminho.Aplicacao $arquivo.log)) {
Remove-Item (Join-Path $caminho.Aplicacao $arquivo.log)}
}
我会首先使用 Join-Path 并将路径存储在变量中,因为它至少会被使用两次。