我一直在为一周或两周的较好部分开发PowerShell脚本。我已经能够得到它的某些部分,但我无法完全实现这种自动化。Powershell - 将多个CSV导出到独特的文件夹中
我每天处理很多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文件夹?如果没有创建一个。我必须有另一个,因为其中一个“类型”包含\如果我没有该文件夹出错,所以我会自动尝试创建它。当我运行脚本时,我得到的路径为空。
的错误是:
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
任何帮助将不胜感激,如果您有任何疑问,请不要犹豫,问。
你从哪里得到路径为空错误? – manojlds
根据错误:第11行字符26或第12行字符45 '$ path = $ _。name +“。csv”; $ _。组' | ' – Anubis