【问题标题】:List files details from folders and subfolders are duplicading文件夹和子文件夹中的列表文件详细信息重复
【发布时间】:2020-09-17 21:55:38
【问题描述】:

我正在尝试获取目录中的文件名以及“父”目录中的子目录。

我设法部分解决了我的问题,但在最后一个目录中,它正在增加文件(好像我检查了这个文件夹两次)。 如果我有 2 个项目,他会将我 4 个导出到最后一个目录。

我设法从目录中获取了文件,但是如果我在根目录(父目录)中有任何文件,它将不会列出。

父目录“\XML”

  • 文件1.xlsx
  • 文件2.txt
  • 文件夹2

目录“Folder2”

  • 文件1.xml
  • 文件2.xml

因此,在导出的 CSV 文件中将有 4 行,这是“Folder2”文件夹中的重复文件。而且它不会给我带来“XML”文件夹中的文件,这是父文件夹。

Powershell 脚本:

# 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

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以将代码简化为以下内容。

    1. 仅保留第一个 Get_ChildItem。移除 -Dir 开关(即获取文件和文件夹)
    2. 直接通过管道连接到 Select-Object。不要为显式循环而烦恼。

    这将给出一个包含五个项目的结果:四个文件和一个文件夹。

    您的“长度”也有错字。

    干杯。

    # 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
    

    【讨论】:

    • 谢谢,成功了!我更快,甚至更干净。为什么类型“长度”之前没有附带数据?出了什么问题?
    • 在您的代码中,您有“长度”。正确的拼写是“长度”。 ?
    【解决方案2】:

    您已经捕获了所有目录,因此在循环中您需要指定 -File 参数,并且不需要 -Recurse 参数。

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

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-22
      • 2014-10-05
      • 2015-09-25
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多