powershell - foreach + get-childitem 的问题
问题描述
我正在编写一个脚本来组织我的媒体。在添加到我的媒体中心之前,我将文件下载到一个目录中以容纳它们。例如,如果我有一个名为Breaking.Bad.S01E01.DVDRip.XviD-ORPHEUS.avi
我希望脚本获取节目名称的文件,请检查 S01 上的季节并将该文件移动到另一个磁盘中的文件夹,例如e:\series\breaking bad\season 01
到目前为止,它会检查文件是否调用 s01e01 或S01E01 或 s01.e01 或 S01.E01 并返回 Breaking Bad\Season 01,创建移动到的路径和移动动作本身
我有该脚本的一部分,但我无法让 get-childitem 与 foreach 一起使用。
这就是我到目前为止所拥有的以及我得到的错误:
代码
$FilesList = Get-ChildItem -name -recurse -include *.mkv,*.mp4,*.srt,*.avi
$FilesList
foreach ($FL_Item in $FilesList)
{
$SeriesName = ($FL_Item.BaseName -split '\.s\d')[0].Replace('.', ' ')
$SE_Info = $FL_Item.BaseName.Split('.')[-3] -split 'e'`
$Season = $SE_Info[0] -replace 's', 'Season '
#$Episode = 'Episode{0}' -f $SE_Info[1]
$SeriesName
$Season
#$Episode
$SeriesDirectory = Join-Path -Path "$SeriesName" -ChildPath "$Season"
$SeriesDirectory
#$MoverArchivo = move-item -path $FileName -destination e:\series\$SeriesDirectory
#$MoverArchivo
''
}
输出我得到
Breaking.Bad.S01E01.DVDRip.XviD-ORPHEUS.avi
Breaking.Bad.S01E01.DVDRip.XviD-ORPHEUS.spa.srt
Breaking.Bad.S04E01.Box.Cutter.720p.hdtv.x264-orenji.mkv
Breaking.Bad.S04E01.Box.Cutter.720p.hdtv.x264-orenji.spa.srt
Breaking.Bad.S05E15.720p.HDTV.x264-EVOLVE.mkv
Breaking.Bad.S05E15.720p.HDTV.x264-EVOLVE.spa.srt
Path Of Blood (2018) [WEBRip] [1080p] [YTS.AM]\Path.Of.Blood.2018.1080p.WEBRip.x264-[YTS.AM].mp4
They Shall Not Grow Old (2018) [BluRay] [1080p] [YTS.AM]\They.Shall.Not.Grow.Old.2018.1080p.BluRay.x264-[YTS.AM].mp4
错误
You cannot call a method on a null-valued expression.
At D:\shared\temp\test3.ps1:8 char:5
+ $SE_Info = $FL_Item.BaseName.Split('.')[-3] -split 'e'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Cannot index into a null array.
At D:\shared\temp\test3.ps1:10 char:5
+ $Season = $SE_Info[0] -replace 's', 'Season '
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Join-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At D:\shared\temp\test3.ps1:17 char:37
+ $SeriesDirectory = Join-Path -Path "$SeriesName" -ChildPath "$Sea ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Join
PathCommand
有什么想法可能是错的吗?
解决方案
您正在使用第一行中的-name
参数:Get-ChildItem
$FilesList = Get-ChildItem -name -recurse -include *.mkv,*.mp4,*.srt,*.avi
这意味着它将只返回文件名作为字符串。
稍后,在您的循环中,您使用属性访问每个元素BaseName
,该属性是FileInfo
对象的属性,而不是字符串。所以,$FL_Item.BaseName
返回一个空字符串,你会得到如图所示的错误。
只需删除-name
它,它应该可以工作(或者至少你不会得到这些错误)。
推荐阅读
- bash - Bash如何减去用户每次输入的猜测次数?
- javascript - 用户停止输入后运行函数
- python - Python 包和方法未导入
- android - 减少安卓应用程序内存消耗的好处
- c# - Acumatica - 另一个进程已添加/更新记录 - 使用 DAC 和图形扩展创建库存项目
- haskell - 野外 LTS 的发病率是多少?
- c# - C# 上的简化 LDAP/AD 服务器
- java - 在java Entity类中没有从getAll方法中获取值
- sql - 从 SQL 中的 3 个连接表返回 1 条记录
- javascript - 如果存在重复值,则从数组中删除一个对象。JavaScript