Page 1 of 1

Multiple exclude rule

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

Re: Multiple exclude rule

Posted: Wed Jun 27, 2018 12:53 pm
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]

}

Re: Multiple exclude rule

Posted: Thu Jun 28, 2018 7:30 am
by sshree43
but how to get last modified date based on the file_name and year

Re: Multiple exclude rule

Posted: Thu Jun 28, 2018 1:22 pm
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")
:
}

Re: Multiple exclude rule

Posted: Fri Jun 29, 2018 2:42 am
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]

Re: Multiple exclude rule

Posted: Fri Jun 29, 2018 2:59 am
by jvierra

Code: Select all

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

Re: Multiple exclude rule

Posted: Fri Jun 29, 2018 6:22 am
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

Re: Multiple exclude rule

Posted: Sat Jun 30, 2018 3:29 am
by sshree43
Suggestion please