解析目录列表并传递给另一个脚本?

时间:2016-10-06 09:47:56

标签: powershell

我正在尝试编写一个PowerShell脚本,它将遍历C:\驱动器中的目录,并将文件扩展名解析为另一个要使用的脚本。

基本上,目录列表的输出应该可以被逐个解析到另一个脚本。该脚本是一个编译脚本,需要解析一个参数(参数)才能编译特定的模块(文件名)。

代码:

Clear-Host $Path = "C:\SandBox\" 
Get-ChildItem $Path -recurse -force | ForEach { If ($_.extension -eq ".cob") 
    { 
        Write-Host $_.fullname 
    } 
} 
If ($_.extension -eq ".pco") 
{ 
    Write-Host $_.fullname } 
}

2 个答案:

答案 0 :(得分:0)

您不需要将输出解析为文本,不推荐使用。

这里有一些可能对你有用的东西:

# getmyfiles.ps1

Param( [string])$Path = Get-Location )

dir $Path -Recurse -Force | where { 
    $_.Extension -in @('.cob', '.pco')
}

# this is another script that calls the above

. getmyfile.ps1 -Path c:\sandbox | foreach-object {
    # $_ is a file object. I'm just printing its full path but u can do other stuff eith it 
    Write-host $_.Fullname
}

答案 1 :(得分:-1)

Clear-Host
$Path = "C:\Sandbox\"  
$Items = Get-ChildItem $Path -recurse -Include "*.cob", "*.pco" 

从你的乱码中我猜你想要返回一个包含.cob和.pco文件扩展名的文件列表。您可以使用上面的代码来收集这些代码。

$File = $Items.name 
$FullName = $items.fullname
Write-Host $Items.name 
$File
$FullName

添加上述行将允许您以各种方式显示它们。您可以选择满足您需求的那个,然后在for-each上循环遍历它们。

作为一项规则,它不是为您编写代码的地方,但您已尝试在问题中添加一些代码,因此我已经看了一下。有时你只是想要朝着正确的方向轻推。

相关问题