Powershell - 将多个 CSV 导出到唯一的文件夹中

发布于 2024-12-10 13:24:15 字数 3078 浏览 0 评论 0原文

一两周的大部分时间里,我一直在编写 PowerShell 脚本。我已经能够使其某些部分正常工作,但是我无法完全实现自动化。

我每天都会处理很多 CSV 文件,我的任务是将它们上传到我们的软件中,但有时它们太大而无法处理,所以我根据它们的“类型”将它们分解(这是 CSV 中的一列) ),然后我将其导出到每个“类型”的单个 CSV 中。我已经能够通过以下方式完成此任务:

$file = gci -Filter "*.csv";
Import-Csv $file `
| Group-Object –Property “type” `
| Foreach-Object `
    { 
        $path=$_.name+”.csv” ; $_.group `
        | Export-Csv –Path $path –NoTypeInformation 
    }

因此,对于每个单独的 CSV,这都非常有效。不幸的是,我没有时间对每个单独的 CSV 执行此操作。现在我来看看我的另一个 PowerShell 脚本:

get-childitem -Filter "*.csv" `
| select-object basename `
    | foreach-object{ $path=$_.basename+".csv" #iterate through files.
        if(!(Test-Path -path $_.basename)) #If the folder of the file can't be found then it will attempt to create it.
        {
            New-Item $_.basename -type directory; $file=$_.basename+".csv";
            Import-Csv $file `
            | Group-Object -Property "Type" `
            | Foreach-Object {
                $path=$_.name+".csv"; $_.group `
                |  ` 
                    if(!(Test-Path -path $path2))
                    {
                        New-Item $path2 -type directory
                        Export-Csv -Path $path2 + "\" + $path -NoTypeInformation
                    }
                    else
                    {
                        "Failed on: " + $_.basename
                        #Export-Csv -Path $_.basename + "\" + $path -NoTypeInformation
                    }                    
                }
        }
        else
        {
        Import-Csv $path `
            | Group-Object -Property "Type" `
            | Foreach-Object {$path=$_.basename+".csv" ; $_.group                     
                if(Test-Path -path $._)
                    {
                        New-Item $path2 -type directory
                        Export-Csv -Path $path2 + "\" + $path -NoTypeInformation
                    }
                    #else
                    #{
                        Write-Host "Failed on: $_.basename"
                        #Export-Csv -Path $_.basename + "\" + $path -NoTypeInformation
                    #}                    
                }                
        }
}

我只是无法理解“为什么”它不能有效地工作。我有两个条件。有 CSV 的文件夹吗?如果没有创建一个。我必须有另一个,因为其中一个“类型”包含一个 \ ,如果我没有该文件夹,它会出错,所以我会自动尝试创建它。当我运行脚本时,我发现路径为空。

错误是:

术语“ ”未被识别为 cmdlet、函数、 脚本文件,或可运行的程序。检查姓名的拼写,或者 如果包含路径,请验证该路径是否正确,然后重试。 在 C:\Users\c.burkinshaw\foldermake.ps1:11 字符:26 + | ` <<<<< + 类别信息:ObjectNotFound:(:String) [],CommandNotFoundException + FullQualifiedErrorId:CommandNotFoundException

Test-Path:无法将参数绑定到参数“Path”,因为它为空。
在 C:\Users\c.burkinshaw\foldermake.ps1:12 字符:45
+ if(!(测试路径 -path <<<<$path2))
    + CategoryInfo : InvalidData: (:) [测试路径],ParameterBindingValidationException
    + FullQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

任何帮助将不胜感激,如果您有疑问,请随时询问。

I have been working on a PowerShell script for the better part of well a week or two. I've been able to get some parts of it working however I'm unable to fully get this automated.

I deal with a lot of CSV files on a daily basis, I have been tasked with uploading them into our software however sometimes they're too large to handle so I break them down based upon their "type" (it's a column in the CSV) and I export it to a single CSV per "type". I've been able to accomplish this with the following:

$file = gci -Filter "*.csv";
Import-Csv $file `
| Group-Object –Property “type” `
| Foreach-Object `
    { 
        $path=$_.name+”.csv” ; $_.group `
        | Export-Csv –Path $path –NoTypeInformation 
    }

So this works wonderfully, for each individual CSV. Unfortunately I don't have the time to do this for each individual CSV. Now I come to my other PowerShell script:

get-childitem -Filter "*.csv" `
| select-object basename `
    | foreach-object{ $path=$_.basename+".csv" #iterate through files.
        if(!(Test-Path -path $_.basename)) #If the folder of the file can't be found then it will attempt to create it.
        {
            New-Item $_.basename -type directory; $file=$_.basename+".csv";
            Import-Csv $file `
            | Group-Object -Property "Type" `
            | Foreach-Object {
                $path=$_.name+".csv"; $_.group `
                |  ` 
                    if(!(Test-Path -path $path2))
                    {
                        New-Item $path2 -type directory
                        Export-Csv -Path $path2 + "\" + $path -NoTypeInformation
                    }
                    else
                    {
                        "Failed on: " + $_.basename
                        #Export-Csv -Path $_.basename + "\" + $path -NoTypeInformation
                    }                    
                }
        }
        else
        {
        Import-Csv $path `
            | Group-Object -Property "Type" `
            | Foreach-Object {$path=$_.basename+".csv" ; $_.group                     
                if(Test-Path -path $._)
                    {
                        New-Item $path2 -type directory
                        Export-Csv -Path $path2 + "\" + $path -NoTypeInformation
                    }
                    #else
                    #{
                        Write-Host "Failed on: $_.basename"
                        #Export-Csv -Path $_.basename + "\" + $path -NoTypeInformation
                    #}                    
                }                
        }
}

I just can't wrap my head around "why" this isn't working effectively. I have two conditionals. Is there a folder for the CSV? If no create one. I have to have another one because one of the "types" contains a \ which errors out if I don't have the folder, so I automatically try to create it. When I run the script I get the Path is null.

The Error is:

The term ' ' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At C:\Users\c.burkinshaw\foldermake.ps1:11 char:26
+ | ` <<<<
+ CategoryInfo : ObjectNotFound: ( :String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\c.burkinshaw\foldermake.ps1:12 char:45
+                         if(!(Test-Path -path <<<<  $path2))
    + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

Any help would be greatly appreciated, if you have questions please don't hesitate to ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2024-12-17 13:24:15

您尚未在任何地方定义 $path2 ,因此类似 test-path -path $path2 的内容会说 path 为 null。在一个地方,您使用 $._ ,这将再次出现错误。

在问题更新后进行编辑并显示错误消息:

您的错误消息也说了同样的内容

测试路径:无法将参数绑定到参数“路径”,因为它是
无效的。在 C:\Users\c.burkinshaw\foldermake.ps1:12 字符:45
+ if(!(测试路径-path <<<< $path2))

另外另一个错误是:

$path=$_.name+".csv"; $_.group `
                |  ` 

您想在这里用 $_.group 做什么?

这是不正确的。您不能执行 $_.group | 并提供一些 if 语句。

其他评论:

为什么使用 $_.basename 然后附加 .csv?您可以只使用 $_.name。尝试不使用select-object basename - 我看不到该值。

将通用的 import-csv 和 export-csv 部分提取到一个函数中。

You have not defined $path2 anywhere, so something like test-path -path $path2 will say path is null. And in one place you are using $._ which will again give errors.

Edit after question updated with error message:

Your error message also says the same

Test-Path : Cannot bind argument to parameter 'Path' because it is
null. At C:\Users\c.burkinshaw\foldermake.ps1:12 char:45
+ if(!(Test-Path -path <<<< $path2))

Also the other error is in:

$path=$_.name+".csv"; $_.group `
                |  ` 

what are you trying to do here with the $_.group?

It is not proper. You cannot do $_.group | and provide some if statement.

Other comments:

Why are using $_.basename and then appending .csv? You could have just used $_.name. Try to not use the select-object basename - I don't see the value.

Extract the common import-csv and export-csv part into a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文