简体   繁体   中英

List files details from folders and subfolders are duplicading

I'm trying to get the names of the files within the directory and the sub directories within the "parent" directory.

I managed to solve my problem in parts, but in the last directory it is multiplying the files (as if I was checking this folder twice). If I have 2 items, he is exporting me 4 to that last directory.

I managed to get the files from the directories, but if I have any files in the root directory (parent directory), it won't list.

Parent dir "\\XML"

  • File1.xlsx
  • File2.txt
  • Folder2

Dir "Folder2"

  • File1.xml
  • File2.xml

So, in the CSV file exported will be 4 rows, which is duplicate files from "Folder2" folder. And it won't bring me the files in the "XML" folder, which is the parent folder.

Powershell script:

# To execute the script without agreeing with the execution policy
Set-ExecutionPolicy Bypass -Scope Process

# Defines the parent directory, where all files and folders inside it will be
$DirPai = 'D:\Users\F02579\Desktop\XML'

# Variable to store all directories within the parent directory
$DirPastas = (Get-ChildItem -Directory -Recurse $DirPai).FullName

# Variable that will keep the final result
$results = @()

foreach ($Dir in $DirPastas)
{
    # Write the directory name
    Write-Host $Dir
    # Get the file details
    $Arquivos = Get-ChildItem -Path $Dir -Recurse | Select-Object Directory, Name, Lenght, LastWriteTime, @{Name="Extension";Expression={$_.Extension}} #| Where-Object "Extension"-ne ''
    # Store the result for each path
    $results += $Arquivos
}

# Defines the directory to which the final file will be exported
$DiretorioExportacao = 'D:\Users\F02579\Desktop\XML\I_PI_LISTFILES.csv'

# Export the result to CSV in the previously informed directory
$results | Export-Csv -Path $DiretorioExportacao -NoTypeInformation -Encoding UTF8

You could reduce your code to the following.

  1. Keep only the first Get_ChildItem. Remove the -Dir switch (ie. get files and folders)
  2. Pipe directly to Select-Object. Don't bother with an explicit loop.

This will give a result with five items: four files and one folder.

You also had a typo with 'length'.

Cheers.

# To execute the script without agreeing with the execution policy
Set-ExecutionPolicy Bypass -Scope Process

# Defines the parent directory, where all files and folders inside it will be
$DirPai = 'D:\Users\F02579\Desktop\XML'

$results = (Get-ChildItem -Recurse $DirPai) | Select-Object Directory, Name, Length, LastWriteTime, @{Name="Extension";Expression={$_.Extension}} #| Where-Object "Extension"-ne ''

# Defines the directory to which the final file will be exported
$DiretorioExportacao = 'D:\Users\F02579\Desktop\XML\I_PI_LISTFILES.csv'

# Export the result to CSV in the previously informed directory
$results | Export-Csv -Path $DiretorioExportacao -NoTypeInformation -Encoding UTF8

You already have all the directories captured so in your loop you need to specify the -File parameter and there is no need for the -Recurse parmaeter.

   $Arquivos = Get-ChildItem -Path $Dir -File | 
            Select-Object @{Name="Directory",Expression($Dir),
                    Name, Lenght, LastWriteTime, 
                    @{Name="Extension";Expression={$_.Extension}} 

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM