Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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
Powershell 文件名和文件夹的排序列表_Powershell - Fatal编程技术网

Powershell 文件名和文件夹的排序列表

Powershell 文件名和文件夹的排序列表,powershell,Powershell,正在寻找有关我正在尝试创建的脚本的帮助。 我有一个脚本,要求用户使用Winforms通过文本框输入一些文本(来自电子邮件)。此电子邮件包含文件和文件夹的列表。 我使用一些正则表达式从电子邮件中去掉不需要的内容,并留下一个变量(称为$results),如下所示: $results file1.zip graphics\folder1 file2.zip graphics\folder1 file3.zip graphics\folder2 etc... 我的想法是,我现在需要: Extract

正在寻找有关我正在尝试创建的脚本的帮助。 我有一个脚本,要求用户使用Winforms通过文本框输入一些文本(来自电子邮件)。此电子邮件包含文件和文件夹的列表。 我使用一些正则表达式从电子邮件中去掉不需要的内容,并留下一个变量(称为$results),如下所示:

$results
file1.zip
graphics\folder1
file2.zip
graphics\folder1
file3.zip
graphics\folder2
etc...
我的想法是,我现在需要:

Extract file1.zip into graphics\folder1
Extract file2.zip into graphics\folder1
Extract file3.zip into graphics\folder2
...and so on.
我现在尝试创建一个自定义PSObject,以便稍后在脚本中调用此列表,将文件部署到正确的路径中。我很难想出如何才能做到这一点。到目前为止,我已经尝试:

$Object = New-Object -TypeName psobject
$Object | Add-Member -MemberType NoteProperty -Name File -Value $results[0]
$Object | Add-Member -MemberType NoteProperty -Name Path -Value $results[1]
$Object
这给了我一个结果:

File       Path
----       ----
file1.zip  graphics\folder1
当我尝试遍历$results变量中的每个项时

$Object = New-Object -TypeName psobject
forEach ($i in $results) {
$Object | Add-Member -MemberType NoteProperty -Name File -Value $i[0]
$Object | Add-Member -MemberType NoteProperty -Name Path -Value $i[1]
}
$Object
…我得到以下错误:

Add-Member : Cannot add a member with the name "File" because a member with that name already exists. To overwrite the member anyway, add the Force parameter to your command.
我不想写得太多,我想追加。 问题是,我不知道如何告诉PowerShell遍历$results变量中的每个项目并将其添加,以便留下:

File       Path
----       ----
file1.zip  graphics\folder1
file2.zip  graphics\folder1
file3.zip  graphics\folder2
一旦我有了它,最终的结果就是我可以在PSObject中获取结果,然后相应地提取文件

知道我做错了什么吗? 也许我把这一切都搞错了?
非常感谢您的帮助

根据您的示例,以下是我认为您正在努力实现的目标:

$Object = forEach ($i in $results) {
    [PSCustomObject]@{
        File = $i[0]
        Path = $i[1]
    } 
}
$Object
设置:

PS C:\> $Results
file1.zip
graphics\folder1
file2.zip
graphics\folder1
file3.zip
graphics\folder2
逻辑:

$Files = @{} #initializes empty hashtable

$tmp = ''
For ($i = 0; $i -le $Results.Count; $i++)
{
    If ($i -eq 0) { $tmp = $Results[$i]; Continue } #initializes tmp value

    If ($i % 2 -eq 0) { $tmp = $Results[$i] }
    Else { $Files["$tmp"] = $Results[$i] }
}
输出:

PS C:\> $Files

Name                           Value
----                           -----
file1.zip                      graphics\folder1
file3.zip                      graphics\folder2
file2.zip                      graphics\folder1

为什么要使用PSCustomObject?这就是哈希表的基本用途。我同意@TheIncorrigible1-你可以使用哈希表。(您收到错误的原因正是错误消息所说的。)是的!这是非常接近我要找的!然而,我试图实际能够分别使用每个对象。在本例中,我最终看到了列表,但无法复制项$files.Name$files.Value。这有意义吗?@obs0lete您可以使用
$Files.keys
$Files.values
访问键/值的字符串数组,并对其进行迭代。或者类似于
ForEach($Files.Keys中的文件){Copy Item$File$Files[$File]}
再次感谢您的输入。最后一个问题,如果可以的话。输出没有保持输入的顺序。我怎么能保留这个?好吧,我想我知道了。我添加了$files=[ordered]@{},这似乎有效。@obs0lete-Correct。默认哈希表不保持顺序,您所做的是在那里创建一个有序的字典。