List Child items inside a list of directory names

Geraldo Peralta 86 Reputation points
2021-07-09T15:52:00.057+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,343 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Andreas Baumgarten 94,711 Reputation points MVP
    2021-07-09T17:35:23.36+00:00

    Hi @Geraldo Peralta ,

    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


  2. Andreas Baumgarten 94,711 Reputation points MVP
    2021-07-09T19:08:34.537+00:00

    Hi @Geraldo Peralta ,

    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


  3. Andreas Baumgarten 94,711 Reputation points MVP
    2021-07-09T20:38:11.823+00:00

    Hi @Geraldo Peralta ,

    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

    0 comments No comments