简体   繁体   English

如何在 PowerShell 中找到包含他部分姓名的文件

[英]How find a file with part of his name in PowerShell

I'm new to PowerShell and on StackOverflow.我是 PowerShell 和 StackOverflow 的新手。

I'm trying to write a script which gives me the Total Path to a *.pdf file, My File Name is in a variable (I get it before with previous part of code) for example: $myVariable.Nom我正在尝试编写一个脚本,它为我提供了*.pdf文件的总路径,我的文件名在一个变量中(我在之前的代码部分得到它)例如: $myVariable.Nom

After I try to find my file with this:在我尝试用这个找到我的文件后:

Get-ChildItem -Path "c:\myPath\" -Filter $myVariable.Nom 

But it doesn't work.但它不起作用。 I think I have a mistake where I try to filter with my File Name in the ChildItem command but don't know how to use it correctly.我想我有一个错误,我尝试在 ChildItem 命令中使用我的文件名进行过滤,但不知道如何正确使用它。

The goal is (if myVariable.Nom = "TOTO" for example) to have c:\myPath\TOTO.pdf目标是(if myVariable.Nom = "TOTO" for example)拥有c:\myPath\TOTO.pdf

Can anyone help me?谁能帮我? Thanks.谢谢。

The -Filter parameter on Get-ChildItem supports a wildcard search using * or ? Get-ChildItem上的-Filter参数支持使用*?的通配符搜索. . Your code is pretty close to what you're looking for, you just need to add * .您的代码与您要查找的代码非常接近,您只需添加*

Get-ChildItem -Path "c:\myPath\" -Filter "*$($myVariable.Nom)*"

Note that I'm using * at the beginning and end of the variable, meaning that the filter should search for any Item that contains the string in your variable.请注意,我在变量的开头和结尾使用*这意味着过滤器应搜索包含变量中字符串的任何项目。

In your example, you know that the File Name is TOTO so you could use * only at the end of the Filter :在您的示例中,您知道文件名是TOTO ,因此您只能在Filter的末尾使用*

-Filter "$($myVariable.Nom)*"

Remember that you can add the -Recurse flag on Get-ChildItem if you want to search recursively.请记住,如果要递归搜索,可以在Get-ChildItem上添加-Recurse标志。

Now if you want to get the FullName (Total Path) you can use any of the examples below:现在,如果您想获取FullName (总路径),您可以使用以下任何示例:

# Example 1
(Get-ChildItem -Path "c:\myPath\" -Filter "*$($myVariable.Nom)*").FullName

# Example 2
$var = Get-ChildItem -Path "c:\myPath\" -Filter "*$($myVariable.Nom)*"
$var.FullName

# Example 3
Get-ChildItem -Path "c:\myPath\" -Filter "*$($myVariable.Nom)*" |
Select-Object -Expand FullName

Edit编辑

Following your second question, how to deal with a CSV with no headers:在您的第二个问题之后,如何处理没有标题的 CSV :

$path = 'C:\path\to\csv.csv'

# Note: I'm using -Delimiter ';' because as you have shown,
# you are using semicolon as delimiter, however, if the delimiter
# are commas there is no need to use it.

$csv = Import-Csv $path -Delimiter ';' -Header 'Name','Qte'

# Now you can loop through it to find the files:

foreach($file in $csv)
{
    $var = Get-ChildItem -Path "c:\myPath\" -Filter "*$($file.Name)*"
    0..$file.Qte | foreach-object {
        $var.FullName
    }
}

Thank you for your answers: It's work fine !!感谢您的回答:它工作正常!

Now i want to improve it a litle bit: In my CSV i have 2 column: in the previous example i called the first column "Name" this is why i used myVariable.Name) for example:现在我想稍微改进一下:在我的 CSV 中,我有 2 列:在前面的示例中,我将第一列称为“名称”,这就是我使用 myVariable.Name 的原因),例如:

Name;Qte
TOTO;1
TITI;3

Now how i can select the first column if i didn't have name on first line?现在如果我在第一行没有名字,我怎么能 select 第一列? file like this one for example:像这样的文件,例如:

TOTO;1
TITI;3

Thank you谢谢

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

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