pdf文件不会在文件夹中移动:无法将参数绑定到参数"Path",因为它为null-ForEach-Object cmdlet与foreach循环 [英] pdf files are not moved in folders: cannot bind argument to parameter 'Path' because it is null - ForEach-Object cmdlet vs. foreach loops

查看:68
本文介绍了pdf文件不会在文件夹中移动:无法将参数绑定到参数"Path",因为它为null-ForEach-Object cmdlet与foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个脚本来创建从文本文件 (.txt) 中提取年份的文件夹.
我在 C:\Path 中有一些 pdf 文件,就像这样

I use a script that create folders extracting years from text file (.txt).
I have some pdf files in C:\Path like so

Ben - Hur.pdf
Ben 10 - Corsa contro il tempo.pdf
Ben Hur - Dvd2.pdf
Ben-Hur - Dvd1.pdf
Bolide Rosso - Johnny Dark (1954).pdf
Hard Night (Permanent Midnight).pdf
The Dark Valley.pdf

代替文件文本( Text4.txt )就是这样

Instead file text (Text4.txt) is something this

Hard Night (Permanent Midnight 1998)
Ben-Hur (1925) 
Ben Hur (1959) [Storico [CURA] 
Ben Hur Dvd2 (1959) 
Ben Hur - Dvd1 (1959)[Top 250 - Ita Eng]
The Dark Valley (2014) [ - Ita Deu Aac] Western
Bolide Rosso - Johnny Dark (1954) [it] Azione
Ben - Hur (1959) 

从Text4.txt中

脚本提取年,并使该文件夹像这样

Script extract years from Text4.txt and make this folder like this

1925
1950
1998

但是当它应该移动pdf文件时,powershell脚本会向我返回此错误

But when it should move pdf files powershell script returns me this error

Move.Iten:无法将参数绑定到参数'Path',因为它为空

Move.Iten: cannot bind argument to parameter 'Path' because it is null

错误

这个错误很奇怪(我使用的是 Powershell 5).脚本可以正确创建文件夹,但不能在其中移动文件.脚本是这个

This error is strange (I use Powershell 5). Script create correctly folders but not moving files inside. Script is this

$movies = @()
(get-content C:\Path\Test4.txt) | foreach($_){
$properties = @{
date = $_.substring($_.IndexOf("(")+1,4)
name = $_.substring(0,$_.IndexOf("("))
}
write-host $date
write-host $name

$movies += New-Object PSObject -Property $properties
}

$torrentFiles = dir $torrentPath

foreach($movie in $movies){
$datePath = "C:\Path\$($movie.date)"
if(-not(test-path $datePath)) {
new-item $datePath -ItemType "directory"
}
$words = ($movie.name -split '\s') | ?{ $_.Length -gt 1}
$significant = $words.Count
 foreach($torrentFile in $torrentFiles){
 $matchingWords = 0
  foreach($word in $words){
   if($torrentFile.BaseName -match $word){
    $matchingWords += 1
   }
  }
  if($matchingWords -ge $significant){
  $_ | Move-Item -Destination $datePath
  }
 }
}

推荐答案

您使用 Foreach 循环,但随后尝试使用管道对象访问属性,就像在 Foreach-中一样对象循环.您需要将 $ _ 更改为具有实际值的东西,我猜是 $ torrentFile

Your using a Foreach loop but then trying to access a property using a pipeline object as you would in a Foreach-Object loop. you need to change $_ to something that has an actual value, I'm guessing $torrentFile

if($matchingWords -ge $significant){
  Move-Item -path $torrentfile -Destination $datePath
}

这篇关于pdf文件不会在文件夹中移动:无法将参数绑定到参数"Path",因为它为null-ForEach-Object cmdlet与foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆