powershell 按年/月对文件夹中的文件进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 按年/月对文件夹中的文件进行排序相关的知识,希望对你有一定的参考价值。

<#
Original from:
https://www.thomasmaurer.ch/2015/03/move-files-to-folder-sorted-by-year-and-month-with-powershell/

Sort files inside a folder and move them to YYYY/MM folders.



#>

# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\Thomas\OneDrive\OneDrive Camera Roll' -Recurse | where {!$_.PsIsContainer}

# List Files which will be moved
$files

# Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = 'C:\Users\Thomas\OneDrive\pictures\Albums'

foreach ($file in $files)
{
# Get year and Month of the file
# I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()

# Out FileName, year and month
$file.Name
$year
$month

# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $month
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}

# Move File to new location
$file | Move-Item -Destination $Directory
}

以上是关于powershell 按年/月对文件夹中的文件进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何按月对 PHP 中的 SQL 记录进行排序?

在数据表中按月对数据库表中的数据进行分组

oracle表分区详解(按天按月按年等)

如何按月对数组中的值进行分组?

在 Powershell 中查询 XML 文件中的值

使用Powershell仅选择文件夹中的一个文件