gpt4 book ai didi

windows - 如何使用 powershell 字符串数组在特定文件夹中搜索 xml 文件

转载 作者:可可西里 更新时间:2023-11-01 11:26:13 24 4
gpt4 key购买 nike

我想在特定的源文件夹中搜索字符串。如果我找到一个具有该名称的 .xml 文件,我想将该文件复制到特定的目标目录中。这是我目前所拥有的:

$SourceDirectory =   "D:\MessageQueue_Backup\DPGIN_Backup\ETIMS"
$DestinationDirectory = "C:\Destination"

$orderNumberArray = "1F-16CG-2-94JG-60-2.R6C3.20864",
"1F-16CG-2-28JG-10-2.R3C4.21488",
"1F-16CJ-2-75JG-00-21.R4C2.21487",
"1F-16CG-2-70JG-10-41.R5C3.21586",
"1F-16CG-2-32JG-90-1.R2C2.22733",
"1F-16CG-2-33JG-00-1.R5C4.23044",
"1F-16CG-2-80JG-10-11.R5C4.22660",
"1F-16CG-2-94JG-10-2.R7C3.23046",
"1F-16CG-2-94JG-60-6.R10C7.23031",
"1F-16CJ-2-24JG-30-1.R3C1.23036",
"1F-16CJ-2-28JG-40-1.R4C3.22737",
"1F-16CJ-2-32JG-40-1.R7C3.22728",
"1F-16CJ-2-32JG-90-1.R2C2.22734",
"1F-16CJ-6-11.R14C5.24295",
"1F-16CJ-2-34JG-50-1.R6C7.2,5266"

foreach ($element in $orderNumberArray)
{

If (Get-ChildItem $_.FullName | Select-String -Pattern $element)
{
Copy-Item $file.FullName -Destination $DestinationDirectory
}

}

我的问题是,我不知道如何搜索 SourceDirectory :(顺便说一句,SourceFolder 中只有 xml 文件

最佳答案

像这样的东西应该可以工作:

$sourceDirectory      = "D:\MessageQueue_Backup\DPGIN_Backup\ETIMS"
$destinationDirectory = "C:\Destination"

$orderNumbers = "1F-16CG-2-94JG-60-2.R6C3.20864",
"1F-16CG-2-28JG-10-2.R3C4.21488",
"1F-16CJ-2-75JG-00-21.R4C2.21487",
"1F-16CG-2-70JG-10-41.R5C3.21586",
"1F-16CG-2-32JG-90-1.R2C2.22733",
"1F-16CG-2-33JG-00-1.R5C4.23044",
"1F-16CG-2-80JG-10-11.R5C4.22660",
"1F-16CG-2-94JG-10-2.R7C3.23046",
"1F-16CG-2-94JG-60-6.R10C7.23031",
"1F-16CJ-2-24JG-30-1.R3C1.23036",
"1F-16CJ-2-28JG-40-1.R4C3.22737",
"1F-16CJ-2-32JG-40-1.R7C3.22728",
"1F-16CJ-2-32JG-90-1.R2C2.22734",
"1F-16CJ-6-11.R14C5.24295",
"1F-16CJ-2-34JG-50-1.R6C7.2,5266"

Get-ChildItem $sourceDirectory -Filter *.xml | Where-Object {
$basename = $_.BaseName
$orderNumbers | Where-Object { $basename -like "*$_" }
} | Copy-Item -Destination $destinationDirectory

过滤器检查每个文件的基本名称(不带扩展名的文件名)是否以任何订单号结尾(-like "*$_")。

关于windows - 如何使用 powershell 字符串数组在特定文件夹中搜索 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34637676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com