简体   繁体   English

在powershell中根据名称对照片进行排序

[英]Sort photos based on name in powershell

I have used Advanced Renamer to rename all my photos by date, so they all have names such as:我使用 Advanced Renamer 按日期重命名我的所有照片,因此它们都有如下名称:

2017-Oct-14_8;39_kyd.jpg 2017-Oct-14_8;39_kyd.jpg

Where "8;39" is the time and "kyd" is a string of three random characters to reduce eliminate duplicate names.其中“8;39”是时间,“kyd”是一个由三个随机字符组成的字符串,以减少重复名称。 I would like to write a powershell script to sort them all into folders such as:我想编写一个powershell脚本将它们全部分类到文件夹中,例如:

C:\\Pictures\\2017\\Oct C:\\图片\\2017\\十月

Where the first directory would be the year and the second directory would be the month.第一个目录是年份,第二个目录是月份。 If a photo does not have date taken metadata, it's name would be:如果照片没有拍摄日期元数据,则其名称为:

"--_;_kyd.jpg" “--_;_kyd.jpg”

and I would like to sort it into a "MANUAL_SORT" folder located is C:\\Pictures.我想将它分类到位于 C:\\Pictures 的“MANUAL_SORT”文件夹中。 I am trying to use powershell to do this and this is what I have so far:我正在尝试使用 powershell 来做到这一点,这就是我到目前为止所拥有的:

$SourceFolder = 'C:\Pictures\Test'
$DestinationFolder = 'C:\Pictures\Test_Output'

Get-ChildItem -Path $SourceFolder -Filter *.jpg | ForEach-Object
{
$filename = $_.Name.Substring(0,7);
if ($filename.Substring(0,3) = "--_;")
{
    Move-Item -Path $SourceFolder -Destination "$DestinationFolder\MAUNAL_SORT"
}
else
{
$Year = $filename.Substring(0,3)
$Month = $filename.Substring(5,7)
Move-Item -Path $SourceFolder -Destination $DestinationFolder\$Year\$Month
}
}

But I can't figure out how to use the ForEach-Object command to cycle through each picture.但是我不知道如何使用 ForEach-Object 命令来循环查看每张图片。 Can anyone suggest a method to accomplish this?任何人都可以提出一种方法来实现这一目标吗? Thank you!谢谢!

It looks like you're using Move-Item incorrectly.您似乎错误地使用了 Move-Item。 Use $_.FullName as the argument to the -Path parameter.使用$_.FullName作为 -Path 参数的参数。 As written, you're repeatedly trying to move the entire source folder into the destination.正如所写,您反复尝试将整个源文件夹移动到目标中。

Your string comparison operations are wrong.您的字符串比较操作是错误的。 Use -eq .使用-eq

Your substring calls are getting one too few characters.您的子字符串调用得到的字符太少了。 The parameters are index and count .参数是indexcount Index is zero-based, of course, but count is the actual number of characters you want.当然,索引是从零开始的,但计数是您想要的实际字符数。

Also, the $filename variable is only accomplishing an extra call to Substring(), it's not useful in the rest of the script.此外, $filename变量只是完成对 Substring() 的额外调用,它在脚本的其余部分没有用。

gci $SourceFolder -Filter *.jpg | foreach {
  $YYYY = $_.Name.Substring(0,4)
  if ($YYYY -eq '--_;') {
    mv $_.FullName $DestinationFolder\MANUAL_SORT\
  } else {
    $MM = $_.Name.Substring(5,3)
    mv $_.FullName $DestinationFolder\$YYYY\$MM\
  }
}

A couple of issues:几个问题:

  • substring of name while retrieving filename, removed that code检索文件名时名称的子字符串,删除了该代码
  • if condition comparison - it should be '-eq'如果条件比较 - 它应该是'-eq'

Also please verify folder exists or not added #TODO另外请验证文件夹存在或未添加#TODO

Get-ChildItem -Path $SourceFolder -Filter *.jpg | ForEach-Object {
    $filename = $_.Name;

    if ($filename.Substring(0,3) -eq "--_")
    {


        Move-Item -Path $_.FullName -Destination "$DestinationFolder\MAUNAL_SORT\"
    }
    else
    {
    $Year = $filename.Substring(0,3)
    $Month = $filename.Substring(5,7)
    #TODO: test path if directory exists else create it
    Move-Item -Path $_.FullName  -Destination $DestinationFolder\$Year\$Month
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM