简体   繁体   English

Powershell gci中的一个功能。 将带有通配符的文件名变量传递给函数

[英]Powershell gci in a function. Pass filename variables with wildcards into the function

I was trying to pass parameters to a function to do get-childitem. 我试图将参数传递给函数以执行get-childitem。 Seems it cannot be done. 似乎无法完成。 Furthermore I can't delete my post so I'm just emptying it out with a basic explanation of what I was trying to figure out. 此外,我无法删除自己的帖子,所以我只用对我要弄清楚的内容的基本解释将其清空。 Seems I cannot pass filename variables with wildcards in them. 似乎我无法传递带有通配符的文件名变量。 Every answer in here so far does not help me as everything I've tried generates a zero byte file. 到目前为止,这里的每个答案都无济于事,因为我尝试过的所有操作都会生成一个零字节文件。

Function getFilenames($facPath,$facility,[string[]]$fileIncludes) {

    gci $facPath\* -Include $fileIncludes -Recurse | 
      Select-Object @{ expression={$_.name}; label='FILENAME' },
      @{Name='FACILITY';Expression={$facility}} | 
      Export-Csv -NoTypeInformation -Path $scriptPath"\filenames.txt" -Append

  }

getFilenames $vPath "Building 1" $vFiles

Try this! 尝试这个!

$Include=@("ab*","cd*","ef*")
Get-Childitem -Recurse -Include $Include

You can update your function to the following. 您可以将功能更新为以下内容。 I removed several Select-Object commands because you only need one. 我删除了几个Select-Object命令,因为您只需要一个。

Function getFilenames($facPath,$facility,[string[]]$fileIncludes) {

    gci $facPath -Include $fileIncludes | 
      Select-Object @{ expression={$_.name}; label='FILENAME' },
      @{Name='FACILITY';Expression={$facility}} | 
      Export-Csv -NoTypeInformation -Path "$scriptPath\filenames.txt" -Append

  }

You can run this like the following: 您可以像下面这样运行:

getFilenames "c:\folder\facilities\*" "manufacturing" "ab*","cd*","ef*"

PowerShell determines a string array by various notations: PowerShell通过各种符号来确定字符串数组:

  • "string1","string2","stringx" : Comma-separated list "string1","string2","stringx" :以逗号分隔的列表
  • [string[]]$stringArray : Typing or casting [string[]]$stringArray :键入或强制转换
  • @("string1","string2","string3") : Array sub-expression @("string1","string2","string3") :数组子表达式

See About Arrays for more information on using Arrays. 有关使用数组的更多信息,请参见关于数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM