Multiple exclude rule

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Multiple exclude rule

Post by sshree43 »

I have a requirement to exclude files based on year, country name and last modified date from that particular year and rest files from that particular year and the country moved to an archive folder

for an example

SS_MM_Master_finland_2018.xlsx last modified date 27/06/2018 19:00.

SS_MM_Master_finland_2017.xlsx last modified date 27/06/2017 19:00.
in this case, same country and year is different in the file name so that particular year- last modified date would be excluded so both the files will be excluded

wants to know if someone can give a small example based on their experience...not necessary to be from above example or any multiple exclude rule or anything contribution would be appreciated

I have tried

$Files = Get-ChildItem -File C:\Setup | select Name, LastWriteTime
You then have an export of the files like:

Name ..............................................LastWriteTime
---- -------------

SS_MM_Master_Germany_2017.txt ..........6/27/2017 4:30:09 PM

SS_MM_Master_Germany_2017.txt ..........6/26/2017 4:30:09 PM

SS_MM_Master_Germany_2018.txt ...........6/27/2018 4:30:09 PM

SS_MM_Master_Germany_2018.txt ...........6/27/2018 4:30:09 PM

till SS_MM_Master format is same and countryname may change based on users...expecting any dynamic query which consider country name as different string check max(modified date) based on year...any small example help appreciated
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Multiple exclude rule

Post by mxtrinidad »

Take a look at the *.Split() method.

Use the filename with the split('_') to brake out the filename as elements to get: Country, and Year.
You could be very creative! Just explore the .NET objects, then add the logic.

$Files = Get-ChildItem -File C:\Setup | select Name, LastWriteTime
foreach($FileName in $Files)
{

## - Split elements: 0 = SS, 1 = MM, 2 = Master, 3 = Country, 4 = Year

## - Get FileName:
$MyFile = $FileName

## - Get Country:
$fileCountry = $FileName.Name[0].Split('_')[3]

## - Get Year
$fileYear = $FileName.Name[0].Split('_')[4]

}
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: Multiple exclude rule

Post by sshree43 »

but how to get last modified date based on the file_name and year
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Multiple exclude rule

Post by mxtrinidad »

As long the .NET object has the properties you need, you can pick any direction you like.

$Files = Get-ChildItem -File C:\Setup
$Files | Get-Member
:
LastWriteTime Property datetime LastWriteTime {get;set;}
:

There's no last modified date property in the $Files object. You will need to use the ".LastWriteTime" and for the Filename use the ".Name".
$Files[0].LastWriteTime.ToString("yyyy") - Simple test to check one element in the object collection containing all files results.

Or, Just use the foreach() to list them all:

foreach($FileName in $Files)
{
## - Sample getting only the filename:
$FileName.Name

## - Sample getting the year value from the ".LastWriteTime":
$FileName.LastWriteTime.ToString("yyyy")
:
}
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: Multiple exclude rule

Post by sshree43 »

I am getting error on running this file

$FileName=C:\Users\rramesnc\Documents\Advisory_Rate\*.*;
$Files = Get-ChildItem -File C:\Users\rramesnc\Documents\Advisory_Rate | select Name, LastWriteTime
foreach($FileName in $Files)
{

#- Split elements: 0 = SS, 1 = MM, 2 = Master, 3 = Country, 4 = Year
# - Get FileName:
$MyFile = $FileName

# - Get Country:
$fileCountry = $FileName.Name[0].Split("_")[3]

# - Get Year
$fileYear = $FileName.Name[0].Split("_")[4]

}

getting error:
Method invocation failed because [System.Char] does not contain a method named 'Split'.
At C:\Users\garang\Documents\input_files\Script\year.ps1:12 char:1
+ $fileCountry = $FileName.Name[0].Split("_")[3]
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple exclude rule

Post by jvierra »

Code: Select all

$files = Get-ChildItem -File C:\Users\rramesnc\Documents\Advisory_Rate
foreach($file in $files){
    $file.Name
}
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: Multiple exclude rule

Post by sshree43 »

Previous error fixed....


I tried below-mentioned query today and getting all the values year , country but how to get max(last modified date for that particular year and country

$Files = Get-ChildItem -File C:\Users\rramesnc\Documents\Advisory_Rate | select Name, LastWriteTime
foreach($FileName in $Files)
{

####- Split elements: 0 = SS, 1 = MM, 2 = Master, 3 = Country, 4 = Year
#### - Get FileName:
$MyFile = $FileName

#### - Get Country:

$FileName -split "-"

#### - Get Year

$fileYear = $FileName.Name.Split("_")[4,3]

$fileCountry.Name

#### - Sample getting the year value from the ".LastWriteTime":

$FileName

$fileYear

$FileName.LastWriteTime
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: Multiple exclude rule

Post by sshree43 »

Suggestion please
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked