1

I'm trying to make a PowerShell script that can copy files to a new destination using a .txt file as a search parameter. The issue I'm having is I don't have the full file extension.

$sourceItemFolder = C:\Art_directory
$targetFolder = O:\Work_Folder
$FileList = C:\VbbList.txt

The list is laid out

191428A1
191435A1
191431A1
191429A1
191430B1
191432A1

They are in a column copied from excel*

The source item Folder has many different folders and inside each folder there is variant of each prefix. The prefix being the first 6 characters of each Reference number 191428 and the variant being A1

Sub-Folder Example-

`C:\Art_directory\191428\191428A1 - Family Where Our Roots Grow Deep 1.625x7.25.jpg`


"191428A1 - Family Where Our Roots Grow Deep 1.625x7.25.jpg"` is the full file name.

The file I'm wanting to copy is always a .jpg

They're about 14k folders in the Art_directory folder so going through this, typing in one reference# at a time kinda sucks.

Some of the older folders have descriptions after the reference #'s Example-

C:\Art_directory\191428 - Family Where Our Roots Grow Deep

Is it possible to use a txt file as a -Filter or is there another way to go about doing this?

3
  • 2
    you haven't described what to do with a file - or what to the text file has to do with anything. please, try some very specific examples. ///// also, PLEASE add formatting around your list and file names. it's quite iffy to read as it now is laid out.
    – Lee_Dailey
    Commented Mar 9, 2021 at 19:31
  • 1
    How many Reference numbers might you have in a file? If it's ten, I would approach the search one way, if 1000, another. For a relatively small set, I would construct a single filer for all subdirectory names, but would loop with a larger data set. Commented Mar 10, 2021 at 18:30
  • 25-100+ I simplified the list I posted. I have 75 in my current list. Commented Mar 10, 2021 at 18:39

2 Answers 2

0

Here's one way:

$sourceItemFolder = 'C:\Art_directory'
$targetFolder     = 'O:\Work_Folder'
$FileList         = 'C:\VbbList.txt'
$extFilter        = '*.jpg'

# $RefNums = Get-Content $FileList

$RefNums = @'
191428A1
191435A1
191431A1
191429A1
191430B1
191432A1 
'@ -Split '\n'  # Sustituting for Get-COntent for testing

ForEach ( $refNum in $RefNums ) {
    $gciSplat = @{ # splatted for readability
        'Path'    = ( Join-Path $sourceItemFolder $refNum.Remove(6) ) + '*' ### or "$sourceItemFolder\$($refNum.Remove(6))*"
        'Filter'  = $refNum + $extFilter
        'Recurse' = $True
        'Depth'   = 1
    }
    ForEach ( $foundFile in ( Get-ChildItem @gciSplat )) {
        Copy-Item -LiteralPath $_.FullName -Destination $targetFolder
    }
}

Pipelined:

$sourceItemFolder = 'C:\Art_directory'
$targetFolder     = 'O:\Work_Folder'
$FileList         = 'C:\VbbList.txt'
$extFilter        = '*.jpg'

Get-Content $FileList | ForEach{
    $gciSplat    = @{ # splatted for readability
        'Path'    = ( Join-Path $sourceItemFolder $_.Remove(6) ) + '*'
        'Filter'  = $_ + $extFilter
        'Recurse' = $True
        'Depth'   = 1
    }
### -WhatIf parameter included for testing. Remove to execute copy

    Get-ChildItem @gciSplat | Copy-Item -LiteralPath { $_.FullName } $targetFolder -WhatIf

### -WhatIf parameter included for testing. Remove to execute copy
}

Just for grins, aliases & hard-coded paths to make a one-liner:

gc 'C:\VbbList.txt' | %{ gci "C:\Art_directory\$($_.Remove(6))*" "$_*.jpg" -Recurse -Depth 1 | copy -Literal { $_.FullName } $targetFolder -WhatIf }

Assumptions made:

  • The folders with names derived from Refernce# are all direct sub-folders of $sourceItemFolder
  • Files being searched for are items within the sub-folder ( i.e. Search of sub-folder is non-reucrsive)

About Splatting

About ForEach (Statement)

ForEach-Object

0
0

As for this.

"Is it possible to use a txt file as a -Filter or is there another way to go about doing this?"

No. You'd use that file list in a loop. Read the file and process each line.

$sourceItemFolder = 'C:\Art_directory'
$targetFolder     = 'O:\Work_Folder'
$FileList         = (Get-Content -Path 'C:\VbbList.txt')

$FileList | 
ForEach {
    "Processing $PSItem"
    # Other code here.
}

As noted by Lee_Daily, you are really not giving enough info, no file content sample, folder info, etc.; thus leaving us to guess/assume. All of which does not lead to a potential helpful response. If all you need is .jpg, then just filter on that for your copy.

# Validate your copy action
(Get-ChildItem -Path 'SomeFilePath' -Filter '*.jpg' -Recurse).FullName | 
Copy-Item -Destination 'SomeDestinationPath' -WhatIf
# remove the -WhatIf for thing to happen
5
  • The files I'm wanting to copy are pictures(.jpg). The reason I'm copying the file to a new location is to do a batch conversion using adobe illustrator. In illustrator I have a source folder('O:\Work_Folder') that is gone through and various actions are done to manipulate a set of files for printing. We dont use .jpg for printing so they are converted to a proprietary format. I want to be able to copy files out of 'C:\Art_directory' to 'O:\Work_Folder' so i can do batch actions for a certain list of files and not all files. Commented Mar 10, 2021 at 15:33
  • The 'C:\Art_directory' folder has sub folders that are labeled for example 191428 and then inside that folder there are .jpg files named with the variants of that file name example 191428A1- "_Description of file_" .jpg Commented Mar 10, 2021 at 15:40
  • The text file is a list of reference numbers that were sent to me for editing but they didn't include the location/link of each file or the full name. I know were the files are kept C:\Art_directory. Instead of using the search box in file explorer for each individual item opening the folder for 1914281 and then copying 191428A1- "_Description of file_" .jpg to 'O:\Work_Folder'. I would like to able to change the content of the text file to what ever reference numbers i need copied hence why I'm using a text list rather then having specific search terms. Commented Mar 10, 2021 at 16:03
  • So the reference numbers in the list will relate to the name of a folder and also a portion of the file name which you want to perform an operation against, correct? @AntonioMcfarren Perhaps using that string from the GC with another Get-ChildItem, etc. with -like "*$_*" filtering perhaps would work. Just a quick idea for postanote but I'd have to dig into the guts of this myself otherwise. Commented Mar 10, 2021 at 16:27
  • Kinda, the reference number's first 6 digits indicate the folder or type of art so the "a1" would get removed. The people that upload art just dump it into a "incoming" folder then I have a script that moves files using the first 6 digits to folders with the same 6 digits and if no folder exists it creates one then moves the file from the incoming folder to the C:\Art_directory\"folder_created". Example of the folder created "C:\Art_directory\191428\" then the 191428A1-"description".jpg is moved into this folder. Commented Mar 10, 2021 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .