multiple exclude rules in powershell

筅森魡賤 提交于 2019-12-14 03:31:33

问题


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:\Test\Country'
$destdir = 'C:\Test\Country\Archive'

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

thanks


回答1:


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.



来源:https://stackoverflow.com/questions/51064464/multiple-exclude-rules-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!