Get folder name and filename with PowerShell

Geraldo Peralta 86 Reputation points
2021-08-31T20:45:02.823+00:00

Hello, community.

I need your help to write a PowerShell code to meet the following requirement:

I have several directories with one filename inside of each one:

\NetworkPath\FixedDir1\FixedDir2\Client\AAAAA-BBBBB-CCCCC --> inside this directory there is a filename: filename.txt
\NetworkPath\FixedDir1\FixedDir2\Client\ZZZZZ--BBBBB-CCCCC --> inside this directory there is a filename: fielename2.txt

There is also a text file (DirectoriesList.txt) with a list of similar directories like above:

\NetworkPath\FixedDir1\FixedDir2\Client\AAAAA-BBBBB-CCCCC
\NetworkPath\FixedDir1\FixedDir2\Client\ZZZZZ--BBBBB-CCCCC
...
Could be millions

The PS Code should accept a text file (DirectoriesList.txt) as input.
The PS Code should produce a two columns csv as output. One column with the name of the final directory (AAAAA-BBBBB-CCCCC) and one column with the filename inside this directory.

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,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 104K Reputation points MVP
    2021-08-31T21:51:40.587+00:00

    Hi @Geraldo Peralta ,

    maybe this helps:

    $directoryListfile = ".\Junk\DirectoriesList.txt"  
    $csvFile = ".\Junk\result.csv"  
    Out-File -FilePath $csvFile -InputObject "Folder,FileName"  
    $line = ""  
    Get-Content -Path $directoryListfile | ForEach-Object {  
        $file = Get-ChildItem -Path $_ -File  
        $line += (($file.DirectoryName).Split("\")[-1]) + "," + ($file.Name) + "`r`n"  
    }  
    Out-File -FilePath $csvFile -InputObject $line -Append  
    

    ----------

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

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Dave Woolsey 256 Reputation points
    2021-08-31T22:04:42.813+00:00

    Here is a pretty simple script that should do the trick... Keep in mind that it will append the data if there is already an existing CSV. If you only plan on running the script once, that shouldn't be a problem. If you plan to run this on a schedule, I would add some logic in to create a uniquely named CSV each time it is run.

    $DirectoriesFile = 'C:\DirectoriesList.txt' ##Replace with the path to your file with directories
    $CSV = 'C:\Data.csv' ##Replace with the path to where the CSV should be output
    
    
    Add-Content -Path $CSV  -Value 'Directory,File name'
    
    foreach($line in Get-Content -Path $DirectoriesFile) {
    
        $sourceFile=gci $line | sort LastWriteTime | select -Last 1
        $fileName = Split-Path $sourceFile -leaf
        $directory = $line.Split("/")[-1]
    
        Add-Content -Path  $CSV -Value $directory','$fileName 
    }
    

    Your post implies that only one file will exist in each directory. I added a filter to ensure that only one is added to the CSV within each directory (it uses the last write time). If you are expecting multiple files within the directories you are searching, this code below will work better.

    $DirectoriesFile = 'C:\DirectoriesList.txt' ##Replace with the path to your file with directories
    $CSV = 'C:\Data.csv' ##Replace with the path to where the CSV should be output
    
    
    Add-Content -Path $CSV  -Value 'Directory,File name'
    
    foreach($line in Get-Content -Path $DirectoriesFile) {
    
    
        $directory = $line.Split("\")[-1]
    
        foreach($fileName in gci $line){
    
        Add-Content -Path $CSV -Value $directory','$fileName
        }
    }
    
    1 person found this answer helpful.

  2. Limitless Technology 39,511 Reputation points
    2021-09-01T13:01:42.397+00:00

    Hi there,

    This code might help your code to accept the .tx file

    $inputFile = "DirectoriesList.txt"
    $outputFile = "C:\temp\output.txt"

    $results = Get-Content $inputFile | ForEach-Object{
    $props = @{
    Folder = $_
    Sum = "{0:N2}" -f ((Get-ChildItem $_ -Recurse -Force | Measure-Object -property length -sum | Select-Object -ExpandProperty Sum) / 1MB)
    }

    New-Object -TypeName PSCustomObject -Property $props
    

    }

    $results | Export-CSV $outputFile -NoTypeInformation
    "Total,{0}" -f ($results | Measure-Object Sum -sum | Select-Object -ExpandProperty Sum) | Add-Content $outputFile

    Hope this Answers all your queries , if not please do repost back .
    If an Answer is helpful, please click "Accept Answer" and upvote it : )