Multiple exclude rule

Ask your Windows 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.
Locked
User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Multiple exclude rule

Post by sshree43 » Wed Jun 27, 2018 11:58 am

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
Site Admin
Posts: 260
Joined: Sun Mar 03, 2013 12:42 pm

Re: Multiple exclude rule

Post by mxtrinidad » Wed Jun 27, 2018 12:53 pm

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
Joined: Wed Jun 27, 2018 11:57 am

Re: Multiple exclude rule

Post by sshree43 » Thu Jun 28, 2018 7:30 am

but how to get last modified date based on the file_name and year

User avatar
mxtrinidad
Site Admin
Posts: 260
Joined: Sun Mar 03, 2013 12:42 pm

Re: Multiple exclude rule

Post by mxtrinidad » Thu Jun 28, 2018 1:22 pm

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
Joined: Wed Jun 27, 2018 11:57 am

Re: Multiple exclude rule

Post by sshree43 » Fri Jun 29, 2018 2:42 am

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]

User avatar
jvierra
Posts: 13297
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Multiple exclude rule

Post by jvierra » Fri Jun 29, 2018 2:59 am

Code: Select all

$files = Get-ChildItem -File C:\Users\rramesnc\Documents\Advisory_Rate
foreach($file in $files){
    $file.Name
}

User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Re: Multiple exclude rule

Post by sshree43 » Fri Jun 29, 2018 6:22 am

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
Joined: Wed Jun 27, 2018 11:57 am

Re: Multiple exclude rule

Post by sshree43 » Sat Jun 30, 2018 3:29 am

Suggestion please

Locked