【发布时间】: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