1

I want to search for strings in a specific source folder. If I find a .xml file with that name, I want to copy that file into specific destination directory. This is what I have so far:

$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
    }

}

My problem is, I don't know how to search SourceDirectory :( Btw, there are only xml files in SourceFolder

2
  • What exactly do you mean by "search"? Are you looking for files with one of the order numbers in their name or in their content? Commented Jan 6, 2016 at 16:31
  • I need to find and copy all xml files that have name that ends with string provided in array Commented Jan 6, 2016 at 18:12

3 Answers 3

1

Something like this should work:

$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

The filter checks if the basename (the filename without extension) of each file ends with any of the order numbers (-like "*$_").

Sign up to request clarification or add additional context in comments.

Comments

1

this might work

$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*"
)

dir $SourceDirectory -Include $orderNumberArray | % {copy $_.fullname $destinationdirectory}

Comments

1

Try something like this

$Include = @('1F-16CG-2', '1F-16CG-2')
gci -Path D:\MessageQueue_Backup\DPGIN_Backup\ETIMS | ForEach-Object ($_) {
    for($i = 0; $i -lt $Include.Count; $i++){
        if($_.Name.StartsWith($Include[$i])){
            Write-Warning $_.Name
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.