Powershell - 为每个循环的变量分配文件名时缺少属性错误

时间:2016-04-13 14:16:08

标签: shell powershell variables foreach hashtable

我正在尝试遍历文件夹,获取文件夹中每个文件的文件名,而不是将该文件名分配给变量。然后我会在哈希表中使用该变量。这是我遇到问题并继续收到此错误的地方:

At C:\Users\jnwankwo\Documents\IUR.ps1:19 char:16
+ Files.Add(fName, Counter)
+                ~
Missing argument in parameter list.

以下是代码:

$folder = 'C:\Users\jnwankwo\Documents\IUR Test\r' # Enter the root path you          want to monitor. 
$Failedfolder = 'C:\Users\jnwankwo\Documents\IUR Test\r'
$filter = '*.*'  # You can enter a wildcard filter here. 
$Files = @{}
$Counter = 1


# In the following line, you can change 'IncludeSubdirectories to $true if         required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property     @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName,     LastWrite'} 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 

ForEach ($file in folder) { 

$fName = file.Name

if (Files.ContainsKey(fName) = false)
{

Files.Add(fName, Counter)

}

if ((Files.ContainsKey(fName) = true) -and (Files.Item(fName) -lt 3))
{
Move-Item C:\Users\jnwankwo\Documents\IUR Test\r\*.txt     C:\Users\jnwankwo\Documents\IUR Test -force
Files.Set_Item(fName, Counter++)

}
ElseIf ((Files.ContainsKey(fName) = true) -and (Files.Item(fName) == 3))
{
Files.Remove(fName)
Move-Item C:\Users\jnwankwo\Documents\IUR Test\r\*.txt Failedfolder -force
}

}

} 

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您在变量之前缺少$。 因此,请将所有fName替换为$fName。您还必须将file.Name重命名为$file.Name。您还错过了$ Counter上的Files.Add($fName, Counter),您必须写$false而不是false ....

这应该是固定版本:

$folder = 'C:\Users\jnwankwo\Documents\IUR Test\r' # Enter the root path you          want to monitor. 
$Failedfolder = 'C:\Users\jnwankwo\Documents\IUR Test\r'
$filter = '*.*'  # You can enter a wildcard filter here. 
$Files = @{}
$Counter = 1


# In the following line, you can change 'IncludeSubdirectories to $true if         required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property     @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName,     LastWrite'} 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 

    ForEach ($file in $folder) 
    { 
        $fName = $file.Name

        if (-not $Files.ContainsKey($fName))
        {
            $Files.Add($fName, $Counter)
        }

        if (($Files.ContainsKey($fName)) -and ($Files.Item($fName) -lt 3))
        {
            Move-Item 'C:\Users\jnwankwo\Documents\IUR Test\r\*.txt'  'C:\Users\jnwankwo\Documents\IUR Test' -force
            $Files.Set_Item($fName, $Counter++)

        }
        ElseIf (($Files.ContainsKey($fName)) -and ($Files.Item($fName) -eq 3))
        {
            $Files.Remove($fName)
            Move-Item 'C:\Users\jnwankwo\Documents\IUR Test\r\*.txt' $Failedfolder -force
        }
    }
} 

答案 1 :(得分:0)

是的,正如jisaak所说,你已经错过了带有表达式的变量。

所以用变量替换所有声明可能会解决问题。

另外,当您下次尝试创建任何PowerShell脚本时,请尝试使用 PSDebug cmdlet 来调试脚本。它非常方便。我发现它非常有用。

只需在运行脚本之前添加以下代码,或者在脚本顶部添加。

Set-PSDebug -Trace 1