Forum Discussion
TampaCCT
Nov 02, 2020Copper Contributor
Find all files inside directory Trees with a given name. Powershell question
I need help understanding the cmdlet behavior.
Simple command Get-ChildItem
I want to find all the files in directories with a given name.
Example Data:
c:\temp\AD\File1.txt
c:\temp\AD\File2.txt
c:\temp\AD\File3.txt
c:\temp\AD\FileX.txt
c:\temp2\AD\File1.txt
c:\temp2\AD\File2.txt
Set-location c:\
Get-ChildItem -Path "*\AD\*" -Include *.txt -Recurse
This command will run correctly and show file1 through FileX in any part of the directory tree.
Change the Set-location to C:\temp and no files displayed. Even though the AD directory exists in the tree.
The problem is when I'm looking for files in a directory on the root of the Get-ChildItem -Path "*\$myDriectoryName\*" -Include *.txt -Recurse will not display anything.
I can use the where-object to filter but I'm curious about the behavior. Can I write the path in a different way?
This is the updated code
Set-location c:\ Get-ChildItem -Include *.txt -Recurse | where {$_.FullName -like "*\AD\*"}
you don't need to use the path if you already use set-location.
the update here is adding the where filter to filter all the stream from the Get-ChildItem and check if in the full path of the result include a folder named AD.
as you are scanning the entire root this should give you the result you are looking for.
5 Replies
Sort By
- farismalaebSteel Contributor
Id refar to use Join-path
check the example below
$Path1="C:\" $path2="Folder1" $Fullpath=Join-Path $Path1 $path2 Get-ChildItem -Path $Fullpath -Include *.txt -Recurse
- TampaCCTCopper Contributor
farismalaeb Thank you for the reply but I'm looking for the reason why.
I've updated the original post to include more details. The exercise is to find all the files in a directory tree or on a drive with a given directory name. The directory can be located in multiple places on the drive or in the tree. I used AD for the example but it could be "Users", "Temp" or "Deleted Items.IMAP" etc.
My ideal code would not have me search the drive for all the instances of the requested Directory name and then process that object looking for the files within. I'm new to Powershell programming and how cmdlets behave. The path parm acts differently depending on some variables I do not understand. I've tested the above code and have gotten different results depending on where the current path is set. Some times no results. Some times the first instance of the Directory, Some times everything that is requested. What is affecting the behavior?
I see the -path parm is not a reliable method of finding a specific directory name if there are multiple instances of the name in different places in the tree.
- farismalaebSteel Contributor
This is the updated code
Set-location c:\ Get-ChildItem -Include *.txt -Recurse | where {$_.FullName -like "*\AD\*"}
you don't need to use the path if you already use set-location.
the update here is adding the where filter to filter all the stream from the Get-ChildItem and check if in the full path of the result include a folder named AD.
as you are scanning the entire root this should give you the result you are looking for.