1

I have a PowerShell script I am trying to write. Basically I am trying to find folders that have a specific name within a shared volume, there are multiple instances of these folders at various DIR levels all throughout the share all with the same name. I need too list both the folder path and the contents and output to CSV. I have been able list the folder paths with the following script:

gci -Filter "foldername" -Recurse -Path "X:\DIR" -Directory | select-object FullName | Export-Csv C:\Output\folderlist.csv

That's all well and good, but now I would like to list the file contents of each folder named "Foldername". This is where it get's tricky, some of the folders have contents, some are empty. I always need to output the folder path, even if it's empty, and also list the folder contents, if it has contents.

The following script will list all folders named "foldername", but it only outputs the folders that have contents, if the folder is empty, then the path listing does not get included.

gci -Path X:\DIR -Filter *foldername* | where { $_.psiscontainer } | gci | Export-Csv C:\Output\folderlist.csv

I am also running into a 260 character limit on PowerShell now. It's fine when only listing the folders, but then when it get's to the file level, it bombs out on some folders because either the folder path, or file name are then too long.

I read an article about someone using robocopy through PowerShell to get around the 260/248 char limit.

Any suggestions would be fantastic.

1 Answer 1

1
$Search = gci -Filter "Test" -Recurse -Path "d:" -Directory 
Foreach ($path in $Search.fullname) {
Write-output $path | Out-File d:\Filelist.txt -append
$file = gci -path $path | select name 
$file.name | out-file d:\filelist.txt -append
write-output "------- Next Folder --------------" | Out-File d:\Filelist.txt -append }

For Outputting Files >260, you can use the get-folderitem.ps1 Script: http://gallery.technet.microsoft.com/Get-Deeply-Nested-Files-a2148fd7

$Search = gci -Filter "Test" -Recurse -Path "d:\Testfolder" -Directory 
Foreach ($path in $Search.fullname) {
Write-output $path | Out-File d:\Filelist.txt -append
$file = get-folderitem -Path $path | where {$_.fullname.length -gt 260} 
$file.name | out-file d:\filelist.txt -append
write-output "------- Next Folder --------------" | Out-File d:\Filelist.txt -append }
Sign up to request clarification or add additional context in comments.

1 Comment

Daniel, Your first solution is almost what I was also looking for with the only exception that I want to get a list of folders through Filelist.txt that are empty and not the ones that have files inside them. Please assist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.