質問

We have a script running daily that removes old files and directories from an area that people use to transfer data around. Everything works great except for one little section. I want to delete a folder if it's older than 7 days and it's empty. The script always shows 1 file in the folder because of the thumbs.db file. I guess I could check to see if the one file is thumb.db and if so just delete the folder but I'm sure there is a better way.

 
$location = Get-ChildItem \\dropzone -exclude thumbs.db
foreach ($item in $location) {

  other stuff here going deeper into the tree...

  if(($item.GetFiles().Count -eq 0) -and ($item.GetDirectories().Count -eq 0)) {

    This is where I delete the folder but because the folder always has
     the Thumbs.db system file we never get here

  } 

}


役に立ちましたか?

解決

$NumberOfFiles = (gci -Force $dir | ?{$_ -notmatch "thumbs.db"}).count

他のヒント

You can try the get-childitem -exclude option where all files/items in your directory will be counted except those that end in db:

$location = get-childitem -exclude *.db

It also works out if you specify the file to exclude, in this case thumbs.db

$location = get-childitem -exclude thumb.db

Let me know if this works out.


Ah, I also just noticed something,

$location = get-childitem -exclude *.db

Will only handle .db items in the location directory, if you're going deeper into the tree (say from your GetFiles() and GetDirectories() methods) then you may still find a thumb.db. Hence you'll have to add the exclude option in these methods to ignore thumbs.db.

So, for example in your $item.getFiles() method, if you use get-childitem you will have to specify the -exclude option as well.

Sorry, I should have read your question more closely.

Use this method to provide a exclusion list in the form of a simple text file to exclude specific files or extensions from your count:

$dir = 'C:\YourDirectory'
#Type one filename.ext or *.ext per line in this txt file
$exclude = Get-Content "C:\Somefolder\exclude.txt"
$count = (dir $dir -Exclude $exclude).count
$count
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top