Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

multiple exclude rules in powershell

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

funny thing is that i have only single file excluder statement and do not know the multiple file excluder rule based on file name, Any example appericiated

I have only single file exclude statement

$sourcedir = 'C:TestCountry'
$destdir = 'C:TestCountryArchive'

Get-ChildItem -File -Path $sourcedir |
    Sort-Object -Property LastWriteTime -Descending |
    Select-Object -Skip 1 |
    Move-Item -Destination $destdir -force

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I post this as an answer as I don't have the characters to do it as comment. Let me see if I understand this.

$Files = Get-ChildItem -File C:Setup | select Name, LastWriteTime

You then have an export of the files like:

Name                           LastWriteTime       
----                           -------------       
SS_MM_Master_Finland_2017.txt  6/27/2018 4:30:09 PM
SS_MM_Master_Finland_2018.txt  6/27/2018 4:30:09 PM
SS_MM_Master_Germany_2017.txt  6/27/2018 4:30:09 PM
SS_MM_Master_Germany_2018y.txt 6/27/2018 4:30:09 PM
SS_MM_Master_Italy_2017.txt    6/27/2018 4:30:09 PM
SS_MM_Master_Italy_2018.txt    6/27/2018 4:30:09 PM

And then you can go with an foreach with if like:

foreach ($File in $Files) {

If ($File.Name -like "*Italy*" -and $File.Name -like "*2017*") {
Write-Host $File.Name
}
Else{
Write-Host "This is not the file you are looking for" $file.Name
}
}

I believe you can understand the concept behind this code. You can replace the Italy with a variable that you can do with Read-Host that goes for all your conditions on the if statement and then if those are true move the file to the other folder.

Hope this answer will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...