question

GeraldoPeralta-0803 avatar image
0 Votes"
GeraldoPeralta-0803 asked AndreasBaumgarten edited

List Child items inside a list of directory names

I have a directory structure like: \\NetworkPath\FixedDir1\FixedDir2\Client. Inside Client directory I have lots of directories and each of these directories have one text file inside them.

\\NetworkPath\FixedDir1\FixedDir2\Client\

qwertyuiop --> dir
apple.txt
asdfghjklzx --> dir
gold.txt
mnbvcxzlkj --> dir
cat.txt


I want to build a powershell script that take the directory names in a text file list, look for a list of directory names and save a two columns output. One column with the name of the directory and the other column with the full name of the file inside that directory. E.g.

My input to the powershell script: a file.txt with a list of directory names (mentioned above):

qwertyuiop
asdfghjklzx
mnbvcxzlkj
....
could be thousands of directory names

The output within a Excel File:

DirectoryName FileName
qwertyuiop apple.txt
asdfghjklzx gold.txt
mnbvcxzlkj cat.txt

Thanks in advanced.

Regards,

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Are those directory names all under the same root directory? Or are they sometimes child directories of other directories? Are the directories all on the same storage device (e.g. C:) or may they be on different storage devices? Can the directory names appear in more than one parent directory? Can the directory names appear multiple times (as child directories of other child directories) beneath the same root directory?

Does the output have to contain the complete path (e.g. <device><root><child>) or just the name of the child directory?

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @GeraldoPeralta-0803 ,

maybe this helps:

 $dirPath = "C:\Junk\Client"
 $csvFile = "result.csv"
 $delimiter = ","
 "DirectoryName" + $delimiter + "FileName" | Out-File -FilePath $csvFile -Encoding utf8
 Get-ChildItem -Path $dirPath -Directory |
 ForEach-Object {
     $_.Name + $delimiter + (Get-ChildItem -Path $_.Fullname | Select-Object -ExpandProperty Name) |
     Out-File -FilePath $csvFile -Append -Encoding utf8
 }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok. Thanks for the answer.

The code is good.

How can I input a text file with a list of directory names. It would loop looking for the directory and then for the file inside? Because right now, I just can input a directory name in $dirPath variable.

Again, thanks.

Regards,

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered GeraldoPeralta-0803 commented

Hi @GeraldoPeralta-0803 ,

please give this a try:

folders.txt

  C:\Junk\Client1
  C:\Junk\Client2

script

 $csvFile = "result.csv"
 $delimiter = ","
 $folders = Get-Content -Path "C:\Junk\folders.txt"
 "DirectoryName" + $delimiter + "FileName" | Out-File -FilePath $csvFile -Encoding utf8
 foreach ($dir in $folders) {
     Get-ChildItem -Path $dir -Directory |
     ForEach-Object {
         $_.Name + $delimiter + (Get-ChildItem -Path $_.Fullname | Select-Object -ExpandProperty Name) |
         Out-File -FilePath $csvFile -Append -Encoding utf8
     }
 }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried this:

  1. I put two folder names with its absolute path in C:\results\folders.txt", like:
    c:\qwertyuiop
    c:\asdfghjklzx

$csvFile = "C:\results\result.csv"
$delimiter = ","
$folders = Get-Content -Path "C:\results\folders.txt"
"DirectoryName" + $delimiter + "FileName" | Out-File -FilePath $csvFile -Encoding utf8
foreach ($dir in $folders) {
Get-ChildItem -Path $dir -Directory |
ForEach-Object {
$.Name + $delimiter + (Get-ChildItem -Path $.Fullname | Select-Object -ExpandProperty Name) |
Out-File -FilePath $csvFile -Append -Encoding utf8
}
}

The output csv has no content.

Regards,

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @GeraldoPeralta-0803 ,

my last script with reading the text file takes the folders of the txt file and process every subfolder to get the files.

First take C:\Junk\Client1 and get all subfolders and get file in subfolders
Next take C:\Junk\Client1 and get all subfolders and get file in subfolders
. ..... and so on
I thought that's your requirement.

If you want to get the files of the folders in the txt file please try this:

folders1.txt

 C:\Junk\Client1\asdfghjklzx
 C:\Junk\Client1\mnbvcxzlkj
 C:\Junk\Client1\qwertyuiop

script:

 $csvFile = "result1.csv"
 $delimiter = ","
 $folders = Get-Content -Path "C:\Junk\folders1.txt"
 "DirectoryName" + $delimiter + "FileName" | Out-File -FilePath $csvFile -Encoding utf8
 foreach ($dir in $folders) {
        $dir + $delimiter + (Get-ChildItem -Path $dir | Select-Object -ExpandProperty Name) |
        Out-File -FilePath $csvFile -Append -Encoding utf8
 }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.