저는 1 주일이나 2 주일의 더 나은 부분을 위해 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 용 폴더가 있습니까? 생성하지 않을 경우. "유형"중 하나에 폴더가없는 경우 오류가 발생하여 자동으로 폴더를 만들려고하기 때문에 다른 폴더가 있어야합니다. 스크립트를 실행하면 경로가 null이됩니다.
오류 : 당신이 물어 주저하지 말아 주시기 바랍니다 문의 사항이있을 경우
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
어떤 도움을 크게 감상 할 수있다.
어디에서 경로가 null 오류입니까? – manojlds
오류에 따라 : 줄 11 Char 26 또는 줄 12 Char 45 '$ 경로 = $ _. 이름 + ".csv"; $ _. 그룹' | ' – Anubis