C : \ Temp \ ConversionCSV \의 모든 XLSX 파일을 감지하고 CSV로 변환하는 스크립트를 만들려고합니다. 폴더에 파일이 하나만 있으면 제대로 작동합니다.
그러나 2 개의 파일이있는 경우 ForEach는 xlsx에서 csv 로의 변환을 시도하기 전에 두 FullName 값을 하나의 값으로 "연결"합니다 (분명히 그것이 작동하는 방식입니다). 이로 인해 변환이 실패합니다. ForEach-Object도 시도했습니다.
먼저 그룹화하지 않고 각 항목에서 스크립트를 실행할 수있는 방법이 있습니까? 아니면 대안? 확장자 만 변경할 수 있습니까 (잘못된 것 같습니다)?
추신 : 더 이상 원본 스크립트의 출처를 찾을 수 없어서 저자를 인정할 수 없습니다.
스크립트:
cls
Try {
$Directory = "C:\temp\ConversionCSV\"
$ExcelFiles = (Get-ChildItem $Directory | Where Extension -Like .XLS* `
| Select Name `
| Format-Table -HideTableHeaders `
| Out-String -ErrorAction Stop `
).trim()
#Files successfully detected:
#C:\temp\ConversionCSV\copy.xlsx
#C:\temp\ConversionCSV\FinancialSample.xlsx
Get-ChildItem $Repertoire | Select Name, Extension, `
BaseName, FullName, `
Directory, DirectoryName `
| Where Extension -eq .XLSX `
| Out-String
ForEach ($ExcelFile in $ExcelFiles)
{
$xlCSV = 6
$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.displayalerts = $False
##############################################################################
$FullName = $Directory+(Get-Variable -scope Local `
| Where Name -EQ 'ExcelFile' `
| Select Value `
| Format-table -HideTableHeaders -AutoSize -Wrap `
| Out-String -ErrorAction Stop).Trim()
Write-Host " `$FullName is $FullName"
##############################################################################
$CSVfilename = ($FullName -Replace "XLSX", "CSV").Trim()
##############################################################################
$Workbook = $Excel.Workbooks.Open($FullName)
$Workbook.SaveAs($CSVfilename,$xlCSV)
##############################################################################
$NewCSV= Get-ChildItem $Directory | Select Name, Extension, `
BaseName, FullName, `
Directory, DirectoryName `
| Where Extension -eq .CSV `
| Out-String -ErrorAction Stop
##############################################################################
#regionCloseExcel
$Excel.Quit()
If (Get-Process Excel)
{Write-Host "Closing Excel"
Stop-Process -Name Excel}
ElseIf (!(Get-Process Excel))
{Write-Host "`n Excel not runnning."}
Else {Write-Host "`n Unexpected event."}
#endregionCloseExcel
}
}
Catch {
Write-Host "`n---------------------ERROR--------------`n"
$_.Exception
}
Finally {Write-Host "`n----------------------END---------------"}
Excel 파일 Get-ChildItem $Repertoire
을 생성하기 위해 너무 많은 코드를 요청 하고 있어야 하는 이유를 이해하기가 어렵습니다 $FullName
.
작동하는 코드의 단순화 된 버전은 다음과 같습니다.
Try {
$Directory = "C:\temp\"
$ExcelFiles = (Get-ChildItem $Directory | Where Extension -Like ".XLS*")
ForEach ($ExcelFile in $ExcelFiles) {
# grab the FullName here -> no need to parse
$FullName = $ExcelFile.FullName
$xlCSV = 6
$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.displayalerts = $False
##############################################################################
Write-Host " `$FullName is $FullName"
##############################################################################
$CSVfilename = ($FullName -Replace "XLSX", "CSV").Trim()
##############################################################################
$Workbook = $Excel.Workbooks.Open($FullName)
$Workbook.SaveAs($CSVfilename,$xlCSV)
##############################################################################
$Excel.Quit()
If (Get-Process Excel)
{Write-Host "Closing Excel"
Stop-Process -Name Excel}
ElseIf (!(Get-Process Excel))
{Write-Host "`n Excel not runnning."}
Else {Write-Host "`n Unexpected event."}
#endregionCloseExcel
}
}
Catch { Write-Host "n---------------------ERROR--------------n" $_.Exception }
Finally {Write-Host "`n----------------------END---------------"}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다