将目录名存储在列表中并显示

时间:2015-06-29 17:34:06

标签: arrays powershell scope

我正在尝试将表大量导入SQL。为此,我需要打开一个主目录,然后循环遍历这些子目录以执行实际文件。我似乎无法将这些子目录名称存储在列表中。

这是我的代码:

$path = "E:\Dictionary_SQL_Commands\";
$i = 0;

#I would like to dynamically allocate this space as I do not know the length
$global:contents = new-object object[] 10;

foreach ($item in $path){
    $global:contents[$i] = $local:item.name;
    $i++;
}

$contents已创建,我可以使用gci variable:查看,但输出为{$null,$null,...}

1 个答案:

答案 0 :(得分:2)

您不需要"列表"。这是Powershell。

# Get all File items in the given path recursively #
Get-ChildItem -Path:$path -Recurse -File | 
# Process each one as it's available #
    Foreach-Object { 
        # Get the contents of each file #
        $Contents = Get-Content $_; 
        # do other stuff with $Contents here #
    }

这是Powershell pipeline