在Windows中,如何找出按参数排序的文件夹

时间:2019-05-25 08:37:24

标签: windows powershell

我正在Node.js中构建图像查看应用程序。我注意到在Windows中,文件夹中的图片可以按名称,大小,状态,类型,日期和标签等进行排序,并在按相同列表等进行排序后进行分组。

是否有一种获取排序参数的方法,或者只是检索文件的排序列表,将正则表达式 /\.(jpg|jpg_large|jpeg|jpe|jfif|jif|jfi|jpe|gif|png|ico|bmp|webp|svg)$/i 匹配为数组(例如:['c:\man.jpg', 'c:\woman.jpg']使用Powershell?

编辑:

本文使我更接近解决方案。 https://cyberforensicator.com/2019/02/03/shellbags-forensics-directory-viewing-preferences/

不幸的是,它没有解释如何获取给定文件夹的节点列表值,因此我使用了nirsoft的名为shellbagsview的应用程序来获取此值。无论如何,只要找到该值,其余的就很容易了。我提供了一个示例python脚本,该脚本在此处说明了如何完成此操作。

from winreg import *


# Registry is of the form:
# HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\1375\Shell\{5C4F28B5-F869-4E84-8E60-F11DB97C5CC7}
# where 1375 is a value called the NodeList, and {5C4F28B5-F869-4E84-8E60-F11DB97C5CC7} is a value under Shell chosen based on creation date. It is a good idea to look at the registry after getting the nodelist from shellbagsview
folder_reg_path = "Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\Shell\\Bags\\1375\\Shell\\{5C4F28B5-F869-4E84-8E60-F11DB97C5CC7}"

# the size of icons used by the folder
def get_folder_icon_size(reg_key):
    with OpenKey(HKEY_CURRENT_USER, reg_key) as key:
        value = QueryValueEx(key, 'IconSize')
        return '%d pixels' % (value[0])

# the folder view. details, list, tiles e.t.c       
def get_logical_view_mode(reg_key):
    with OpenKey(HKEY_CURRENT_USER, reg_key) as key:
        value = QueryValueEx(key, 'LogicalViewMode')
        logical_view_mode_dict = {1 : "Details view", 2 : "Tiles view", 3 : "Icons view", 4 : "List view", 5 : "Content view"}
        return logical_view_mode_dict[value[0]]


# folder view is based on view mode. so you can have a  logical view mode of icons view with a view mode of large icons for instance    
def get_folder_view_mode(reg_key):
    with OpenKey(HKEY_CURRENT_USER, reg_key) as key:
        value = QueryValueEx(key, 'Mode')
        # view_mode 7 is only available on xp. A dead os
        view_mode_dict = {1 : "Medium icons", 2 : "Small icons", 3 : "List", 4 : "Details", 5 : "Thumbnail icons", 6 : "Large icons", 8 : "Content"}
        return view_mode_dict[value[0]]


# how is the folder being sorted
def get_folder_sort_by(reg_key):
    with OpenKey(HKEY_CURRENT_USER, reg_key) as key:
        value = QueryValueEx(key, 'Sort')
        folder_sort_dict = {"0E000000" : "Date Modified", "10000000" : "Date Accessed", "0F000000" : "Date Created", "0B000000" : "Type", "0C000000" : "Size", "0A000000" : "Name", "02000000" : "Title", "05000000" : "Tags"}
        # we get a byte value which we will hexify and get a rather long string
        # similar to : 000000000000000000000000000000000100000030f125b7ef471a10a5f102608c9eebac0c000000ffffffff
        reg_value = value[0].hex()
        # now for this string, we need to get the last 16 strings. then we now get the first 8 out of it. so we will have
        folder_sort_dict_key = (reg_value[-16:][:8]).upper()
        return folder_sort_dict[folder_sort_dict_key]

# in what order is the folder being sorted. ascending or descending???
def get_folder_sort_by_order(reg_key):
    with OpenKey(HKEY_CURRENT_USER, reg_key) as key:
        value = QueryValueEx(key, 'Sort')
        folder_sort_dict = {"01000000" : "Ascending", "FFFFFFFF" : "Descending"}
        # we get a byte value which we will hexify and get a rather long string
        # similar to : 000000000000000000000000000000000100000030f125b7ef471a10a5f102608c9eebac0c000000ffffffff
        reg_value = value[0].hex()
        # now for this string, we need to get the last 16 strings. then we now get the last 8 out of it. so we will have
        folder_sort_dict_key = (reg_value[-16:][-8:]).upper()
        return folder_sort_dict[folder_sort_dict_key]


icon_size = get_folder_icon_size(folder_reg_path)
logical_view_mode = get_logical_view_mode(folder_reg_path)
view_mode = get_folder_view_mode(folder_reg_path)
sorted_by = get_folder_sort_by(folder_reg_path)
sorted_by_order = get_folder_sort_by_order(folder_reg_path)
print ('The folder icon size is %s' % icon_size)
print('The folder logical view mode is %s' % logical_view_mode)
print('The folder view mode is %s' % view_mode)
print('The folder is sorted by %s in %s order' % (sorted_by, sorted_by_order))

2 个答案:

答案 0 :(得分:1)

问题本身以及 运行它的环境尚不清楚。

当您引用PowerShell和RegEx来限制特定扩展时,

使用此示例树:

> tree /f a:\
A:\
└───Test
        boy.bmp
        child.ico
        girl.gif
        man.jpg
        woman.jpg

此脚本:

Get-ChildItem -Path A:\Test -File |
 Where-Object Extension -match '\.(jpg|jpg_large|jpeg|jpe|jfif|jif|jfi|jpe|gif|png|ico|bmp|webp|svg)$' |
  Sort-Object Name | 
   Select-Object -ExpandProperty FullName |
    ConvertTo-Json -Compress

产量:

["A:\\Test\\boy.bmp","A:\\Test\\child.ico","A:\\Test\\girl.gif","A:\\Test\\man.jpg","A:\\Test\\woman.jpg"]

答案 1 :(得分:0)

IShellView实现(资源管理器的文件列表部分)在需要加载/保存其状态时向其IShellBrowser询问流。我的建议是托管一个“浏览到该文件夹​​”的IExplorerBrowser实例,并向视图询问其项目。我不知道您是否可以询问它对哪个列进行了排序,但是仅按排序顺序获取项目就足够满足您的需求。

我不知道如何使用脚本语言来实现此目的,但我认为PS支持足够的COM来使之成为可能。