简体   繁体   English

使用 PowerShell 时有没有办法返回子文件夹中的最后一个文件

[英]is there a way to return last file in the subfolders when using PowerShell

I have PowerShell script and i want to return multiple folders based on their positional using a wile card ex.我有 PowerShell 脚本,我想使用一个诡计卡 ex 根据它们的位置返回多个文件夹。 sqlfile*\table i get-childitems -recurse. sqlfile*\table 我得到-childitems -recurse。 Thank you谢谢

"I want to return the newest and last file which is in the end of the last subfolder." “我想返回最后一个子文件夹末尾的最新和最后一个文件。”

Is this what you are going for based on your comment above?根据您上面的评论,这是您想要的吗?

Get-ChildItem -Recurse -Path D:\Temp -Directory

# Results
<#
    Directory: D:\Temp


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
d-----        06-May-20     20:30                AddressFiles                                                                                                       
d-----        17-Feb-20     15:50                est                                                                                                                
d-----        14-Mar-20     17:03                here                                                                                                               
d-----        26-May-20     10:38                hold                                                                                                               
d-----        06-May-20     20:30                LogFiles                                                                                                           
d-----        06-Feb-20     14:56                NewFolder                                                                                                          
d-----        12-Feb-20     14:24                ParentFolder                                                                                                       
d-----        03-Feb-20     11:55                Reference                                                                                                          
d-----        06-Feb-20     14:56                Source                                                                                                             
d-----        24-Feb-20     22:03                Target                                                                                                             
d-----        22-May-20     20:51                Zips                                                                                                               


    Directory: D:\Temp\NewFolder


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
d-----        03-Feb-20     11:55                New folder                                                                                                         
d-----        20-Jan-20     11:17                temp                                                                                                               


    Directory: D:\Temp\ParentFolder


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
d-----        12-Feb-20     14:08                ChildFolder                                                                                                        


    Directory: D:\Temp\ParentFolder\ChildFolder


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
d-----        12-Feb-20     14:08                GrandchildFolder  
#>

Get-ChildItem -Recurse -Path D:\Temp -Directory | 
Select-Object -Last 1

# Results
<#
    Directory: D:\Temp\ParentFolder\ChildFolder


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
d-----        12-Feb-20     14:08                GrandchildFolder 
#>

(Get-ChildItem -Recurse -Path D:\Temp -Directory | 
Select-Object -Last 1).Fullname

# Results

<#
D:\Temp\ParentFolder\ChildFolder\GrandchildFolder
#>

$LastSubFolder = (Get-ChildItem -Recurse -Path D:\Temp -Directory | 
Select-Object -Last 1).Fullname  

Get-ChildItem -Path $LastSubFolder 

# Results
<#
    Directory: D:\Temp\ParentFolder\ChildFolder\GrandchildFolder


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
-a----        28-Aug-07     13:08        2646058 GrandchildThumperLong.wav                                                                                          
-a----        28-Aug-07     13:05         683670 GrandchildThumperShort.wav                                                                                         
-a----        23-Apr-99     18:22         668440 GrandchildWELCOM98.WAV 
#>

Get-ChildItem -Path $LastSubFolder | Sort-Object -Property LastWriteTime

# Results
<#
    Directory: D:\Temp\ParentFolder\ChildFolder\GrandchildFolder


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
-a----        23-Apr-99     18:22         668440 GrandchildWELCOM98.WAV                                                                                             
-a----        28-Aug-07     13:05         683670 GrandchildThumperShort.wav                                                                                         
-a----        28-Aug-07     13:08        2646058 GrandchildThumperLong.wav  
#>

Get-ChildItem -Path $LastSubFolder | Sort-Object -Property LastWriteTime | 
Select-Object -First 1 -Last 1

# Results
<#

    Directory: D:\Temp\ParentFolder\ChildFolder\GrandchildFolder


Mode                LastWriteTime         Length Name                                                                                                               
----                -------------         ------ ----                                                                                                               
-a----        23-Apr-99     18:22         668440 GrandchildWELCOM98.WAV                                                                                             
-a----        28-Aug-07     13:08        2646058 GrandchildThumperLong.wav  

#>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在使用 distinct 时返回所有列? - Is there a way to return all columns when using distinct? 使用 groupby 和 union all 时有没有办法返回不存在的值? - Is there a way to return non existent values when using groupby and union all? Powershell将SQL返回数据集保存到CSV文件中 - Powershell to save sql return dataset into csv file 使用聚合MIN时,我无法让mysql返回最后一行 - I can't get mysql to return last row when using the aggregate MIN PowerShell SQL部署脚本-处理新的子文件夹 - PowerShell sql deployment script - handle new subfolders 使用 PowerShell 从列中返回数据 - Return the Data from Column Using PowerShell 使用SQL Server“ LIKE”功能从文件路径表中查找文件路径的子文件夹 - Finding subfolders of a file path from table of file paths using SQL Server “LIKE” function 使用 SQL 返回存储在 oracle blob 列中的文件的可读“文件大小”的优雅方法是什么? - What is an elegant way to return a readable 'file size' of a file stored in an oracle blob column using SQL? 在 snowflake 中使用 SUM function 时,有没有办法返回零而不是空字符串? - Is there a way to return zero vs. an empty string when using the SUM function in snowflake? 如何使用powershell执行.sql文件? - How to execute .sql file using powershell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM