How do I prevent Get-ChildItem from traversing a particular directory?
Asked Answered
O

2

8

Let me start by saying that I've looked at Unable to exclude directory using Get-ChildItem -Exclude parameter in Powershell and How can I exclude multiple folders using Get-ChildItem -exclude?. Neither of these has an answer that solves my problem.

I need to search a directory recursively for files with a certain extension. For simplicity, let's just say I need to find *.txt. Normally, this command would suffice:

Get-ChildItem -Path 'C:\mysearchdir\' -Filter '*.txt' -Recurse

But I have a major problem. There's a node_modules directory buried somewhere inside C:\mysearchdir\, and NPM creates extremely deep nested directories. (The detail of it being an NPM managed directory is only important because this means the depth is beyond my control.) This results in the following error:

Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

I believe this error bubbles up from the limitations in the .NET IO libraries.

I can't search in the other directories around it very easily. It's not at the top of the directory; it's deeper in, say at C:\mysearchdir\dir1\dir2\dir3\node_modules, and there are directories I need to search at all those levels. So just searching the other directories around it is going to be cumbersome and not very maintainable as more files and directories are added.

I've tried to -Exclude parameter without any success. That isn't surprising since I just read that -Exclude is only applied after the results are fetched. I can't find any real info on using -Filter (as is noted in this answer).

Is there any way I can get Get-ChildItem to work, or am I stuck writing my own recursive traversal?

Orford answered 11/3, 2015 at 2:38 Comment(4)
May this will help, powershell-file-path-too-long, it uses a third party library instead of System.IOMadalene
You could implement this recursive pattern in PowerShell and put in a condition to ignore the directory if the name is node_modulesLather
Would the downvoter care to offer some suggestions for improvement?Orford
I can't imagine why it was downvoted, I was just about to post this exact question!Outvote
O
6

Oh, man, I feel dumb. I was facing the same problem as you. I was working with @DarkLite1's answer, trying to parse it, when I got to the "-EA SilentlyContinue" part.

FACEPALM

FACEPALM!

That's all you need!

This worked for me, try it out:

Get-ChildItem -Path 'C:\mysearchdir\' -Filter '*.txt' -Recurse -ErrorAction SilentlyContinue

Note: This will not exclude node_modules from a search, just hide any errors generated by traversing the long paths. If you need to exclude it entirely, you're going to need a more complicated solution.

Outvote answered 3/8, 2015 at 16:4 Comment(1)
+1 There are a lot of threads out there trying to solve this question with all sorts of "Map drive letters to folder paths" type solutions when a simple SilentlyContinue solves it. Facepalm indeed.Maineetloire
L
0

Maybe you could try something like this:

$Source = 'S:\Prod'
$Exclude = @('S:\Prod\Dir 1', 'S:\Prod\Dir 2')

Get-ChildItem -LiteralPath $Source -Directory -Recurse -PipelineVariable Dir -EV e -EA SilentlyContinue | 
    Where {($Exclude | Where {($Dir.FullName -eq "$_") -or ($Dir.FullName -like "$_\*")}).count -eq 0}
Lesbianism answered 11/3, 2015 at 9:30 Comment(1)
That's a pretty complicated command. Could you describe what it's doing?Orford

© 2022 - 2024 — McMap. All rights reserved.