Powershell - 不为每个循环输入脚本

时间:2016-04-14 09:54:56

标签: shell loops powershell foreach hashtable

我有一个脚本设置,每次创建一个文件时为每个循环输入一个。一旦进入循环,它将一个文件移动到另一个文件夹,如果同一个文件必须移动3次,它将把文件移动到另一个表并从哈希表中删除它的记录。

我的问题是,当我运行脚本时,它不执行我在每个循环中编写的任何内容。只有我在上面写脚本。有人可以建议吗?

$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


$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.clear()
            Move-Item 'C:\Users\jnwankwo\Documents\Failed\' $Failedfolder -force
        }
    }
} 

# To stop the monitoring, run the following commands: 
# Unregister-Event FileCreated 

2 个答案:

答案 0 :(得分:2)

我在你的代码中发现了一件事。

ForEach($文件夹中的$文件)更改为 ForEach((gci $ folder)中的$文件)

答案 1 :(得分:0)

在这里,您将不得不更改文件夹:)

$folder = 'C:\temp\test' # Enter the root path you              want to monitor. 
$filter = '*.*'  # You can enter a wildcard filter here. 

# 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 { 
$folder = 'C:\temp\test' # Enter the root path you              want to monitor. 
$Failedfolder = 'C:\temp\test3' 
$Files = @{}
$Counter = 1

ForEach ($file in (gci $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 $file.Fullname 'C:\Users\jnwankwo\Documents\IUR Test' -force
        $Files.Item($fName) = $Counter++

    }
    ElseIf (($Files.ContainsKey($fName)) -and ($Files.Item($fName) -eq 3))
    {
        $Files.clear()
        Move-Item $file.Fullname $Failedfolder -force
    }
}
} 

增加:

要将Hashtable存储到文件并在下次运行时重新导入,您可以使用以下代码:

#store hashtable to file
$Files.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Out-File files_ht.txt

#to import again
$Files = Get-Content files_ht.txt | Convertfrom-StringData

这应该使您能够获得哈希表持久性数据