Hituptony
Hituptony

Reputation: 2860

Folder Structure Name Match Powershell

I have a folder structure in a directory called FTP.

\\FTP\December 15
\\FTP\January 15
\\FTP\February 15
\\FTP\March 15

etc...

I will be moving files to this folder using a powershell script: multiple files. potentially multiple folders.

I want to extract the month

$month = get-date -format m

This will return December

Now how do I write the GCI statement to match the folder to the month

For example: Something like this?

gci '\\FTP\' -recurse WHERE $_.Fullname -like $a.substring(0,9)

Please help

Upvotes: 0

Views: 154

Answers (2)

CalebB
CalebB

Reputation: 607

Here is a function to get the month specific folder then you can do whatever you need by replacing the "Do Work To Said Folder" Comment with whatever you want to do to the folder.(Copy To, Get-ChildItems, etc.)

function Get-MonthlyFolder
{
$month = get-date -format m;
#Bellow Line Gets The Full Path To Current Monthly Folder Stored In $folderInst variable:
$folderInst = gci "\\FTP" | Where {$.Name -like "$month*"} | Select FullName;
#Do Work To Said Folder
}
Get-MonthlyFolder;

Upvotes: 1

Micky Balladelli
Micky Balladelli

Reputation: 9991

$month = get-date -format m returns December 15 I assume that's correct.

Then your code will look like this:

Get-ChildItem \\FTP\ -recurse | where { $_.Fullname -like "*$month*"} 

we use Where-Object which will return each item matching the expression.

Upvotes: 0

Related Questions