【问题标题】:Powershell - Script not entering for each loopPowershell - 每个循环都没有输入脚本
【发布时间】:2016-08-05 19:12:07
【问题描述】:

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

我的问题是,当我运行脚本时,它不会执行我在 for each 循环中编写的任何操作。只有当我在它上面写脚本时。有人可以请教吗?

$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 

【问题讨论】:

  • $file in $folder 是什么意思?
  • 您在脚本块之外声明文件夹变量并且不将其作为参数传递,因此脚本块内的$folder 为空($counter$Files 也是如此)。将它作为参数传递或在脚本块中声明它,也是@AgentK 写的
  • 你能发帖告诉我你的意思吗?到目前为止,我没有得到不同的结果。

标签: shell loops powershell foreach hashtable


【解决方案1】:

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

ForEach ($file in $folder) 更改为 ForEach ($file in (gci $folder))

【讨论】:

    【解决方案2】:

    给你,不过你得把文件夹改回来:)

    $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
    

    这应该使您能够将哈希表中的数据持久化

    【讨论】:

    • 哈希表每次都被重新创建,因此阻止了 foreach 循环到达 elseif。我认为如果所有变量和哈希表都是全局的会更好。你能告诉我怎么做吗?
    • @user3162426 问题是你放弃了一次脚本块,从那时起,每次它被触发时,它都会在自己的上下文中运行。如果您需要将数据从一次运行转移到下一次运行,则需要将其存储在脚本之外,例如在注册表中。 Powershell 变量仅在它们在 afaik 中创建的会话中持续存在,无论是否全局
    • @user3162426 我添加了一些代码,允许您从文件中存储和加载哈希表,这样您就可以让它在运行中持续存在。清空哈希表时不要忘记清空文件,否则会遇到问题;)
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2019-05-17
    • 2017-04-17
    • 2020-03-09
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多