Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据powershell中的名称对照片进行排序_Powershell_Photo - Fatal编程技术网

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

根据powershell中的名称对照片进行排序,powershell,photo,Powershell,Photo,我已使用高级重命名器按日期重命名我的所有照片,因此它们都有以下名称: 2017-10-14_8;39_kyd.jpg 其中8;39是时间,kyd是由三个随机字符组成的字符串,用于减少或消除重复名称。我想编写一个powershell脚本,将它们全部排序到文件夹中,例如: C:\Pictures\2017\10月 其中第一个目录是年份,第二个目录是月份。如果照片没有拍摄日期元数据,则其名称为: -_)_kyd.jpg 我想把它分类到一个手动文件夹中,这个文件夹位于C:\Pictures。我正在尝试使

我已使用高级重命名器按日期重命名我的所有照片,因此它们都有以下名称:

2017-10-14_8;39_kyd.jpg

其中8;39是时间,kyd是由三个随机字符组成的字符串,用于减少或消除重复名称。我想编写一个powershell脚本,将它们全部排序到文件夹中,例如:

C:\Pictures\2017\10月

其中第一个目录是年份,第二个目录是月份。如果照片没有拍摄日期元数据,则其名称为:

-_)_kyd.jpg

我想把它分类到一个手动文件夹中,这个文件夹位于C:\Pictures。我正在尝试使用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
}
}

但我不知道如何使用ForEach对象命令循环浏览每张图片。有人能提出一种方法来实现这一点吗?谢谢大家!

看起来您不正确地使用了移动项。使用$\.FullName作为-Path参数的参数。如前所述,您反复尝试将整个源文件夹移动到目标文件夹中

您的字符串比较操作错误。使用-eq

子字符串调用得到的字符太少。参数是索引和计数。当然,索引是从零开始的,但count是您想要的实际字符数

另外,$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\
  }
}
有几个问题:

检索文件名时,删除了名称的子字符串 如果条件比较-应为“-eq” 另外,请验证文件夹是否存在或未添加到

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
    }
}

还有其他bug,比如$Year=$filename.Substring0,3只拉201。它可以工作!是的,我知道我的代码会充满问题。主要是为了传达我所要做的事情的概念。谢谢你的帮助!嘿它起作用了!谢谢我知道我的代码会充满bug,我对powershell的使用还很陌生。我感谢你抽出时间来帮助我。