Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Powershell连接路径返回数组_Arrays_Powershell_Join_Path - Fatal编程技术网

Arrays Powershell连接路径返回数组

Arrays Powershell连接路径返回数组,arrays,powershell,join,path,Arrays,Powershell,Join,Path,我试图在脚本开始时创建文件夹,然后在这些文件夹中创建文件。为了创建路径,我使用了md-Force$path,我还使用它来测试类似的方法 if(![System.IO.Directory]::Exists($path)){ [System.IO.Directory]::CreateDirectory($path) } 在创建了我想要构建的文件名+路径的路径后,我选择了两种方式: Join-Path $path "myfile.txt" 这导致我得到一个字符串数组作为结果,有两条记录,都

我试图在脚本开始时创建文件夹,然后在这些文件夹中创建文件。为了创建路径,我使用了
md-Force$path
,我还使用它来测试类似的方法

if(![System.IO.Directory]::Exists($path)){
    [System.IO.Directory]::CreateDirectory($path)
}
在创建了我想要构建的文件名+路径的路径后,我选择了两种方式:

Join-Path $path "myfile.txt"
这导致我得到一个字符串数组作为结果,有两条记录,都构建了相同的路径
$path\myfile.txt
也使用

[System.IO.Path]::Combine($path, "myfile.txt")
返回具有两条记录的相同字符串数组(即使combine方法只有一个用于返回字符串的定义)

这种行为只有在我以前创建文件夹时才会发生,因此我想知道是什么导致这些结果,以及如何避免这些结果


我的设置是带Win7的Powershell 3(客户“希望”)。

您的变量
$path
包含一个数组

PS C:\> $path = 'C:\Temp'
PS C:\> $res = Join-Path $path 'file.txt'
PS C:\> $res.GetType().FullName
System.String
PS C:\> $res
C:\Temp\file.txt
PS C:\> $path = 'C:\Temp', 'C:\Windows'
PS C:\> $res = Join-Path $path 'file.txt'
PS C:\> $res.GetType().FullName
System.Object[]
PS C:\> $res
C:\Temp\file.txt
C:\Windows\file.txt
PS C:\>$path='C:\Temp'
PS C:\>$res=连接路径$Path'file.txt'
PS C:\>$res.GetType().FullName
系统字符串
PS C:\>$res
C:\Temp\file.txt
PS C:\>$path='C:\Temp','C:\Windows'
PS C:\>$res=连接路径$Path'file.txt'
PS C:\>$res.GetType().FullName
系统对象[]
PS C:\>$res
C:\Temp\file.txt

C:\Windows\file.txt
请再添加一点代码,foreach块中是否使用了连接路径?嘿,谢谢。用括号调用我的函数。在我的路径上形成了一个数组。