Domanda

Currently I am using this line to collect all files in a certain path (and beyond)

$files = Get-ChildItem -Path $loc -Recurse | ? { !$_.PSIsContainer }

however now ive been asking to generate this list while exclude (for example) all "docx" and "xlsx" files... and folders named "scripts" and their content

I want to read in these file extensions and directory names from a txt file into an array and simply use that array.

Also would speed is important as the functions I will be performing on these files take long enough, I dont need this process slowing down my script 10 full (a bit is okay)

Thanks so much for any inputs

FAILED ATTEMPT:

gi -path H:\* -exclude $xfolders | gci -recurse -exclude $xfiles | where-object { -not $_.PSIsContainer }

I think this works but only does the folder exclusion at the root of the H:\ drive

È stato utile?

Soluzione

Something like this? I'm comparing the realative path only(path from $loc) in case $loc includes one of the foldernames to ignore.

$loc = "C:\tools\scripts\myscripts\"
$files = Get-ChildItem -Path $loc -Recurse -Exclude *.docx, *.xlsx | ? { !$_.PSIsContainer -and !($_.FullName.Replace($loc,"") -like "*scripts\*") }

Multiple folders(this got ugly):

#Don't include "\" at the end of $loc - it will stop the script from matching first-level subfolders
$loc = "C:\tools\scripts\myscripts"
$ignore = @("testfolder1","testfolder2");

$files = Get-ChildItem -Path $loc -Recurse -Exclude *.docx, *.xlsx | ? { !$_.PSIsContainer } | % { $relative = $_.FullName.Replace($loc,""); $nomatch = $true; foreach ($folder in $ignore) { if($relative -like "*\$folder\*") { $nomatch = $false } }; if ($nomatch) { $_ } }

Altri suggerimenti

If I understand the question, you're headed down the right path. To exclude *.docx files and *.xlsx files, you'll want to supply them to the -exclude parm as an array of filter strings.

$files = Get-ChildItem -Path $loc -Recurse -Exclude @('*.docx','*.xlsx') | ? { !$_.PSIsContainer }

I was trying to do this as well, and Graimer's answer didn't work (way too complex as well), so I've come up with the following.

$ignore = @("*testfolder1*","*testfolder2*");
$directories = gci $loc -Exclude $ignore | ? { $_.PSIsContainer } | sort CreationTime -desc

In version 3 of PowerShell, we can tell Get-ChildItem to show only files as follows:

PS> $files = Get-ChildItem -File -Recurse -Path $loc 

If you only want certain filenames (e.g. abc*.txt) to be collected, you can also use a filter:

PS> $files = Get-ChildItem -File -Recurse -Path $loc -Filter abc*.txt
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top