首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历文件目录的PoshRSJob循环

遍历文件目录的PoshRSJob循环
EN

Stack Overflow用户
提问于 2017-02-26 21:55:48
回答 1查看 218关注 0票数 1

我尝试遍历一个目录(按最小文件排序),获取路径和文件名,然后将这些结果放入utility.exe程序中。

我尝试用PoshRSJob做这个多线程,但我甚至没有在任务管理器中看到实用程序,我得到了一个错误“空键不允许在散列文字中。”,对于每个存在的文件(如果目录中有50个文件,那么我会得到50个错误)。我也不能测试节流是否有效,因为实际上没有任何东西在运行。

代码语言:javascript
复制
Import-Module C:\PoshRSJob.psm1
Function MultiThread($SourcePath,$DestinationPath,$CommandArg, $MaxThreads){
    if($CommandArg -eq "import") {
        $fileExt = "txt"
    }else{
        $fileExt = "ini"
    }
    $ScriptBlock = {
        Param($outfile, $cmdType, $fileExtension)
        [pscustomobject] @{
            #get the full path
            $filepath = $_.fullname     
            #get file name (minus extension)
            $filename = $_.basename
            #build output directory
            $destinationFile = "$($outfile)\$($filename).$($fileExtension)"
            #command to run
            $null = .\utility.exe $cmdType -source `"$filepath`" -target `"$destinationFile`"
        }
    }
    #get the object of the passed source directory, and pipe it into start-rsjob
    Get-ChildItem $SourcePath | Sort-Object length | Start-RSJob -ScriptBlock $ScriptBlock -ArgumentList $DestinationPath, $CommandArg, $fileExt -Throttle $MaxThreads

    Wait-RSJob -ShowProgress | Receive-RSJob
    Get-RSJob | Receive-RSJob
}
MultiThread "D:\input" "D:\output" "import" 3
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-26 22:01:12

您的scriptblock正在创建一个对象,您在其中将$null = .\utility.exe +++定义为一个属性。正如它所说的,$null (nothing)的值不能是属性名。我建议你直接运行这些线路..

您可能还希望更改Wait-RSJob-part。您不指定作业,因此它永远不会等待任何东西。尝试:

尝试将scriptblock更改为:

代码语言:javascript
复制
Import-Module C:\PoshRSJob.psm1
Function MultiThread($SourcePath,$DestinationPath,$CommandArg, $MaxThreads){
    if($CommandArg -eq "import") {
        $fileExt = "txt"
    }else{
        $fileExt = "ini"
    }

    $ScriptBlock = {
        Param($outfile, $cmdType, $fileExtension)
        #get the full path
        $filepath = $_.fullname     
        #get file name (minus extension)
        $filename = $_.basename
        #build output directory
        $destinationFile = "$($outfile)\$($filename).$($fileExtension)"
        #command to run
        $null = .\utility.exe $cmdType -source `"$filepath`" -target `"$destinationFile`"
    }

    #get the object of the passed source directory, and pipe it into start-rsjob
    Get-ChildItem $SourcePath | Sort-Object length | Start-RSJob -ScriptBlock $ScriptBlock -ArgumentList $DestinationPath, $CommandArg, $fileExt -Throttle $MaxThreads

    Get-RSJob | Wait-RSJob -ShowProgress | Receive-RSJob
}
MultiThread "D:\input" "D:\output" "import" 3
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42469370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档